This example shows you how to configure ActiveMQ to send and receive Stomp messages.
The example will start a ActiveMQ server configured with Stomp and JMS.
The client will open a socket to send one Stomp message (using TCP directly). The client will then consume a message from a JMS Queue and check it is the message sent with Stomp.
To run the example, simply type mvn verify -Pexample
from this directory
Socket socket = new Socket("localhost", 61613);
String connectFrame = "CONNECT\n" + "login: guest\n" + "passcode: guest\n" + "request-id: 1\n" + "\n" + Stomp.NULL; sendFrame(socket, connectFrame);
jms.queue.exampleQueue
(which corresponds to the ActiveMQ address for the JMS Queue exampleQueue
) with a text bodyString text = "Hello, world from Stomp!"; String message = "SEND\n" + "destination: jms.queue.exampleQueue\n" + "\n" + text + Stomp.NULL; sendFrame(socket, message); System.out.println("Sent Stomp message: " + text);
String disconnectFrame = "DISCONNECT\n" + "\n" + Stomp.NULL; sendFrame(socket, disconnectFrame);
socket.close();
initialContext = getContext(0);
Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue"); ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
connection = cf.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(queue);
connection.start();
TextMessage messageReceived = (TextMessage)consumer.receive(5000); System.out.println("Received JMS message: " + messageReceived.getText());
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();
}
}