How to send and receive STOMP message in Wildfly
The following code is an example of how we can send and receive a stomp message with JBoss 7.2. package org.meveo.util; import java.io.In...
https://www.czetsuyatech.com/2014/01/wildfly-send-stomp-message.html
The following code is an example of how we can send and receive a stomp message with JBoss 7.2.
package org.meveo.util; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Properties; import javax.naming.InitialContext; /** * @author Edward P. Legaspi * @since Nov 4, 2013 **/ public class StompUtil { private static final String END_OF_FRAME = "\u0000"; private static final String USERNAME = "guest"; private static final String PASSWORD = "guest"; private static String HOST = "localhost"; private static String STOMP_VERSION = "1.1"; public static void sendMessage(String destinationQueue, String message) throws Exception { sendMessage(STOMP_VERSION, HOST, destinationQueue, USERNAME, PASSWORD, message); } public static void sendMessage(String destinationQueue, String username, String password, String message) throws Exception { sendMessage(STOMP_VERSION, HOST, destinationQueue, username, password, message); } public static void sendMessage(String stompVersion, String url, String destinationQueue, String username, String password, String message) throws Exception { if (url != null && !url.isEmpty()) { HOST = url; } // Step 1. Create a TCP socket to connect to the Stomp port Socket socket = new Socket("localhost", 61613); // Step 2. Send a CONNECT frame to connect to the server String connectFrame = "CONNECT\n" + "accept-version:" + stompVersion + "\n" + "host:" + HOST + "\n" + "login:" + username + "\n" + "passcode:" + password + "\n" + "request-id:1\n" + "\n" + END_OF_FRAME; System.out.println("sending message to queue=" + destinationQueue); sendFrame(socket, connectFrame); String response = receiveFrame(socket); System.out.println("response: " + response); // Step 3. Send a SEND frame (a Stomp message) to the // jms.queue.exampleQueue address with a text body String queueMessage = "SEND\n" + "destination:" + destinationQueue + "\n" + "\n" + message + END_OF_FRAME; sendFrame(socket, queueMessage); System.out.println("Sent Stomp message: " + message); // Step 4. Send a DISCONNECT frame to disconnect from the server String disconnectFrame = "DISCONNECT\n" + "\n" + END_OF_FRAME; sendFrame(socket, disconnectFrame); // Step 5. Slose the TCP socket socket.close(); } private static void sendFrame(Socket socket, String data) throws Exception { byte[] bytes = data.getBytes("UTF-8"); OutputStream outputStream = socket.getOutputStream(); for (int i = 0; i < bytes.length; i++) { outputStream.write(bytes[i]); } outputStream.flush(); } private static String receiveFrame(Socket socket) throws Exception { InputStream inputStream = socket.getInputStream(); byte[] buffer = new byte[1024]; int size = inputStream.read(buffer); byte[] data = new byte[size]; System.arraycopy(buffer, 0, data, 0, size); String frame = new String(data, "UTF-8"); return frame; } }And below is how you would call the utility:
StompUtil.sendMessage("jms.queue.test", "hello world!");To receive the message
@Inject @TestQueue private Queue testQueue; @Inject private Connection connection; //...... Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(testQueue); connection.start(); TextMessage messageReceived = (TextMessage) consumer.receive(5000); System.out.println("Received JMS message: " + messageReceived.getText());The producer class
public class Resources { @Resource(mappedName = "java:/ConnectionFactory") private ConnectionFactory connectionFactory; @Resource(mappedName = "queue/test") @TestQueue private Queue testQueue; @Produces public Connection createConnection() throws JMSException { return connectionFactory.createConnection(); } public void closeConnection(@Disposes Connection conn) throws JMSException { conn.close(); } @Produces @TestQueue public Queue getTestQueue() { return testQueue; } }
Post a Comment