mirror of
https://github.com/apache/activemq-artemis.git
synced 2025-02-06 10:09:01 +00:00
https://issues.apache.org/jira/browse/ACTIVEMQ6-3 We are renaming packages from activemq6 to activemq as that's more generic and version independent The previous commit renamed the directories. On this commit now I'm changing the code. If we changed the code and the directories on the same commit git would remove and add a lot of files without recognizing the renames.
<html> <head> <title>HornetQ Stomp Example</title> <link rel="stylesheet" type="text/css" href="../common/common.css" /> <link rel="stylesheet" type="text/css" href="../common/prettify.css" /> <script type="text/javascript" src="../common/prettify.js"></script> </head> <body onload="prettyPrint()"> <h1>Stomp Example</h1> <p>This example shows you how to configure HornetQ to send and receive Stomp messages.</p> <p>The example will start a HornetQ server configured with Stomp and JMS.</p> <p>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.</p> <h2>Example step-by-step</h2> <p><i>To run the example, simply type <code>mvn verify</code> from this directory</i></p> <ol> <li>We create a TCP socket to connect to the Stomp port <pre class="prettyprint"> Socket socket = new Socket("localhost", 61613); </pre> <li>We send a CONNECT frame to connect to the server</li> <pre class="prettyprint"> String connectFrame = "CONNECT\n" + "login: guest\n" + "passcode: guest\n" + "request-id: 1\n" + "\n" + Stomp.NULL; sendFrame(socket, connectFrame); </pre> <li>We send a SEND frame (a Stomp message) to the destination <code>jms.queue.exampleQueue</code> (which corresponds to the HornetQ address for the JMS Queue <code>exampleQueue</code>) with a text body</li> <pre class="prettyprint"> String 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); </pre> <li>We send a DISCONNECT frame to disconnect from the server</li> <pre class="prettyprint"> String disconnectFrame = "DISCONNECT\n" + "\n" + Stomp.NULL; sendFrame(socket, disconnectFrame); </pre> <li>We close the TCP socket</li> <pre class="prettyprint"> socket.close(); </pre> <li>We create an initial context to perform the JNDI lookup.</li> <pre class="prettyprint"> initialContext = getContext(0); </pre> <li>We perform a lookup on the queue and the connection factory</li> <pre class="prettyprint"> Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue"); ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory"); </pre> <li>We create a JMS Connection, Session and a MessageConsumer on the queue</li> <pre class="prettyprint"> connection = cf.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(queue); </pre> <li>We start the connection</li> <pre class="prettyprint"> <code>connection.start();</code> </pre> <li>We receive the message. Stomp messages are mapped to JMS TextMessage.</li> <pre class="prettyprint"> TextMessage messageReceived = (TextMessage)consumer.receive(5000); System.out.println("Received JMS message: " + messageReceived.getText()); </pre> <li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li> <pre class="prettyprint"> <code>finally { if (initialContext != null) { initialContext.close(); } if (connection != null) { connection.close(); } }</code> </pre> </ol> </body> </html>