Reputation: 41220
I was wondering how to test JMS using ActiveMQ based on the answer suggesting using ActiveMQ to do the test Simulating JMS - jUnit
However, I am not getting the message I was expecting from the MessageConsumer. If I used receive it just hangs there.
Here is my code
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Assert;
import org.junit.Test;
public class JmsTest {
@Test
public void test() throws Exception {
final ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"vm://localhost?broker.persistent=true");
final Queue queue;
final Connection connection = connectionFactory.createConnection();
final Session session = connection.createSession(true,
Session.AUTO_ACKNOWLEDGE);
{
queue = session.createQueue("test");
}
{
final MessageProducer producer = session.createProducer(queue);
final TextMessage message = session.createTextMessage("testing");
producer.send(message);
}
{
final MessageConsumer consumer = session.createConsumer(queue);
final TextMessage message = (TextMessage) consumer.receiveNoWait();
// "message" is null at this point
Assert.assertEquals("testing", message.getText());
}
}
}
Upvotes: 1
Views: 14557
Reputation: 11
Using Topics would look something like this:
final ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
final javax.jms.Connection connection = connectionFactory.createConnection();
connection.setClientID("12345");
connection.start();
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Topic temporaryTopic = session.createTemporaryTopic();
final MessageConsumer consumer1 = session.createConsumer(temporaryTopic);
final MessageProducer producer = session.createProducer(temporaryTopic);
producer.send(session.createTextMessage("Testmessage"));
final TextMessage message = (TextMessage)consumer1.receiveNoWait();
Assert.assertNotNull(message);
Assert.assertEquals("testing", message.getText());
Upvotes: 1
Reputation: 41220
Thanks Tim, we found it at about the same time, but I had to make a few other modifications as I have specified in my comment. In a nutshell, I have to make sure the "transactional" attribute is "false" and as Tim had noted I had to do a connection.start() call.
@Test
public void test() throws Exception {
final ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"vm://localhost?broker.persistent=false");
final Connection connection = connectionFactory.createConnection();
connection.start();
final Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
final Queue queue = session.createTemporaryQueue();
{
final MessageProducer producer = session.createProducer(queue);
final TextMessage message = session.createTextMessage("testing");
producer.send(message);
}
{
final MessageConsumer consumer = session.createConsumer(queue);
final TextMessage message = (TextMessage) consumer.receiveNoWait();
Assert.assertNotNull(message);
Assert.assertEquals("testing", message.getText());
}
}
Upvotes: 5
Reputation: 18356
Before you can receive a message you need to call connection.start() otherwise no messages will be dispatched to the consumer. Also your code should not assume that receiveNoWait will always return a message, since it can take a small amount of time for a message to be dequeued and routed to the consumer, so attempting a retry there or using the timed receive() called might be appropriate.
Upvotes: 3