January 23, 2021
Two versions of ActiveMQ
Java Runtime Environment (JRE) is needed
download ActiveMq from https://activemq.apache.org/ Download link https://activemq.apache.org/components/classic/download/
Extract the files into a directory
Run activemq start command to start the server
C:\....\apache-activemq-5.16.0\bin> activemq start
ActiveMQ will start a webserver which is displayed on the screen
open the below link in browser, you will be asked to enter password, use the below. http://127.0.0.1:8161/admin/
username: admin
password: admin
Queues
linkactivemq
Apache NMS ActiveMQ
61616
is the default port for ActiveMQ, so we have to connect localhost:61616
Create below XAML in the window
<Grid>
<StackPanel Margin="10">
<StackPanel Orientation="Horizontal">
<Button Content="Send message"
Name="sendButton"
Click="SendButton_Click" />
</StackPanel>
</StackPanel>
</Grid>
Type below code in code behind. I wanted to keep the code simple, so didn’t use MVVM concept, just simply using code behind.
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using System;
using System.Windows;
namespace ActiveMQPOC
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private IConnection connection;
private ISession session;
private IMessageProducer producer;
public MainWindow()
{
InitializeComponent();
}
private void Init()
{
Uri connecturi = new Uri("activemq:tcp://localhost:61616");
ConnectionFactory connectionFactory = new ConnectionFactory(connecturi);
// Create a Connection
this.connection = connectionFactory.CreateConnection();
this.connection.Start();
// Create a Session
this.session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
// Get the destination (Topic or Queue)
IDestination destination = this.session.GetQueue("testqueue");
// Create a MessageProducer from the Session to the Topic or Queue
this.producer = this.session.CreateProducer(destination);
this.producer.DeliveryMode = MsgDeliveryMode.NonPersistent;
}
private void SendButton_Click(object sender, RoutedEventArgs e)
{
// Create a messages
String text = "Test msg : " + DateTime.Now;
ITextMessage message = session.CreateTextMessage(text);
// Tell the producer to send the message
this.producer.Send(message);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Init();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Clean up
session.Close();
connection.Close();
}
}
}
send message
Queues
pagecreate one more C# project in the same solution. Add the same nuget package in this project also.
simple add a list box in xaml
<Grid>
<ListBox Name="lstBox">
</ListBox>
</Grid>
add below code in the windows’s code behind
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using System;
using System.Threading.Tasks;
using System.Windows;
namespace ActiveMQConsumer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private IConnection connection;
private ISession session;
private IMessageConsumer consumer;
public MainWindow()
{
InitializeComponent();
}
private void Init()
{
Uri connecturi = new Uri("activemq:tcp://localhost:61616");
ConnectionFactory connectionFactory = new ConnectionFactory(connecturi);
// Create a Connection
this.connection = connectionFactory.CreateConnection();
this.connection.Start();
// Create a Session
this.session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
// Get the destination (Topic or Queue)
IDestination destination = this.session.GetQueue("testqueue");
// Create a MessageProducer from the Session to the Topic or Queue
this.consumer = this.session.CreateConsumer(destination);
this.consumer.Listener += Consumer_Listener;
}
private void Consumer_Listener(IMessage message)
{
var txtMessage = message as ITextMessage;
lstBox.Dispatcher.Invoke(() => {
lstBox.Items.Add(txtMessage.Text);
});
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Init();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Clean up
consumer.Close();
session.Close();
connection.Close();
}
}
}
in visual studio, right click on a project —> Debug —> Start a new instance. start producer and consumer project.
by clicking the send message at producer program, we can see the messages reached at consumer’s side. At the same time we can refresh the admin webpage to see the status.