Technical Blog Of JackCHAN

July 27, 2010

Create Message Driven Bean (3) –JMS Topic and Topic Client

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

1. Build a TopicEJB class in MessageDrivenEJB project

Add annotation ‘@ActivationConfigProperty(propertyName = “destination”, propertyValue = “topic/weatherTopic”)’

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;

@MessageDriven(mappedName = "jms/TopicEJB", activationConfig = {
 @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
 @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
 @ActivationConfigProperty(propertyName = "clientId", propertyValue = "TopicEJB"),
 @ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/weatherTopic"),
 @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "TopicEJB") })
public class TopicEJB implements MessageListener {

 public void onMessage(Message msg) {
 try {
 System.out.println(msg.getJMSDestination());// the name of message queue

 if (msg instanceof TextMessage) { //         1.text message
 TextMessage message = (TextMessage) msg;
 System.out.println(message.getText());
 } else if (msg instanceof MapMessage) {//    2.map message
 MapMessage mapMessage = (MapMessage) msg;
 System.out.println(mapMessage.getString("hello"));
 } else if (msg instanceof ObjectMessage) {// 3. object message
 ObjectMessage objectMessage = (ObjectMessage) msg;
 System.out.println(objectMessage.getObject());
 } else if (msg instanceof BytesMessage) {//  4. byte message
 BytesMessage bytesMessage = (BytesMessage) msg;
 System.out.println(bytesMessage.getStringProperty("byte"));
 } else if (msg instanceof StreamMessage) {// 5.stream message
 StreamMessage streamMessage = (StreamMessage) msg;
 System.out.println(streamMessage.getStringProperty("stream"));
 }

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

}

2.In destinations-service.xml, add topic mbean

<?xml version="1.0" encoding="UTF-8"?>
<server>
 <mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.mq.destination:service=Queue,name=ztfQueue">
 <attribute name="JNDIName">queue/ztfQueue</attribute>
 <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
 </mbean>
 <mbean code="org.jboss.mq.server.jmx.Topic" name="jboss.mq.destination:service=Topic,name=weatherTopic">
 <attribute name="JNDIName">topic/weatherTopic</attribute>
 <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
 </mbean>
</server>

Make sure the propertyValue of destination in TopicEJB class is same of attribute of JNDIName in destinations-service.xml

3. Re-deploy the MessageDrivenEJB project into jboss5 server

4. Re-start jboss5 server

5. Create a MessageDrivenEJB class in MessageDrivenEJBClient project

import java.util.Properties;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TopicMDBClient {

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

 TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ctx
 .lookup("ConnectionFactory");
 TopicConnection topicConnection = topicConnectionFactory
 .createTopicConnection();
 TopicSession session =
 topicConnection.createTopicSession(false,
 Session.CLIENT_ACKNOWLEDGE);

 // find the topic
 Topic topic = (Topic) ctx.lookup("topic/weatherTopic");
 TopicPublisher publisher = session.createPublisher(topic);

 /**
 * There are five type message
 */

 // 1. text message
 TextMessage msg = session
 .createTextMessage("Hello, this is a text message. I got it.");
 publisher.send(msg);

 // 2. map message
 MapMessage mapMessage = session.createMapMessage();
 mapMessage.setString("hello",
 "I have seen the part of the world of Map Message");
 publisher.send(mapMessage);

 // 3. byte message
 BytesMessage bytesMessage = session.createBytesMessage();
 bytesMessage.setStringProperty("byte", "This is a byte message");
 publisher.send(bytesMessage);

 // 4. object message
 ObjectMessage objectMessage = session.createObjectMessage();
 objectMessage.setObject(new String(
 "The object can be any type of object"));
 publisher.send(objectMessage);

 // 5. stream message
 StreamMessage streamMessage = session.createStreamMessage();
 streamMessage.setStringProperty("stream",
 "stream messages usually are file and picture");
 publisher.send(streamMessage);

 } catch (JMSException e) {
 e.printStackTrace();
 } catch (NamingException e) {
 e.printStackTrace();
 }
 }

}

6.Run TopicMDBClient in MessageDrivenEJBClient project

7.Switch to jboss5 Console

12:16:40,109 INFO  [STDOUT] JBossTopic[weatherTopic]
12:16:40,109 INFO  [STDOUT] JBossTopic[weatherTopic]
12:16:40,109 INFO  [STDOUT] Hello, this is a text message. I got it.
12:16:40,109 INFO  [STDOUT] JBossTopic[weatherTopic]
12:16:40,109 INFO  [STDOUT] This is a byte message
12:16:40,109 INFO  [STDOUT] stream messages usually are file and picture
12:16:40,109 INFO  [STDOUT] JBossTopic[weatherTopic]
12:16:40,109 INFO  [STDOUT] JBossTopic[weatherTopic]
12:16:40,109 INFO  [STDOUT] I have seen the part of the world of Map Message
12:16:40,109 INFO  [STDOUT] The object can be any type of object

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

Create Message Driven Bean (1) –JMS Queue

Filed under: ejb, java, jboss, myeclipse — Tags: , , — kaisechen @ 1:43 am

1. Create a new EJB project in MyEclipse, which name is MessageDrivenEJB

chose EJB3.0, don’t tick ‘Add support for Entity beans’

2.create a new Message Driven Bean class


chose Queue Destination Type

3. The QueueEJB class will automatically implements MessageListener and it will add MessageDriven etc annotation.

add ‘@ActivationConfigProperty(propertyName = “destination”, propertyValue = “queue/ztfQueue”)’

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;

@MessageDriven(mappedName = "jms/QueueEJB", activationConfig = {
 @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
 @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/ztfQueue"),
 @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") })
public class QueueEJB implements MessageListener {

 public void onMessage(Message msg) {
 try {
 if(msg ==null){
 System.out.println("msg is null");
 return;
 }
 System.out.println(msg.getJMSDestination());// the name of message queue
 if (msg instanceof TextMessage) { //         1.text message
 TextMessage message = (TextMessage) msg;
 System.out.println(message.getText());
 } else if (msg instanceof MapMessage) {//    2.map message
 MapMessage mapMessage = (MapMessage) msg;
 if(mapMessage!=null){
 System.out.println(mapMessage.getString("hello"));
 }else{
 System.out.println(mapMessage);
 }
 } else if (msg instanceof ObjectMessage) {// 3. object message
 ObjectMessage objectMessage = (ObjectMessage) msg;
 if(objectMessage!=null){
 System.out.println(objectMessage.getObject());
 }else{
 System.out.println("objectMessage is null");
 }
 } else if (msg instanceof BytesMessage) {//  4. byte message
 BytesMessage bytesMessage = (BytesMessage) msg;
 System.out.println(bytesMessage.getStringProperty("byte"));
 } else if (msg instanceof StreamMessage) {// 5.stream message
 StreamMessage streamMessage = (StreamMessage) msg;
 System.out.println(streamMessage.getStringProperty("stream"));
 }
 } catch (JMSException e) {
 e.printStackTrace();
 }
 }

}

4.Build a destinations-service.xml under META-INF directory in MessageDrivenEJB project

destinations-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.mq.destination:service=Queue,name=ztfQueue">
<attribute name="JNDIName">queue/ztfQueue</attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
<mbean code="org.jboss.mq.server.jmx.Topic" name="jboss.mq.destination:service=Topic,name=weatherTopic">
<attribute name="JNDIName">topic/weatherTopic</attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
</server>

5.run or restart Jboss5 server

6. Deploy the project into Jboss5 server


7. The result shown in console

11:38:48,203 INFO  [EJBContainer] STARTED EJB: com.jack.ejb.QueueEJB ejbName: QueueEJB
11:38:48,531 INFO  [EJBContainer] STARTED EJB: com.jack.ejb.TopicEJB ejbName: TopicEJB
11:38:48,859 INFO  [QueueService] Queue[queue/ztfQueue] started, fullSize=200000, pageSize=2000, downCacheSize=2000
11:38:49,062 INFO  [TopicService] Topic[topic/weatherTopic] started, fullSize=200000, pageSize=2000, downCacheSize=2000

July 26, 2010

Create EJB Enitiy Bean Example In MyEclipse (2)

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

Now, we will create a JAVA project as the remote client invoking stateless session bean in EntityEJBUserManager project.

1. Create a Java project which name is ‘EntityEJBUserManagerClient’

2.import ‘EntityEJBUserManager.jar’ which exported through ‘EntityEJBUserManager’ project

3.import Jboss5 User Library which includes all jar in D:/jboss5/client/

4.import Hibernate3 User Library which includes the necessary jar of Hibernate 3.3.1

5. Create a client class

import java.util.List;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.jack.ejb.domain.User;
import com.jack.ejb.inter.UserDAO;

public class UserDAOClient {

 /**
 * @param args
 */
 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;
 UserDAO userdao;
 try {
 ctx = new InitialContext(env);
 userdao = (UserDAO) ctx.lookup("UserDAOBean/remote");

 System.out.println(".................................");
 System.out.println("Insert a user....................");
 boolean re = userdao.insert("Hello World", "Male");
 System.out.println("The result of insert a user is "+re);
 re = userdao.insert("EntityBean", "FeMale");
 System.out.println("The result of insert a user is "+re);

 System.out.println(".................................");
 System.out.println("Get a user.......................");

 User user = userdao.get(1);
 System.out.println("ID:1 and Name:"+user.getName()+" Sex:"+user.getSex());

 System.out.println(".................................");
 System.out.println("Update a user ...................");

 User user1 = userdao.get(1);
 user1.setName("UpdateEJB");
 userdao.update(user1);

 System.out.println("ID:"+user1.getId()+" and Name:"+user1.getName()+" Sex:"+user1.getSex());

 System.out.println(".................................");
 System.out.println("Get a user list..................");

 List<User> userlist = userdao.getUserList();
 for(User u:userlist){
 System.out.println("ID:"+u.getId()+" and Name:"+u.getName()+" Sex:"+u.getSex());
 }

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

}

6.run the java project and will get the result listed below

7. Check the MySQL database, and will have the result listed below

8. Check the admin console of jboss5, we can get more information

URL:http://localhost:8082/jmx-console/

Blog at WordPress.com.