Pages

Monday, August 29, 2011

Messaging in JBoss

Share it Please

In This article, we will see how we can create a basic ejb3 and messaging applications in eclipse and deploy in JBoss 5.

In The First series we will see how we can create a basic Queue or Topic in JBoss and send some messages to them.

Creating a Basic Topic or Queue

Creating a basic Topic or Queue in JBoss is very simple. Create a file with a any name like
mess-service.xml and add the following contents to the file

<server>

<mbean code="org.jboss.jms.server.destination.QueueService" name="jboss.messaging.destination:service=Queue,name=SampleQueue" xmbean-dd="xmdesc/Queue-xmbean.xml">
<depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
<depends>jboss.messaging:service=PostOffice</depends>
</mbean>

<mbean code="org.jboss.jms.server.destination.TopicService" name="jboss.messaging.destination:service=Topic,name=MyTopic" xmbean-dd="xmdesc/Topic-xmbean.xml">
<depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
<depends>jboss.messaging:service=PostOffice</depends>
</mbean>   

</server> 
In The above lines, aim creating a Queue named ‘SampleQueue’ and Topic named ‘MyTopic’.
Just create the file in <server Profile> (all)/deploy/messaging/

Once The File is created start the server and you can see the following lines in the log file,

2011-08-26 12:12:34,441 INFO [org.jboss.jms.server.destination.QueueService] (main) Queue[/queue/SampleQueue] started, fullSize=200000, pageSize=2000, downCacheSize=2000

2011-08-26 12:12:35,109 INFO [org.jboss.jms.server.destination.TopicService] (main) Topic[/topic/MyTopic] started, fullSize=200000, pageSize=2000, downCacheSize=2000

We can check for the deployed Queus’s and Topic’s in the jmx console too ,
Open your jmx console and go for jboss.messaging.destination under the Object filter name and click it to see the Topics and Queues on the right hand side.








Basic Sample Application to access the Topic Or Queue Deployed in JBoss 5.

Once we are done with the deployment of Queue and Topic in JBoss, we will write a sample java application that access these Topic and Queues and send a message to them.

We have seen how to configure a sample Topic or Queue and deploy them in JBoss (see here).Once we confirm that the destinations are deployed correctly, we will write the sample application to access them. The code for the sample application looks like,

public class JBossMessageTopic implements MessageListener {

//Method For Sending The Message for JBossMessageTopic

public void MySample(){

String destinationName="/queue/SampleQueue";
Context content=null;
ConnectionFactory factory=null;
Connection connection=null;

try {

content=getContext();
factory=(ConnectionFactory)content.lookup("/ConnectionFactory");
Queue queue=(Queue)content.lookup(destinationName);
connection=(Connection)factory.createConnection();
Session session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
MessageProducer producer=session.createProducer(queue);
MessageConsumer consumer=session.createConsumer(queue);
consumer.setMessageListener(this);
connection.start();
TextMessage message=session.createTextMessage("This is Sample Message For JMS Destination");
producer.send(message);
connection.close();

}catch (Exception e) {

System.out.println("Exception Came At  "+e);
e.printStackTrace(); 

}

}//Constructor closed

//On Message Handler
public void onMessage(Message mess) {

TextMessage message=(TextMessage)mess;

     try {

System.out.println("Message Received \t"+message.getText());

}catch(Exception e) {
   e.printStackTrace();
}

    }//On Message Handler


public static void main(String str[]){
          new JBossMessageTopic().MySample();
}

public static javax.naming.Context getContext() throws NamingException{

Properties prop=new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
prop.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
prop.put(Context.PROVIDER_URL,"jnp://localhost:1099");

return new javax.naming.InitialContext(prop);

}//Close of GetContext() method

}//Close Of Class

Here are some important points,

1.All The Destination in JBoss are configured under the queue and topic .So if we need to access the destination from JNDI , we need to give the JNDI name as ‘/queue/SampleQueue’.

2. Since we are writing a stand alone application to access the destinations, they are set as a global resources (since our standalone application is in another JVM then the JBoss one).

3.The Connection properties will be same with most application (besides the URL that depends on your  sustem).

You can see the text sent as an Output.

No comments :

Post a Comment