0 Replies Latest reply: Mar 15, 2007 1:38 AM by 566396 RSS

    Mysterious problem in JMS

    566396
      Hi,

      When I send a message to JMS queue and then try receiving it, I don't receive the message. However, if I send and receive messages in a multi-threaded program, it succeeds. Could anyone tell me how to fix this problem?
      Also, after sending the message to JMS queue and before receiving it, the server console shows the count of messages received as 0.

      Following is the program in which i try to send the message and receive it,through sepearate methods:

      import java.util.Hashtable;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.jms.*;

      public class SampleQueue {

      public static void main(String a[]) throws Exception {
           SampleQueue s=new SampleQueue();
           s.send();
      s.receive();
      }


      public void send() throws NamingException, JMSException {
      Hashtable environment=new Hashtable();
      environment.put(Context.INITIAL_CONTEXT_FACTORY,"oracle.j2ee.rmi.RMIInitialContextFactory");
      environment.put(Context.PROVIDER_URL,"ormi://localhost/");
      environment.put(Context.SECURITY_PRINCIPAL,"oc4jadmin");
      environment.put(Context.SECURITY_CREDENTIALS,"welcome1");

      Context context=new InitialContext(environment);
      QueueConnectionFactory queueConnectionFactory=
      (QueueConnectionFactory)context.lookup("jms/QueueConnectionFactory");
      QueueConnection queueConnection=queueConnectionFactory.createQueueConnection();
      queueConnection.start();
      Queue queue=(Queue)context.lookup("jms/demoQueue");

      QueueSession queueSession=
      queueConnection.createQueueSession(true,Session.AUTO_ACKNOWLEDGE);

      QueueSender queueSender=
      queueSession.createSender(queue);

      Message message=queueSession.createTextMessage("this is my message");

      queueSender.send(message);
      queueSender.send(message);

      queueSender.close();
      queueSession.close();
      queueConnection.close();

      System.out.println("sent"+message);
      }

      public void receive() throws NamingException, JMSException {
      Hashtable environment=new Hashtable();
      environment.put(Context.INITIAL_CONTEXT_FACTORY,"oracle.j2ee.rmi.RMIInitialContextFactory");

      environment.put(Context.PROVIDER_URL,"ormi://localhost/");
      environment.put(Context.SECURITY_PRINCIPAL,"oc4jadmin");
      environment.put(Context.SECURITY_CREDENTIALS,"welcome1");

      Context context=new InitialContext(environment);

      QueueConnectionFactory queueConnectionFactory=
      (QueueConnectionFactory)context.lookup("jms/QueueConnectionFactory");

      QueueConnection queueConnection=queueConnectionFactory.createQueueConnection();
      queueConnection.start();
      Queue queue=(Queue)context.lookup("jms/demoQueue");

      QueueSession queueSession=
      queueConnection.createQueueSession(true,Session.AUTO_ACKNOWLEDGE);

      QueueReceiver queueReceiver=
      queueSession.createReceiver(queue);


      Message message=queueReceiver.receiveNoWait();
      System.out.println("received:"+message);

      queueReceiver.close();
      queueSession.close();
      queueConnection.close();

      System.out.println("received");

      }
      }


      Output:
      sentTextMessage[ID:Oc4jJMS.Message.pc-cgnw2a066-in.6202078c:11154475972:-8000.5]
      received:null






      This is the multithreaded version of the above program, which works properly


      public class SampleQueue {

      public static QueueReceiver _queueReceiver;


      public static void main(String a[] ) throws Exception{
      Hashtable environment=new Hashtable();
      environment.put(Context.INITIAL_CONTEXT_FACTORY,"oracle.j2ee.rmi.RMIInitialContextFactory");
      environment.put(Context.PROVIDER_URL,"ormi://localhost/");
      environment.put(Context.SECURITY_PRINCIPAL,"oc4jadmin");
      environment.put(Context.SECURITY_CREDENTIALS,"welcome1");
      Context context=new InitialContext(environment);
      QueueConnectionFactory queueConnectionFactory=
      (QueueConnectionFactory)context.lookup("jms/QueueConnectionFactory");
      QueueConnection queueConnection=queueConnectionFactory.createQueueConnection();
      queueConnection.start();
      Queue queue=(Queue)context.lookup("jms/demoQueue");
      QueueSession queueSession=
      queueConnection.createQueueSession(true,Session.AUTO_ACKNOWLEDGE);
      QueueSender queueSender=
      queueSession.createSender(queue);
      Message message=queueSession.createTextMessage("this is my message");


      QueueReceiver queueReceiver=
      queueSession.createReceiver(queue);
      _queueReceiver = queueReceiver;

      Thread t=(new Thread("rcv"){public void run(){

              try{
              Message m= getQueueReceiver().receive();
              System.out.println("Recieved:"+m.toString());
              }catch (Exception e){}

      }});
      t.start();


      queueSender.send(message);
      System.out.println("sent"+message.toString());

      t.join();
      queueReceiver.close();
      queueSender.close();
      queueSession.close();
      queueConnection.close();
      }

      public static QueueReceiver getQueueReceiver(){
      return _queueReceiver;
      }

      }

      output:
      sentTextMessage[ID:Oc4jJMS.Message.pc-cgnw2a066-in.-56827ba1:1115445cf67:-8000.5]
      RecievedTextMessage[ID:Oc4jJMS.Message.pc-cgnw2a066-in.-56827ba1:1115445cf67:-8000.5]

      Could anyone tell me what couldbe the problem? Have I missed any initial configuration to be done?(Actually I did not find any initial configuration to be done for JMS).

      Thank you for the time spent. Any suggestions would be highly appreciated.