Required JAR files to be included:• GLASSFISHV2_HOME/lib/appserv-admin.jar
• GLASSFISHV2_HOME/lib/appserv-rt.jar
• GLASSFISHV2_HOME/lib/javaee.jar
• GLASSFISHV2_HOME/install/applications/jmsra/imqjmsra.jar
New Enterprise Project Creation:File >> New Project >> Enterprise >> Enterprise Application >> Enter the name of the application. (Ventura is my Project Name) >> Click on Finish
It creates three modules named:
1- Ventura
2- Ventura-ear
3- Ventura-war
Starting the GlasshFish Application Server:1- Go the Services Tab
2- Navigate to Servers > GlassFish V2 > Right Click > Start
When the server gets started, right click on the GlassFishv2 >> View Admin Console
Browser is launched>> Enter the user name [admin], and password [adminadmin] default
setting.
Creating connection factory & Message Destination1- Navigate to Resources >> JMS Resources >> Connection Factories >> New >>
Enter the JNDI Name (jms/NewConnectionFactory)
Resource Type : Select javax.jms.ConnectionFactory
Leave all else as default setting. Click on OK (ConnectionFactory is created.)
2- Navigate to the Resources >> Destination Resources >> New >>
Enter the JNDI name (jms/topic)
Resource Type : Select javax.jms.Topic
1- New Destination
2- Destination Name = jms/topic
3- Physical Destination name (newtopic)
4- Source Type = javax.jms.Topic
Leave all else as default setting. Click on OK (New Destination Resource is created)
Creating Message Driven Bean1- Create package eg. mdb
2- Create a Message Driven Bean Enter the MDB name (mine is LogBean.java)
3- Set Project Destination javax.jms.Topic
4- Click on OK
You Get the following automatically generated MDB code (definition to onMessage(Message message) method is given by me.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mdb;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
/**
*
* @author Chandra
*/
@MessageDriven(mappedName = "jms/topic", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
@ActivationConfigProperty(propertyName = "clientId", propertyValue = "TopicBeanBean"),
@ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "TopicBeanBean")
})
public class TopicBeanBean implements MessageListener {
public TopicBeanBean() {
}
public void onMessage(Message message) {
if(message instanceof MapMessage){
MapMessage mapMessage = (MapMessage)message;
try {
String name = mapMessage.getString("name");
String address = mapMessage.getString("address");
String contact_no = mapMessage.getString("contact_no");
System.out.println("Message Received : Name= " + name + " Address= " + address + " Contact No= " + contact_no);
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}
}
}
Creating the Client for Topic generator1- Create a new class named LogClient.java for example
2- Inside the main method put the following code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mdb;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
*
* @author Chandra
*/
public class TopicClient {
public static void main(String[] args) {
try {
Context ctx = new InitialContext();
ConnectionFactory factory = (ConnectionFactory)ctx.lookup("jms/NewMessageFactory");
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) ctx.lookup("jms/topic");
MessageProducer producer = session.createProducer(topic);
MapMessage mapMessage = session.createMapMessage();
mapMessage.setString("name", "Chandra");
mapMessage.setString("address", "Bangalore");
mapMessage.setString("contact_no", "123456789");
producer.send(mapMessage);
producer.close();
connection.close();
} catch (NamingException ne) {
ne.printStackTrace();
} catch(JMSException jmse){
jmse.printStackTrace();
}
}
}
Building & Running:Now you are ready to deploy and test your Message Driven Bean:
Right Click on Ventura >> Clean & Build >> UnDeploy & Deploy >> Run
When the application is Up & Running you can run the TopicClient.
See the output in the Server's log file.
Message Received : Name= Chandra Address= Bangalore Contact No= 12345678
Enjoy!!!