Technical Blog Of JackCHAN

July 27, 2010

Create Message Driven Bean (2) –JMS Queue Client

Filed under: ejb, java, jboss, myeclipse — Tags: , , , — kaisechen @ 2:04 am

1. Create a Java project in MyEclipse, which name is MessageDrivenEJBClient

2.Import Jboss5 User Library in this project

3.Build a client class

import java.util.Properties;

import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class QueueMDBClient {

 public static void main(String[] args) {
 Properties env = new Properties();
 env.put("java.naming.factory.initial",
 "org.jnp.interfaces.NamingContextFactory");
 env.put("java.naming.factory.url.pkgs",
 "org.jboss.naming:org.jnp.interfaces");
 env.put("java.naming.provider.url", "localhost");

 InitialContext ctx;
 try {
 ctx = new InitialContext(env);

 QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
 QueueConnection conn = factory.createQueueConnection();

 QueueSession session = conn.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
 // find the destination
 Destination destination  = (Destination) ctx.lookup("queue/ztfQueue");
 MessageProducer producer = session.createProducer(destination);

 /**
 * There are five type message
 */
 // 1. text message (Yes)
 TextMessage msg = session
 .createTextMessage("Hello, this is a text message. I got it.");
 producer.send(msg);
 // 2. map message (No)
 MapMessage mapMessage = session.createMapMessage();
 mapMessage.setString("hello", "I have seen the part of the world of Map Message");
 producer.send(mapMessage);
 // 3. byte message (No)
 BytesMessage bytesMessage = session.createBytesMessage();
 bytesMessage.setStringProperty("byte", "This is a byte message");
 producer.send(bytesMessage);
 // 4. object message (No)
 ObjectMessage objectMessage = session.createObjectMessage();
 objectMessage.setObject(new String("The object can be any type of object"));
 producer.send(objectMessage);
 // 5. stream message (OK)
 StreamMessage streamMessage = session.createStreamMessage();
 streamMessage.setStringProperty("stream", "stream messages usually are file and picture");
 producer.send(streamMessage);
 producer.close();
 //EJB 2.1 rule
 Queue queue = (Queue)ctx.lookup("queue/ztfQueue");
 QueueSender sender = session.createSender(queue);
 msg = session.createTextMessage("Now, we use EJB 2.1 rule to send message.Hello, this is a text message.");
 sender.send(msg);
 sender.send(mapMessage);
 sender.send(bytesMessage);
 sender.send(objectMessage);
 sender.send(streamMessage);
 sender.close();
 //close session
 session.close();
 //close connection
 conn.close();
 } catch (JMSException e) {
 e.printStackTrace();
 } catch (NamingException e) {
 e.printStackTrace();
 }
 }
}

4. Run the java project

5. Swith to jboss5 Server Console in MyEclipse

We can see that the Queue MDB receives variety messages and prints them to console.

1:55:02,390 INFO  [STDOUT] JBossQueue[ztfQueue]
11:55:02,390 INFO  [STDOUT] JBossQueue[ztfQueue]
11:55:02,390 INFO  [STDOUT] JBossQueue[ztfQueue]
11:55:02,390 INFO  [STDOUT] JBossQueue[ztfQueue]
11:55:02,390 INFO  [STDOUT] JBossQueue[ztfQueue]
11:55:02,390 INFO  [STDOUT] Now, we use EJB 2.1 rule to send message.Hello, this is a text message.
11:55:02,390 INFO  [STDOUT] JBossQueue[ztfQueue]
11:55:02,406 INFO  [STDOUT] JBossQueue[ztfQueue]
11:55:02,406 INFO  [STDOUT] This is a byte message
11:55:02,406 INFO  [STDOUT] JBossQueue[ztfQueue]
11:55:02,406 INFO  [STDOUT] JBossQueue[ztfQueue]
11:55:02,406 INFO  [STDOUT] This is a byte message
11:55:02,406 INFO  [STDOUT] JBossQueue[ztfQueue]
11:55:02,406 INFO  [STDOUT] I have seen the part of the world of Map Message
11:55:02,406 INFO  [STDOUT] Hello, this is a text message. I got it.
11:55:02,406 INFO  [STDOUT] stream messages usually are file and picture
11:55:02,406 INFO  [STDOUT] stream messages usually are file and picture
11:55:02,421 INFO  [STDOUT] The object can be any type of object
11:55:02,421 INFO  [STDOUT] The object can be any type of object
11:55:02,421 INFO  [STDOUT] I have seen the part of the world of Map Message

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.