1

我想知道如何根据建议使用 ActiveMQ 进行测试模拟 JMS - jUnit的答案使用 ActiveMQ 测试 JMS

但是,我没有从 MessageConsumer 收到我期望的消息。如果我使用接收它只是挂在那里。

这是我的代码

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());
    }
}
}
4

3 回答 3

4

谢谢蒂姆,我们几乎在同一时间找到了它,但我必须进行一些其他修改,正如我在评论中指定的那样。简而言之,我必须确保“事务”属性为“假”,正如 Tim 所说,我必须进行 connection.start() 调用。

@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());
    }
}
于 2012-05-05T21:47:14.320 回答
3

在您收到消息之前,您需要调用 connection.start() 否则不会向消费者发送任何消息。此外,您的代码不应假设 receiveNoWait 将始终返回一条消息,因为消息出列并路由到消费者可能需要很短的时间,因此尝试在那里重试或使用调用的定时接收()可能是合适的。

于 2012-05-05T20:38:00.483 回答
1

使用主题看起来像这样:

        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());
于 2016-11-24T13:51:20.690 回答