This example shows you how to configure ActiveMQ Artemis to send and receive Stomp messages using Stomp 1.1 protocol.
The example will start a ActiveMQ Artemis server configured with Stomp and JMS.
The client will open a socket to initiate a Stomp 1.1 connection and then 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" + "accept-version:1.1\n" + "host:localhost\n" + "login:guest\n" + "passcode:guest\n" + "request-id:1\n" + "\n" + END_OF_FRAME; sendFrame(socket, connectFrame);
String response = receiveFrame(socket); System.out.println("response: " + response);
jms.queue.exampleQueue
(which corresponds to the ActiveMQ Artemis address for the JMS Queue exampleQueue
) with a text bodyString text = "Hello World from Stomp 1.1 !"; String message = "SEND\n" + "destination:jms.queue.exampleQueue\n" + "\n" + text + END_OF_FRAME; 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();
}
}