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