The next code is an instance of how nosotros tin post away post too have a stomp message amongst 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 November 4, 2013 **/ world degree StompUtil { mortal static lastly String END_OF_FRAME = "\u0000"; mortal static lastly String USERNAME = "guest"; mortal static lastly String PASSWORD = "guest"; mortal static String HOST = "localhost"; mortal static String STOMP_VERSION = "1.1"; world static void sendMessage(String destinationQueue, String message) throws Exception { sendMessage(STOMP_VERSION, HOST, destinationQueue, USERNAME, PASSWORD, message); } world static void sendMessage(String destinationQueue, String username, String password, String message) throws Exception { sendMessage(STOMP_VERSION, HOST, destinationQueue, username, password, message); } world static void sendMessage(String stompVersion, String url, String destinationQueue, String username, String password, String message) throws Exception { if (url != aught && !url.isEmpty()) { HOST = url; } // Step 1. Create a TCP socket to connect to the Stomp port Socket socket = novel 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 reply = receiveFrame(socket); System.out.println("response: " + response); // Step 3. Send a SEND frame (a Stomp message) to the // jms.queue.exampleQueue address amongst a text torso 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(); } mortal 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(); } mortal static String receiveFrame(Socket socket) throws Exception { InputStream inputStream = socket.getInputStream(); byte[] buffer = novel byte[1024]; int size = inputStream.read(buffer); byte[] information = novel byte[size]; System.arraycopy(buffer, 0, data, 0, size); String frame = novel String(data, "UTF-8"); provide frame; } }
And below is how you lot would telephone telephone the utility:
StompUtil.sendMessage("jms.queue.test", "hello world!");
To have the message
@Inject @TestQueue mortal Queue testQueue; @Inject mortal 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 degree
public degree Resources { @Resource(mappedName = "java:/ConnectionFactory") mortal ConnectionFactory connectionFactory; @Resource(mappedName = "queue/test") @TestQueue mortal Queue testQueue; @Produces world Connection createConnection() throws JMSException { provide connectionFactory.createConnection(); } world void closeConnection(@Disposes Connection conn) throws JMSException { conn.close(); } @Produces @TestQueue world Queue getTestQueue() { provide testQueue; } }
0 komentar:
Please comment if there are any that need to be asked.