This example shows how to set up the jms queue and send msg to the queues in java.
1.Setup XDoclet
Download XDoclet from here, and extract it.
In Eclipse->Window->preferences, select xdoclet and set the Xdoclet home to the appropriate directory.
Create the Message Driven Bean
Create an EJB project in Eclipse.
In the J2EE perspective, right-click on the Deployment descriptor and create a new Message Driven Bean. Eclipse generates the required classes and the ejb-jar.xml file with the new MDB definition in it. Modify the Bean to look like this
public class MessagingExampleBean implements javax.ejb.MessageDrivenBean, javax.jms.MessageListener {
private javax.ejb.MessageDrivenContext messageContext = null;
public void setMessageDrivenContext(javax.ejb.MessageDrivenContext messageContext) throws javax.ejb.EJBException {
this.messageContext = messageContext;
}
public void ejbCreate() {}
public void ejbRemove() {
messageContext = null;
}
public MessagingExampleBean() {}
public void onMessage(javax.jms.Message message) {
System.out.println("Message Driven Bean got message " + message);
}
}
2. Add the following definitions to the ejb-jar.xml
MessagingExample
MessagingExampleMDB
MessagingExampleMDB
jms.MessagingExampleMdb
Bean
javax.jms.Queue
MessagingExampleMDB
Required
3. Create a new file weblogic-ejb-jar.xml. This is required for Weblogic bindings.
MessagingExampleMDB
5
5
jms/testQueue
weblogic.jndi.WLInitialContextFactory
jms/connectionFactory
20
3600
4.Create the Client. For this example, I used a servlet that simply sends a "Hello" message to the MDB through the Queue. Here is the code for it
public class MessaginClientServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public final static String JMS_FACTORY = "weblogic.examples.jms.QueueConnectionFactory";
public final static String QUEUE = "weblogic.examples.jms.exampleQueue";public MessaginClientServlet() {
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Context ctx = getInitialContext("t3://localhost:20001");
QueueConnectionFactory qconFactory;
QueueConnection connection;
QueueSession session;
QueueSender sender;
Queue queue;
TextMessage msg;
qconFactory = (QueueConnectionFactory) ctx.lookup("jms/connectionFactory");
connection = qconFactory.createQueueConnection();
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup("jms/testQueue");
msg = session.createTextMessage();
sender = session.createSender(queue);
msg.setText("Hello World");
connection.start();
sender.send(msg);
session.close();
connection.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
private InitialContext getInitialContext(String url) throws NamingException {
Hashtable<//java.sun.com/xml/ns/j2ee:string, string=""> env = new Hashtable<//java.sun.com/xml/ns/j2ee:string, string="">(); <///java.sun.com/xml/ns/j2ee:string,><///java.sun.com/xml/ns/j2ee:string,>
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url); return new InitialContext(env);
}
}
1.Setup XDoclet
Download XDoclet from here, and extract it.
In Eclipse->Window->preferences, select xdoclet and set the Xdoclet home to the appropriate directory.
Create the Message Driven Bean
Create an EJB project in Eclipse.
In the J2EE perspective, right-click on the Deployment descriptor and create a new Message Driven Bean. Eclipse generates the required classes and the ejb-jar.xml file with the new MDB definition in it. Modify the Bean to look like this
public class MessagingExampleBean implements javax.ejb.MessageDrivenBean, javax.jms.MessageListener {
private javax.ejb.MessageDrivenContext messageContext = null;
public void setMessageDrivenContext(javax.ejb.MessageDrivenContext messageContext) throws javax.ejb.EJBException {
this.messageContext = messageContext;
}
public void ejbCreate() {}
public void ejbRemove() {
messageContext = null;
}
public MessagingExampleBean() {}
public void onMessage(javax.jms.Message message) {
System.out.println("Message Driven Bean got message " + message);
}
}
2. Add the following definitions to the ejb-jar.xml
3. Create a new file weblogic-ejb-jar.xml. This is required for Weblogic bindings.
4.Create the Client. For this example, I used a servlet that simply sends a "Hello" message to the MDB through the Queue. Here is the code for it
public class MessaginClientServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public final static String JMS_FACTORY = "weblogic.examples.jms.QueueConnectionFactory";
public final static String QUEUE = "weblogic.examples.jms.exampleQueue";public MessaginClientServlet() {
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Context ctx = getInitialContext("t3://localhost:20001");
QueueConnectionFactory qconFactory;
QueueConnection connection;
QueueSession session;
QueueSender sender;
Queue queue;
TextMessage msg;
qconFactory = (QueueConnectionFactory) ctx.lookup("jms/connectionFactory");
connection = qconFactory.createQueueConnection();
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup("jms/testQueue");
msg = session.createTextMessage();
sender = session.createSender(queue);
msg.setText("Hello World");
connection.start();
sender.send(msg);
session.close();
connection.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
private InitialContext getInitialContext(String url) throws NamingException {
Hashtable<//java.sun.com/xml/ns/j2ee:string, string=""> env = new Hashtable<//java.sun.com/xml/ns/j2ee:string, string="">(); <///java.sun.com/xml/ns/j2ee:string,><///java.sun.com/xml/ns/j2ee:string,>
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url); return new InitialContext(env);
}
}
No comments:
Post a Comment