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

7 Comments »

  1. I want to say hello to everyone

    Comment by oppomoEdund — August 4, 2010 @ 12:53 pm

  2. hiiiiiiiiiiiiiiiiiiiiii

    Comment by سعد الكبير — August 4, 2010 @ 2:07 pm

  3. hi

    Comment by قلب عربي — August 4, 2010 @ 10:42 pm

  4. I am very pleased to have registered with the community and I’m looking forward to meeting together with people inside here eventually

    Comment by AnepleKneenry — August 11, 2010 @ 5:50 pm

  5. Hello everyone! A while before I have written my first website and I want to ask you to make a feedback of my work. I don’t know what I had to thinking about my site, so I asked if You can expend a few minutes, please give an objective thinkings about my website. I will be really thankful for your thinkings and opinions. So I promote you to write some commentsand have a good time on my website.

    odtwarzacze mp4

    Ovislink AirLive Live-FSH5PS v3 ] Switch 5 portw 10/100Mbps

    Comment by Intorsstymn — August 19, 2010 @ 1:40 am

  6. t’s such a tickety-boo site. fanciful, acutely stimulating!!!

    ———–

    Gry
    Gry Ekonomiczne
    Smieszne Gry

    Comment by Soonduddy — August 21, 2010 @ 8:29 am

  7. Evedry time i see you, i can’t understand the meaning

    Comment by Emoryodombju — August 21, 2010 @ 5:10 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.