You must not rely on receiveNoWait to always return a message. What the
consumer side, it should actively poll the broker ... which is probably
> please run following test:
>
> *package com.conceptwave.servicedesigner;
>
> import java.util.Hashtable;
>
> import javax.jms.JMSException;
> import javax.jms.Message;
> import javax.jms.Queue;
> import javax.jms.QueueConnection;
> import javax.jms.QueueConnectionFactory;
> import javax.jms.QueueReceiver;
> import javax.jms.QueueSender;
> import javax.jms.QueueSession;
> import javax.jms.Session;
> import javax.naming.Context;
> import javax.naming.InitialContext;
> import javax.naming.NamingException;
>
> import junit.framework.Assert;
>
> import org.junit.Before;
> import org.junit.Test;
>
> public class test {
> /**
> * put a test message to queue before test case run.
> */
> @Before
> public void setup() {
> InitialContext ctx = null;
> QueueConnection con = null;
> QueueSession queueSession = null;
> QueueSender sender = null;
> try {
> Hashtable env = new Hashtable();
> env = new Hashtable();
> env.put(Context.INITIAL_CONTEXT_FACTORY,
>
> "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
> env.put(Context.PROVIDER_URL,
> "tcp://localhost:61616");
> ctx = new InitialContext(env);
> QueueConnectionFactory fac =
> (QueueConnectionFactory) ctx
> .lookup("ConnectionFactory");
> con = fac.createQueueConnection();
> queueSession = con.createQueueSession(false,
> Session.AUTO_ACKNOWLEDGE);
> con.start();
> sender = queueSession.createSender((Queue) ctx
> .lookup("dynamicQueues/queue1"));
> sender.send(queueSession.createTextMessage("test
> message"));
> } catch (Exception e) {
> e.printStackTrace();
> } finally {
> try {
> sender.close();
> queueSession.close();
> con.close();
> ctx.close();
> } catch (JMSException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (NamingException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
> }
>
> /**
> * the receiver must sleep enough time after it is created in
> order to
> * receive the message in the queue.
> */
> @Test
> public void test1() {
> InitialContext ctx = null;
> QueueConnection con = null;
> QueueSession queueSession = null;
> QueueReceiver receiver = null;
> try {
> Hashtable env = new Hashtable();
> env = new Hashtable();
> env.put(Context.INITIAL_CONTEXT_FACTORY,
>
> "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
> env.put(Context.PROVIDER_URL,
> "tcp://localhost:61616");
> ctx = new InitialContext(env);
> QueueConnectionFactory fac =
> (QueueConnectionFactory) ctx
> .lookup("ConnectionFactory");
> con = fac.createQueueConnection();
> queueSession = con.createQueueSession(false,
> Session.AUTO_ACKNOWLEDGE);
> con.start();
> receiver = queueSession.createReceiver((Queue) ctx
> .lookup("dynamicQueues/queue1"));
>
> // in order to receive the message from queue,
> must sleep enough
> // time
> Thread.sleep(500);
> // the message can be received successfully after
> sleep.
> Message result = receiver.receiveNoWait();
> Assert.assertNotNull(result);
> } catch (Exception e) {
> e.printStackTrace();
> } finally {
> try {
> receiver.close();
> queueSession.close();
> con.close();
> ctx.close();
> } catch (JMSException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (NamingException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
> }
>
> /**
> * the receiver never get the message IMMEDIATELY after it is
> created.
> *
> * @throws Exception
> * to junit.
> */
> @Test
> public void test2() throws Exception {
> // sleep enough time to make sure the test message is
> really put into
> // the queue.
> Thread.sleep(5000);
>
> InitialContext ctx = null;
> QueueConnection con = null;
> QueueSession queueSession = null;
> QueueReceiver receiver = null;
> try {
> Hashtable env = new Hashtable();
> env = new Hashtable();
> env.put(Context.INITIAL_CONTEXT_FACTORY,
>
> "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
> env.put(Context.PROVIDER_URL,
> "tcp://localhost:61616");
>
> // repeatedly try to receive message, but you
> NEVER get it!!
> for (int i = 0; i < 100; i++) {
> ctx = new InitialContext(env);
> QueueConnectionFactory fac =
> (QueueConnectionFactory) ctx
>
> .lookup("ConnectionFactory");
> con = fac.createQueueConnection();
> queueSession =
> con.createQueueSession(false,
> Session.AUTO_ACKNOWLEDGE);
> con.start();
> receiver =
> queueSession.createReceiver((Queue) ctx
>
> .lookup("dynamicQueues/queue1"));
> // no sleep after the receiver is created
> // Thread.sleep(500);
> Message result = receiver.receiveNoWait();
> // you CAN NOT get the message in the
> queue.
> Assert.assertNull(result);
> }
> } catch (Exception e) {
> e.printStackTrace();
> } finally {
> try {
> receiver.close();
> queueSession.close();
> con.close();
> ctx.close();
> } catch (JMSException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (NamingException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
> }
>
> }*
>
>
>
>
> --
> View this message in context:
>
http://activemq.2283324.n4.nabble.com/standard-jms-code-can-not-receive-message-from-queue-tp4671868p4671901.html> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>