-
1. Re: Consumer JMS in JBoss 7.1.1-Final
romarcio Mar 27, 2012 6:13 PM (in response to romarcio)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
jmesnil May 3, 2012 4:20 AM (in response to romarcio)whatd does your code look like? which acknowledgement mode do you use?
-
3. Re: Consumer JMS in JBoss 7.1.1-Final
romarcio May 3, 2012 10:20 AM (in response to jmesnil)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();
}
}