If I had a dollar for every time someone's stumbled on this one in JMS... http://activemq.apache.org/i-am-not-receiving-any-messages-what-is-wrong.html
2008/9/3 Eugeny N Dzhurinsky <[EMAIL PROTECTED]>: > Hello! > > I am trying to write the test for my application which uses requests and > response queues for handling message processing. I recently realized the > onMessage is never being called on the MessageConsumer. I wrote the sample > code listed below and it is really true - onMessage is just skipped. However > in the logs of ActiveMQ I can see the messages are routed and dropped to > queues? > > Can somebody please take a look at the sample unit test below and let me know > what did I miss? > > ================================================================================ > > import javax.jms.*; > > import org.apache.activemq.*; > import org.apache.log4j.*; > import org.jmock.*; > import org.junit.*; > > /** > * Client-server test case > */ > public class TestClientServer { > > private static final String IMAGES_CLIENT_QUEUE = "images_client"; > > private static final String IMAGES_SERVER_QUEUE = "images_server"; > > private static final String CONNECTION_URL = > "vm://localhost?broker.persistent=false"; > > static ActiveMQConnectionFactory factory; > > @BeforeClass > public static void beforeClass() throws Exception { > factory = new ActiveMQConnectionFactory(CONNECTION_URL); > BasicConfigurator.configure(); > Logger.getRootLogger().setLevel(Level.DEBUG); > } > > @Test > public void testClientServerCommunication() throws Exception { > ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( > CONNECTION_URL); > final ActiveMQConnection connection = (ActiveMQConnection) > connectionFactory > .createConnection(); > > // create sessions > System.out.println("Create sessions"); > final ActiveMQSession serverSession = (ActiveMQSession) connection > .createSession(false, Session.AUTO_ACKNOWLEDGE); > final ActiveMQSession clientSession = (ActiveMQSession) connection > .createSession(false, Session.AUTO_ACKNOWLEDGE); > > // create queue > System.out.println("Create destinations"); > // server queues > final Destination serverQueue = serverSession > .createQueue(IMAGES_SERVER_QUEUE); > final Destination clientQueue4Server = serverSession > .createQueue(IMAGES_CLIENT_QUEUE); > > // client queues > final Destination serverQueue4Client = clientSession > .createQueue(IMAGES_SERVER_QUEUE); > final Destination clientQueue = clientSession > .createQueue(IMAGES_CLIENT_QUEUE); > > // create producers > System.out.println("Create producers"); > final MessageProducer clientProducer = clientSession > .createProducer(serverQueue4Client); > clientProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); > > final MessageProducer serverProducer = serverSession > .createProducer(clientQueue4Server); > serverProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); > > Mockery mockery = new Mockery(); > final MessageListener listener = mockery.mock(MessageListener.class); > > mockery.checking(new Expectations() { > { > exactly(2).of(listener).onMessage(with(any(Message.class))); > } > }); > > // create consumers > System.out.println("Create consumers"); > final MessageConsumer clientConsumer = clientSession.createConsumer( > clientQueue, listener); > final MessageConsumer serverConsumer = serverSession.createConsumer( > serverQueue, listener); > > Message request = clientSession.createMessage(); > request.setJMSMessageID("REQUEST"); > clientProducer.send(request); > > Message response = serverSession.createMessage(); > response.setJMSMessageID("RESPONSE"); > serverProducer.send(response); > > Thread.sleep(5 * 1000); > mockery.assertIsSatisfied(); > } > > @AfterClass > public static void afterClass() throws Exception { > } > > } > > Thank you in advance! > -- > Eugene N Dzhurinsky > -- James ------- http://macstrac.blogspot.com/ Open Source Integration http://open.iona.com