3 Replies Latest reply: May 3, 2012 10:20 AM by Marcio B. RSS

    Consumer JMS in JBoss 7.1.1-Final

    Marcio B. Newbie

      I am using JBoss 7.1.1-Final with JMS.

       

      When sending messages to the queue, all right.

       

      But when I try to consume the messages, using the MessageListener Interface, the method onMessage() is not being invoked and the consumer finish your execution without throwing any exception.

       

      I have User and password configured in JBoss and security is setting as true for send and consume.

       

      Is this some setting in JBoss? Because if you use that same code, just by changing the configuration of the JNDI provider for ActiveMQ, works 100%.

       

      In Jboss log I have this:

      16:18:06,127 INFO  [org.jboss.as.naming] (Remoting "mb-pc" task-3) JBAS011806: Channel end notification received, closing channel Channel ID 45d35aa7 (inbound) of Remoting connection 02b28e3f to /127.0.0.1:52089

        • 1. Re: Consumer JMS in JBoss 7.1.1-Final
          Marcio B. Newbie

          I solved the problem mentioned above by changing the object QueueReceiver by a MensageConsumer.

           

          But a new problem arose, it consumes the first message and then have to restart JBoss for him to consume the next message. Why is it that he is not consuming all messages without restarting JBoss?

           

          When I try consumer the 2nd message in the same instance of JBoss, I get a null.

          • 2. Re: Consumer JMS in JBoss 7.1.1-Final
            Jeff Mesnil Master

            whatd does your code look like? which acknowledgement mode do you use?

            • 3. Re: Consumer JMS in JBoss 7.1.1-Final
              Marcio B. Newbie

              import javax.jms.*;

              import javax.naming.Context;

              import javax.naming.InitialContext;

              import javax.naming.NamingException;

              import java.util.Properties;

               

              public class JMSConsumer implements MessageListener {

               

               

                  private InitialContext initialContext;

               

               

                  private Connection connection;

               

               

                  private MessageConsumer consumer;

               

               

                  public void receiver() {

                      initialContext = getInitialContext();

                      try {

                          ConnectionFactory connectionFactory =

                                  (ConnectionFactory) initialContext.lookup("jms/RemoteConnectionFactory");

               

               

                          connection = connectionFactory.createConnection("user", "pass");

               

               

                          connection.start();

               

               

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

               

               

                          Destination destination = (Destination) initialContext.lookup("jms/queue/test");

               

               

                          consumer = session.createConsumer(destination);

               

               

                          consumer.setMessageListener(this);

                      } catch (JMSException e) {

                          e.printStackTrace();

                      } catch (NamingException e) {

                          e.printStackTrace();

                      }

                  }

               

               

                  @Override

                  public void onMessage(Message message) {

                      try {

                          if (message instanceof TextMessage) {

                              TextMessage textMessage = (TextMessage) message;

                              System.out.println(textMessage.getText());

                          }

               

               

                          if (message instanceof ObjectMessage) {

                              ObjectMessage objectMessage = (ObjectMessage) message;

                              System.out.println(objectMessage.getObject());

                          }

                      } catch (JMSException e) {

                          e.printStackTrace();

                      }

               

               

                      try {

                          Thread.sleep(1500);

                      } catch (InterruptedException e) {

                          e.printStackTrace();

                      }

                  }

               

               

                  private InitialContext getInitialContext() {

                      Properties properties = new Properties();

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

                      properties.put(Context.PROVIDER_URL, "remote://localhost:4447");

                      properties.put(Context.SECURITY_PRINCIPAL, "user");

                      properties.put(Context.SECURITY_CREDENTIALS, "pass");

                      InitialContext context = null;

                      try {

                          context = new InitialContext(properties);

                      } catch (NamingException e) {

                          e.printStackTrace();

                      }

                      return context;

                  }

               

               

                  public static void main(String[] args) {

                      new JMSConsumer().receiver();

                  }

              }