9 Replies Latest reply on May 15, 2013 1:29 AM by chandrasachin16

    Error while sending JMS(message) with jboss AS 7 (7.1.0)

    chandrasachin16 Newbie

      Hi Community,

       

      I am in the process of migrating my old application on Jboss 5 to Jboss 7 (7.1.0). I am using JMS to send message on topic which I have configured in Jboss standalone-full.xml.

       

      I am using the following code  to create context where host is localhost and port is 4447 and user1 and user123 are application users which I have created in jboss.

       

      private void sendJmsEvent() {

       

              String factoryName = "jms/RemoteConnectionFactory";

              String topicName = "jms/topic/xMESTopicSystemEventMsg";

       

                InitialContext context=getInitialContext("localhost","4447");

                  TopicConnectionFactory factory = (TopicConnectionFactory) context.lookup(factoryName);

                  Topic topic = (Topic) context.lookup(topicName);

                  context.close();

                  // Create JMS objects

       

                  TopicConnection connection = factory.createTopicConnection("user1","user123");

                  TopicSession topicSession = connection.createTopicSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);

                  TopicPublisher publisher = topicSession.createPublisher(topic);

                  // Send messages

                  System.out.println("Enter message to send or 'quit' to quit");

                  String messageText = "stop";

       

                  TextMessage message = topicSession.createTextMessage(messageText);

                  publisher.publish(message);

      }

       

      public static InitialContext getInitialContext(String host, String port) throws Exception {

              Properties props = new Properties();

              props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");

              props.put(Context.PROVIDER_URL, "remote://"+host+":"+port);

              props.put(Context.SECURITY_PRINCIPAL, "user1");

              props.put(Context.SECURITY_CREDENTIALS, "user123");

              props.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");

              props.put("jboss.naming.client.ejb.context", true);

              InitialContext initialContext = new InitialContext(props);

              return initialContext ;

          }

       

       

      This code is a part of my application deployed in jboss(in the same JVM) but I get following error----------------

       

      javax.naming.NamingException: Failed to create remoting connection [Root exception is java.lang.RuntimeException: Failed to setup EJB remote context]

         at org.jboss.naming.remote.client.ClientUtil.namingException(ClientUtil.java:36)

         at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:117)

         at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)

         at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)

         at javax.naming.InitialContext.init(Unknown Source)

         at javax.naming.InitialContext.<init>(Unknown Source)

         at com.test.TestServlet.getInitialContext(TestServlet.java:107)

         at com.test.TestServlet.sendJmsEvent(TestServlet.java:65)

         at com.test.TestServlet.doGet(TestServlet.java:49)

         at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)

         at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)

         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

         at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:154)

         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)

         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)

         at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

         at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)

         at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)

         at java.lang.Thread.run(Unknown Source)

      Caused by: java.lang.RuntimeException: Failed to setup EJB remote context

         at org.jboss.naming.remote.client.InitialContextFactory.setupEjbContext(InitialContextFactory.java:352)

         at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:111)

         ... 22 more

      Caused by: java.lang.reflect.InvocationTargetException

         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

         at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

         at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

         at java.lang.reflect.Method.invoke(Unknown Source)

         at org.jboss.naming.remote.client.InitialContextFactory.setupEjbContext(InitialContextFactory.java:340)

         ... 23 more

      Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of org/jboss/modules/ModuleClassLoader) previously initiated loading for a different type with name "org/jboss/remoting3/Connection"

         at org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver.<init>(RemotingConnectionEJBReceiver.java:99)

         at org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver.<init>(RemotingConnectionEJBReceiver.java:88)

         at org.jboss.ejb.client.EJBClientContext.registerConnection(EJBClientContext.java:340)

         at org.jboss.naming.remote.client.ejb.EjbClientContextSelector.<init>(EjbClientContextSelector.java:24)

         at org.jboss.naming.remote.client.ejb.EjbClientContextSelector.setupSelector(EjbClientContextSelector.java:14)

         ... 28 more

       

      Can anybody please tell me why this error is occuring?

       

      Regards

      Sachin

        • 1. Re: Error while sending JMS(message) with jboss AS 7 (7.1.0)
          Justin Bertram Master

          You're creating the context incorrectly.  See this for an example of how to do a JNDI lookup for JMS resources from a remote client.

          • 2. Re: Error while sending JMS(message) with jboss AS 7 (7.1.0)
            chandrasachin16 Newbie

            Thanks Justin,

             

                   I will try with the above example.

             

             

             

            Regards

            Sachin

            • 3. Re: Error while sending JMS(message) with jboss AS 7 (7.1.0)
              chandrasachin16 Newbie

              Hi Justin,

               

              Tried with the above link but still could not create the context.Here's my code below

               

              Properties props = new Properties();

                      props.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);

                      props.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL,PROVIDER_URL));

                      props.put(Context.SECURITY_PRINCIPAL, System.getProperty("username", DEFAULT_USERNAME));

                      props.put(Context.SECURITY_CREDENTIALS, System.getProperty("password", DEFAULT_PASSWORD));

                      log.info( " Host is not null - Getting context for: " + host + ":" + port);

                      InitialContext initialContext = new InitialContext(props);

               

              I get a null value in initialContext object

               

               

              I have declared class level variables

               

              private static final String DEFAULT_USERNAME = "user1";

              private static final String DEFAULT_PASSWORD = "user123";

              private static final String PROVIDER_URL = "remote://localhost:4447";

              private static final String INITIAL_CONTEXT_FACTORY="org.jboss.naming.remote.client.InitialContextFactory";

               

              What is that I am missing?

               

              Regards

              Sachin

              • 4. Re: Error while sending JMS(message) with jboss AS 7 (7.1.0)
                Stephen Coy Master

                As you're sending a message to a topic that is set up in the same server instance, your code is much simpler than you think.

                 

                Here is a sample EJB that sends a message to a javax.jms.Topic:

                 

                 

                {code:java}@Stateless

                @LocalBean

                public class Messenger {

                 

                    @Resource(mappedName = "java:/JmsXA")

                    private ConnectionFactory connectionFactory;

                 

                 

                    @Resource(mappedName = "java:jboss/exported/jms/topic/test")

                    private Topic testTopic;

                 

                 

                    public Messenger() {

                    }

                 

                 

                    public void sendMessage(String message) {       

                        try {

                            Connection connection = connectionFactory.createConnection();

                            try {

                                Session session = connection.createSession(true, 0);

                                MessageProducer producer = session.createProducer(testTopic);

                                Message jmsMessage = session.createObjectMessage(message);

                                producer.send(jmsMessage);

                            } finally {

                                connection.close();

                            }

                        } catch (JMSException e) {

                            throw new EJBException("Failed to send JMS message", e);

                        }

                       

                    }

                 

                 

                }{code}

                 

                As it is an in-server call, the setup that you do is much simpler than from an external standalone application.

                • 5. Re: Error while sending JMS(message) with jboss AS 7 (7.1.0)
                  chandrasachin16 Newbie

                  Hi Stephen ,

                   

                            Yes I am sending message on a topic, and this code is a part of my application which is deployed in the same server instance as an ear. With HornetQ I am able to send message but with remote lookup not able to do it.

                   

                  Regards

                  Sachin

                  • 6. Re: Error while sending JMS(message) with jboss AS 7 (7.1.0)
                    Stephen Coy Master

                    If it's in the same server instance it does not need to be remote...

                    • 7. Re: Error while sending JMS(message) with jboss AS 7 (7.1.0)
                      chandrasachin16 Newbie

                      Hi Stephen,

                       

                      Tried with the solution suggested by you but gives me a null pointer while creating connection   [   Connection connection = connectionFactory.createConnection() ]

                       

                      Here's my code some thing like this--

                         

                          @Resource(mappedName = "java:/JmsXA")

                          private ConnectionFactory connectionFactory;

                         

                          @Resource(mappedName = "java:jboss/exported/jms/topic/xMESTopicSystemEventMsg")

                          private Topic xMESTopicSystemEventMsg;

                       

                       

                          try {

                                     Connection connection = connectionFactory.createConnection();

                                      Session session = connection.createSession(true, 0);

                                      MessageProducer producer =null;

                                      producer = session.createProducer(xMESTopicSystemEventMsg);   

                                      Message jmsMessage = session.createObjectMessage(message);

                                      producer.send(jmsMessage);

                      }catch(JMSException e){

                      e.printStackTrace();

                      }

                       

                      I tried with Connection connection = connectionFactory.createConnection("user1","user123")  too  but didn't had any luck ( where user1 is my application user created)

                      What could be the possible cause?

                      • 8. Re: Error while sending JMS(message) with jboss AS 7 (7.1.0)
                        Stephen Coy Master

                        This code needs to be in a container managed object of some sort, such as the EJB I used above or a web component like a servlet.

                         

                        Otherwise @Resource annotated fields will not be injected for you.

                        • 9. Re: Error while sending JMS(message) with jboss AS 7 (7.1.0)
                          chandrasachin16 Newbie

                          Hi Stephen,

                           

                          This code is a part of class file inside a jar file which is again inside an ear file. I modified my code something like given below and was able to successfully send message on my configured topic

                           

                                      Context context=new InitialContext();

                                      ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("java:/JmsXA");    // used the pooled connection

                                      Destination destination = null;

                                      destination=(Destination) context.lookup("java:jboss/exported/jms/topic/xMESTopicSystemEventMsg");

                                      context.close();

                                      Connection connection = connectionFactory.createConnection("user1","user123");

                                      Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

                                      MessageProducer messageProducer = session.createProducer(destination);

                                      connection.start();

                                      TextMessage message1 = session.createTextMessage();

                                      message1.setText("sending jms message------------------------");

                                      messageProducer.send(destination,message1);

                           

                          So I think it works if I do it this way.

                           

                          Regards

                          Sachin