1
votes

I am trying to build a basic Producer Consumer application. I have three queues for message processing and can have multiple producers and consumers. The basic problem that I am facing here is that when should I call the

connection.start()

method of the javax.jms.QueueConnection that I am using. All the examples listed online ( eg :- https://github.com/hornetq/hornetq/blob/master/examples/jms/jmx/src/main/java/org/hornetq/jms/example/JMXExample.java) show that after we have produced a message on a destination and after we have started a consumer should we start the connection. i.e. The connection.start() is usually the last thing to do. Is it possible that I could start my connection as and when it is created ? For example, something like this

Properties jndiProps = new Properties();
jndiProps.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
jndiProps.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
jndiProps.put("java.naming.provider.url", "localhost:1099");
context = new InitialContext(jndiProps);
QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("/ConnectionFactory");
connection = factory.createQueueConnection();
connection.start();
1

1 Answers

3
votes

Are you using asynchronous consumer i.e. do you have onMessage method to receive messages?

The reason for calling connection.start after consumers have been created is that connection.start call tells the messaging provider to start message delivery. Your application must be ready to receive messages. If you are using a message listener (onMessage method), then it is recommended you create the consumer first, setup message listener and then call connection.start so that your application is ready to receive messages.

Otherwise you can call connection.start and then call consumer.receive method to receive messages synchronously.

Note if your application is producing messages and does not have a consumer, then there is no need to call connection.start.