13 Replies Latest reply on Jun 5, 2003 6:47 PM by dafei

    JMS Client problems

    delboypass

      Does anybody have test code for a JMS Client.

      I have followed through chapter 11 of the quickstart guide and have setup the Bean to listen to the testQueue but it doesnt seem to include any test files to send a message to the queue registered in the JNDI tree.

      I have been told that there are test clients in a testsuite but seem unable to find a specific java file example.

      Any help appreciated del.

      I know i need to find the InitialContext and then the factory
      then look up the queue as registered on the JNDI tree
      then send it a message
      but example code would be very much beneficial to my my increased learning.

        • 1. Re: JMS Client problems
          delboypass

          Ive created one now after solving my problem.
          Ill paste it here later.
          Maybe though one should be included in the beginners manual?

          • 2. Re: JMS Client problems
            mars73

            can pass me yr sample codes... i having problem with JMS and JBOSS eorking together.

            thks

            • 3. Re: JMS Client problems
              delboypass


              /*
              * JBoss, the OpenSource J2EE webOS
              *
              * Distributable under LGPL license.
              * See terms of license at gnu.org.
              *
              * Modified for MDB test client (2002-12-01)
              */
              package com.btexact.erica.billing;

              import javax.naming.*;
              import javax.rmi.PortableRemoteObject;
              import javax.jms.*;
              import java.util.*;
              import java.text.*;

              /*
              import test.interfaces.TestSession;
              import test.interfaces.TestSessionHome;
              */

              public class TestClient2 {

              public static void main(String[] args){

              System.out.println("TestClient2 started");

              QueueConnectionFactory qcf = null;
              QueueConnection qc = null;
              QueueSession qs = null;
              Queue q = null;
              QueueSender qsend = null;
              TextMessage ms = null;

              //including this to try and beat the noinitiacontextexception
              Properties h = new Properties();

              h.put(Context.INITIAL_CONTEXT_FACTORY,

              "org.jnp.interfaces.NamingContextFactory");
              h.put(Context.PROVIDER_URL, "jnp://localhost:1099");



              try {
              InitialContext iContext = new InitialContext(h);
              qcf = (QueueConnectionFactory)iContext.lookup( "ConnectionFactory" );
              q = (Queue)iContext.lookup("queue/testQueue");
              }
              catch (NamingException e) {
              System.out.println("JNDI look up failed: " + e.toString());
              System.exit(1);
              }

              try {
              qc = qcf.createQueueConnection();
              qs = qc.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
              qsend = qs.createSender(q);
              ms = qs.createTextMessage();
              DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
              Date timeStamp= new Date();
              ms.setText("MDB message from client was " + df.format(timeStamp));
              qsend.send(ms);
              }
              catch (JMSException e) {
              System.out.println("JMS exception occurrred : " + e.toString());
              }
              finally {
              if (qc != null) {
              try{
              qc.close();
              }
              catch (JMSException e) {}
              }
              }
              }

              }

              • 4. Re: JMS Client problems
                delboypass


                /*
                * JBoss, the OpenSource J2EE webOS
                *
                * Distributable under LGPL license.
                * See terms of license at gnu.org.
                *
                * Modified for MDB test client (2002-12-01)
                */


                import javax.naming.*;
                import javax.rmi.PortableRemoteObject;
                import javax.jms.*;
                import java.util.*;
                import java.text.*;

                /*
                import test.interfaces.TestSession;
                import test.interfaces.TestSessionHome;
                */

                public class TestClient2 {

                public static void main(String[] args){

                System.out.println("TestClient2 started");

                QueueConnectionFactory qcf = null;
                QueueConnection qc = null;
                QueueSession qs = null;
                Queue q = null;
                QueueSender qsend = null;
                TextMessage ms = null;

                //including this to try and beat the noinitiacontextexception
                Properties h = new Properties();

                h.put(Context.INITIAL_CONTEXT_FACTORY,

                "org.jnp.interfaces.NamingContextFactory");
                h.put(Context.PROVIDER_URL, "jnp://localhost:1099");



                try {
                InitialContext iContext = new InitialContext(h);
                qcf = (QueueConnectionFactory)iContext.lookup( "ConnectionFactory" );
                q = (Queue)iContext.lookup("queue/testQueue");
                }
                catch (NamingException e) {
                System.out.println("JNDI look up failed: " + e.toString());
                System.exit(1);
                }

                try {
                qc = qcf.createQueueConnection();
                qs = qc.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
                qsend = qs.createSender(q);
                ms = qs.createTextMessage();
                DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
                Date timeStamp= new Date();
                ms.setText("MDB message from client was " + df.format(timeStamp));
                qsend.send(ms);
                }
                catch (JMSException e) {
                System.out.println("JMS exception occurrred : " + e.toString());
                }
                finally {
                if (qc != null) {
                try{
                qc.close();
                }
                catch (JMSException e) {}
                }
                }
                }

                }

                • 5. Re: JMS Client problems
                  mars73

                  thks for yr help... i manage to send msg to queue.

                  i have another problem.. i can only get 1 msg from queue although i send 10 msg to the queue.

                  do u happen to have a receiver test client... so that i can double check my code.

                  • 6. Re: JMS Client problems
                    delboypass

                    There are a number of solutionsa that you can use to check your messages are arriving.

                    Probably the best though is to implement a message bean that makes use of the onMessage method that will do something every time a message is sent to that queue. Either that or build a receiver that either syncronously or async picks messages off the queue. If you are persisting messages, use can then use a queuebrowser to pick off hte persistent uncomsumed messages off the queue.

                    Here is some bean code although you will have to add the ejb xml and jboss xml around it.

                    import javax.ejb.MessageDrivenBean;
                    import javax.ejb.MessageDrivenContext;
                    import javax.ejb.EJBException;
                    import javax.jms.MessageListener;
                    import javax.jms.Message;
                    import javax.naming.*;
                    import javax.rmi.PortableRemoteObject;
                    import javax.jms.*;
                    import java.util.*;
                    import java.text.*;

                    public class PrintBean implements MessageDrivenBean, MessageListener {

                    public void setMessageDrivenContext (MessageDrivenContext c) throws EJBException{}
                    public void ejbCreate() {}
                    public void ejbRemove() {}

                    public void onMessage(Message message){
                    System.out.println("Got message: "+ message.toString());
                    //if(message.instanceOf(ObjectMessage)){
                    // System.out.println("got a BillEvent message delivered");
                    //}
                    //String property = message.getObjectProperty();
                    try{
                    String messagetype = message.getJMSType();
                    System.out.println(messagetype);
                    }
                    catch(Exception e){System.out.println("");}
                    }
                    }


                    • 7. Re: JMS Client problems
                      dafei

                      i would guess in order to run a JMS client remotely, i would need to run JBOSS in the full, all, mode? as the default mode wont allow remote service...i have been lucky trying out JMS within the same JVM, using the default new initialContext(). but when i try to run it remotely, i never get any luck, dont know why...

                      • 8. Re: JMS Client problems
                        dafei

                        the first line exception reads like this:

                        javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory. Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory

                        what should i put on the server side? i currently have nothing there, the client codes are the same as posted...i am new to JMS, so if you would, give me some step by step help. thank you very much in advance.

                        • 9. Re: JMS Client problems

                          Put client/jbossall-client.jar in your classpath.

                          Regards,
                          Adrian

                          • 10. Re: JMS Client problems
                            dafei

                            i dont see all-client, there is one without all, i put it in, and it doesnt work, then i see jnp-client.jar, i put that in, it seems i get the initial context...

                            thanks very much.

                            • 11. Re: JMS Client problems
                              dafei

                              now i am getting this exception:

                              javax.naming.CommunicationException. Root exception is java.rmi.UnmarshalException: error unmarshalling return; nested exception is:

                              • 12. Re: JMS Client problems
                                dafei

                                the full exception message is:

                                javax.naming.CommunicationException. Root exception is java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
                                java.lang.ClassNotFoundException: org.jboss.mq.referenceable.ObjectRefAddr (no security manager: RMI class loader disabled)
                                at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:169)
                                at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
                                at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:445)
                                at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:429)
                                at javax.naming.InitialContext.lookup(InitialContext.java:347)

                                • 13. Re: JMS Client problems
                                  dafei

                                  ok, i put almost all the client jars in and it is working now. Thank you again for the tip. have a nice evening:))