我想知道如何根据建议使用 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());
}
}
}