To run the example, simply type mvn verify from this directory,
or mvn -PnoServer verify if you want to start and create the server manually.
This example shows you how to configure and use message groups via a connection factory with ActiveMQ Artemis.
Message groups are sets of messages that has the following characteristics:
You can make any message belong to a message group by setting a 'group-id' on the connection factory. All producers created via this connection factory will set that group id on its messages. In this example we set the group id 'Group-0'on a connection factory and send messages via 2 different producers and check that only 1 consumer receives them.
Alternatively, ActiveMQ's connection factories can be configured to auto group messages. By setting autogroup
to true on the ActiveMQConnectionFactory
(or setting <autogroup>true</autogroup>
in activemq-jms.xml
's connection factory settings), a random unique id
will be picked to create a message group. Every messages sent by a producer created from this connection factory will automatically
be part of this message group.
client-jndi.properties
file in the directory ../common/config
InitialContext initialContext = getContext();
Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer1 = session.createProducer(queue);
MessageProducer producer2 = session.createProducer(queue);
MessageConsumer consumer1 = session.createConsumer(queue);
consumer1.setMessageListener(new SimpleMessageListener("consumer-1"));
MessageConsumer consumer2 = session.createConsumer(queue);
consumer2.setMessageListener(new SimpleMessageListener("consumer-2"));
int msgCount = 10;
for (int i = 0; i < msgCount; i++)
{
TextMessage m = session.createTextMessage("producer1 message " + i);
producer1.send(m);
System.out.println("Sent message: " + m.getText());
TextMessage m2 = session.createTextMessage("producer2 message " + i);
producer2.send(m2);
System.out.println("Sent message: " + m2.getText());
}
connection.start();
String trueReceiver = messageReceiverMap.get("producer1 message " + 0);
for (int i = 0; i < msgCount; i++)
{
String receiver = messageReceiverMap.get("producer1 message " + i);
if (!trueReceiver.equals(receiver))
{
System.out.println("Group message [producer1 message " + i + "] went to wrong receiver: " + receiver);
result = false;
}
receiver = messageReceiverMap.get("producer2 message " + i);
if (!trueReceiver.equals(receiver))
{
System.out.println("Group message [producer2 message " + i + "] went to wrong receiver: " + receiver);
result = false;
}
}
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}