0 Replies Latest reply on Jun 25, 2004 5:57 AM by akhilanand

    Problem in deploying a MDB with javax.jms.Topic

    akhilanand Newbie

      hi,
      am trying to create a topic to which a number of subscribers can listen but am facing a problem says JMS provider failure detected.
      what actually am doing is i developed a message driven bean that listens to the same topic and will inform to all clients listening on the topic with a simple text message.
      what client does is sends a text message requests and have its own listener which listens to the reply.
      am printing the message(the arguement of onMessage() method) at client side, am getting that printed as soon as am sending the request but unable to catch the same at server side when am printing the same thing.
      am embedding the code also

      My MDB:
      ------------
      package mymdbtopic;

      import javax.ejb.MessageDrivenContext;
      import javax.ejb.MessageDrivenBean;
      import javax.jms.Message;
      import javax.jms.TopicConnectionFactory;
      import javax.jms.TopicConnection;
      import javax.jms.TopicSession;
      import javax.jms.Topic;
      import javax.jms.TemporaryTopic;
      import javax.jms.TopicPublisher;
      import javax.jms.MessageListener;
      import javax.jms.TextMessage;
      import javax.jms.MapMessage;
      import javax.naming.NamingException;
      import javax.naming.InitialContext;
      import javax.naming.Context;

      import java.io.BufferedReader;
      import java.io.FileInputStream;
      import java.io.File;
      import java.io.InputStreamReader;
      import java.util.Properties;

      public class MyMessageBean implements MessageDrivenBean, MessageListener
      {
      MessageDrivenContext context=null;
      public void setMessageDrivenContext(MessageDrivenContext ctx)
      {
      context=ctx;
      }
      public void ejbCreate(){}
      public void ejbRemove()
      {
      context=null;
      }

      public void onMessage(Message msg)
      {
      System.out.println("listened");
      try
      {
      System.out.println("akhil MSbean : "+msg.getJMSMessageID());
      //Portion to capture TextMessage
      TextMessage text=(TextMessage)msg;
      System.out.println("akhil Message : "+text.getText());
      Context ctx=new InitialContext();
      TopicConnectionFactory factory=(TopicConnectionFactory)ctx.lookup("TopicConnectionFactory");
      TopicConnection connection=(TopicConnection)factory.createTopicConnection();
      TopicSession session=(TopicSession)connection.createTopicSession(true, TopicSession.AUTO_ACKNOWLEDGE);
      Topic tq=(Topic)ctx.lookup("myTopic");
      TopicPublisher publisher = session.createPublisher(tq);
      connection.start();
      TextMessage text1=session.createTextMessage();
      text1.setText("Your message reached the Message Driven Bean");
      publisher.publish(text1);
      connection.close();
      }catch(Exception e)
      {
      e.printStackTrace();
      }
      }

      public Context getContext()
      {
      try
      {
      Properties properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
      return new InitialContext(properties);
      }catch(Exception e)
      {
      e.printStackTrace();
      }
      return null;
      }
      }

      -----------------
      My Client
      -----------------
      import javax.jms.TopicConnectionFactory;
      import javax.jms.TopicConnection;
      import javax.jms.TopicSession;
      import javax.jms.Topic;
      import javax.jms.TemporaryTopic;
      import javax.jms.TopicPublisher;
      import javax.jms.TopicSubscriber;
      import javax.jms.TextMessage;
      import javax.jms.MapMessage;
      import javax.jms.MessageListener;
      import javax.jms.Message;

      import java.util.Properties;
      import java.io.File;
      import java.io.FileInputStream;

      import javax.naming.InitialContext;
      import javax.naming.Context;

      public class MyClient implements MessageListener
      {
      private TopicConnection connection=null;
      public void onMessage(Message msg)
      {
      try
      {
      System.out.println("client messae : "+msg);
      connection.close();
      }catch(Exception e)
      {
      e.printStackTrace();
      }
      }

      public void send()
      {
      try
      {
      Properties properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
      properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
      Context ctx = new InitialContext(properties);
      TopicConnectionFactory factory=(TopicConnectionFactory)ctx.lookup("ConnectionFactory");
      Topic topic=(Topic)ctx.lookup("myTopic");
      connection=(TopicConnection)factory.createTopicConnection();
      TopicSession session=(TopicSession)connection.createTopicSession(true,TopicSession.AUTO_ACKNOWLEDGE);
      TemporaryTopic ttopic=(TemporaryTopic)session.createTemporaryTopic();
      TopicPublisher publisher=session.createPublisher(ttopic);
      TopicSubscriber subscriber=session.createSubscriber(ttopic);
      subscriber.setMessageListener(this);
      //Portion to send a TextMessage
      TextMessage msg=session.createTextMessage();
      msg.setJMSReplyTo(ttopic);
      msg.setText("This is Akhil from CGG");
      connection.start();
      publisher.publish(msg);
      session.commit();
      }catch(Exception e)
      {
      e.printStackTrace();
      }
      }

      public static void main(String args[])
      {
      new MyClient().send();
      }
      }
      --------------
      My ejb-jar.xml
      --------------
      ?xml version="1.0" encoding="UTF-8"?>
      <!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>
      My first Message Bean
      <display-name>My Message Bean</display-name>
      <enterprise-beans>
      <message-driven>
      <ejb-name>MessageBeanTopic</ejb-name>
      <ejb-class>mymdbtopic.MyMessageBean</ejb-class>
      <transaction-type>Container</transaction-type>
      <message-driven-destination>
      <destination-type>javax.jms.Topic</destination-type>
      <subscription-durability>Durable</subscription-durability>
      </message-driven-destination>
      </message-driven>
      </enterprise-beans>
      </ejb-jar>

      ---------
      My jboss.xml
      ----------
      <?xml version="1.0"?>

      <enterprise-beans>
      <message-driven>
      <ejb-name>MessageBeanTopic</ejb-name>
      <configuration-bean>My Message Driven Bean</configuration-bean>
      <destination-jndi-name>myTopic</destination-jndi-name>
      </message-driven>
      </enterprise-beans>



      So please reply with a solution
      with thanks in advance
      Akhil