The following code listing shows a HelloWorldPublisher.java program containing the main() method, and a message-driven bean called MessageHelloBean. HelloWorldPublisher publishes the HelloWorld message on the topic hello_topic. When the message arrives on the topic, the container notifies the message-driven bean through the onMessage() method. This method then uses the WebLogic log and displays the method. Next, the ejb-jar.xml and weblogic-ejb-jar.xml files are listed. You will see that the topic on which the application publishes the message is mapped in the "destination-jndi-name" tag. package com.sams.learnweblogic7.ejb.jms; import java.rmi.RemoteException; import java.util.Properties; import javax.jms.*; import javax.ejb.*; import javax.naming.*; import javax.rmi.*; public class HelloWorldPublisher { static private String MESSAGE_TOPIC_NAME = "hello_topic"; private String messageUrl; private Context messageContext; private TopicConnection messageTopicConnection; public HelloWorldPublisher(String mesgUrl) throws NamingException{ messageUrl = msgUrl; try{ //first create the context object. Properties h = new Properties(); h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); h.put(Context.PROVIDER_URL, messageUrl); messageContext = new InitialContext(h); // Create the connection TopicConnectionFactory topicConnFactory = (TopicConnectionFactory) messageContext.lookup("weblogic.jms.ConnectionFactory"); messageTopicConnection = topicConnFactory.createTopicConnection(); messageTopicConnection.start(); } catch(Exception e) { e.printStackTrace(); } }//end of constructor public static void main(String[] args) throws Exception { System.out.println("***** Beginning of message Hello world *****"); HelloWorldPublisher msgPublisher = null; try { msgPublisher = new HelloWorldPublisher("t3://localhost:7001"); } catch (NamingException e){ e.printStackTrace(); } try { Topic myTopic = null; TopicSession mySession = null; try { mySession = messageTopicConnection.createTopicSession (false,Session.AUTO_ACKNOWLEDGE); myTopic = (Topic) messageContext.lookup(MESSAGE_TOPIC_NAME); } catch(NamingException ex) { myTopic = mySession.createTopic(MESSAGE_TOPIC_NAME); messageContext.bind(MESSAGE_TOPIC_NAME, newTopic); } TopicPublisher topicPublisher = session.createPublisher(myTopic); TextMessage helloWorldMessage = session.createTextMessage(); helloWorldMessage.setText("Hello World"); topicPublisher.publish(helloWorldMessage); } catch (Exception e) { e.printStackTrace(); } System.out.println("***** End message.Client *****"); } } The message-driven bean is as follows: package com.sams.learnweblogic7.ejb.jms; import weblogic.rmi.*; import javax.ejb.*; import javax.jms.*; import javax.naming.*; public class MessageHelloBean implements MessageDrivenBean, MessageListener { private static final boolean VERBOSE = true; private MessageDrivenContext messageContext; // Using WebLogic's log service private void log(String s) { System.out.println(s); } /** * Per the EJB specifications */ public void ejbActivate() { log("MessageHelloBean's ejbActivate called"); } /** * Per the EJB specifications */ public void ejbRemove() { log("MessageHelloBean's ejbRemove called"); } /** * Per the EJB specifications */ public void ejbPassivate() { log("MessageHelloBean's ejbPassivate called"); } /** * This method sets the session context. */ public void setMessageDrivenContext(MessageDrivenContext msgCtx) { messageContext = msgCtx; } public void ejbCreate () throws CreateException { //empty for this application } /** * Implementation of MessageListener * Retrieve the text message and display on WebLogic's log. */ public void onMessage(Message mesg) { TextMessage helloTextMsg = (TextMessage) mesg; try { String text = helloTextMsg.getText(); log(text); } catch(JMSException e) { e.printStackTrace(); } } } Here is the ejb-jar.xml file: <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd"> <ejb-jar> <enterprise-beans> <message-driven> <ejb-name>MessageHelloBean</ejb-name> <ejb-class>com.sams.learnweblogic7.ejb.jms.MessageHelloBean </ejb- class> <transaction-type>Container</transaction-type> <message-driven-destination> <destination-type>javax.jms.Topic</destination-type> </message-driven-destination> </message-driven> </enterprise-beans> <ejb-bar> The weblogic-ejb-jar.xml file is as follows: <?xml version="1.0"?> <!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc. //DTD WebLogic 7.0.0 EJB//EN" "http://www.bea.com/servers/wls700/dtd/weblogic-ejb-jar.dtd"> <weblogic-ejb-jar> <weblogic-enterprise-bean> <ejb-name>MessageHelloBean</ejb-name> <message-driven-descriptor> <pool> <max-beans-in-free-pool>200</max-beans-in-free-pool> <initial-beans-in-free-pool>20</initial-beans-in-free-pool> </pool> <destination-jndi-name>hello_topic</destination-jndi-name> </message-driven-descriptor> <jndi-name>MessageHelloBean</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar> |