How to send and receive STOMP message in JBoss 7.2
Tech-Today

How to send and receive STOMP message in JBoss 7.2


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;
}

}




- How To Setup Stomp In Jboss As 7 And Create A Java Client Call
This tutorial will teach us how we can enable stomp in JBoss 7.1.3 and create a sample java client call.  What you need: 1.) JBoss AS7.1.3 2.) standalone-full.xml - this is where we will copy the settings  Setup JBoss AS 7 1.) 1.) Add extension:...

- How To Configure Mail Resource In Jboss 7.2 With A Sample Java Client
This tutorial assumes that you have the following installed on your local system: JBoss 7.2eclipse to run a projecta mail server, can be gmailIn this page we will summarize how to configure a mail dataSource in Jboss and create a JavaEE6 client to send...

- How To Receive A Synchronous Jms Message
This tutorial requires that you have glassfish installed and followed the following previous 2 tutorials: http://czetsuya-tech.blogspot.com/2012/06/how-to-setup-set-jms-factory-and-queue.html http://czetsuya-tech.blogspot.com/2012/06/how-to-send-jms-message-to-jms-queue.html...

- How To Send Jms Message To A Jms Queue
This tutorial requires that you have completed my previous tutorial on how to setup jms broker on glassfish: http://czetsuya-tech.blogspot.com/2012/06/how-to-setup-set-jms-factory-and-queue.html From the previous tutorial we will use the ff values: queue...

- Create A Primary Key Column With Sequence Generated Value In Hibernate
For example you have a class User: package org.kalidad.seamexercises.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.SequenceGenerator; ...



Tech-Today








.