英文:
Unit test JMS 2.0 using embedded Artemis
问题
我尝试测试一个使用JMS 2.0 API消费JMS消息的应用程序。
为了降低测试的复杂性,我在测试中使用MessageProducer发送一条消息,并尝试在同一会话中使用MessageConsumer将其读取回来。
我使用的是符合JMS 2.0标准的嵌入式ActiveMQ-Artemis 2.5.0实例。
package be.attle;
import javax.jms.CompletionListener;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import org.apache.activemq.artemis.api.core.client.ActiveMQClient;
import org.apache.activemq.artemis.api.core.client.ServerLocator;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory;
import org.apache.activemq.artemis.junit.EmbeddedActiveMQResource;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
public class ActiveMQTest {
@Rule
public EmbeddedActiveMQResource resource = new EmbeddedActiveMQResource();
private Queue queue;
private ConnectionFactory factory;
private Session session;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
ServerLocator serverLocator = ActiveMQClient.createServerLocator("vm://0");
factory = new ActiveMQJMSConnectionFactory(serverLocator);
queue = ActiveMQJMSClient.createQueue(AsyncJobConst.QUEUE_NAME);
Connection conn = factory.createConnection();
session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
}
@Test
public void easyPeasy() throws JMSException {
MessageProducer producer = session.createProducer(queue);
CompletionListener listener = new CompletionListener() {
@Override
public void onException(Message message, Exception exception) {
System.err.println("message send failed:" + exception);
}
@Override
public void onCompletion(Message message) {
System.out.println("message send successfully");
}
};
producer.send(session.createTextMessage("testBody1"), listener);
producer.send(session.createTextMessage("testBody2"), listener);
// (1)
// let's try to consume the first message
MessageConsumer consumer = session.createConsumer(queue);
Message message = consumer.receiveNoWait();
Assert.assertNotNull(message);
}
}
在(1)处,控制台显示两次 "message send successfully"。然而,当我尝试从队列中读取时,断言失败(即未找到消息)。我的会话没有被事务化。
我在这里缺少了什么?
英文:
I try to test an application which consumes JMS messages using JMS 2.0 API.
To reduce the complexity of my test I send a message inside the test using a MessageProducer and try to read it back using a MessageConsumer on the same session.
I use an embedded ActiveMQ-Artemis 2.5.0 instance which is JMS 2.0 compliant.
package be.attle;
import javax.jms.CompletionListener;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import org.apache.activemq.artemis.api.core.client.ActiveMQClient;
import org.apache.activemq.artemis.api.core.client.ServerLocator;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory;
import org.apache.activemq.artemis.junit.EmbeddedActiveMQResource;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
public class ActiveMQTest {
@Rule
public EmbeddedActiveMQResource resource = new EmbeddedActiveMQResource();
private Queue queue;
private ConnectionFactory factory;
private Session session;
@Before
public void setUp() throws Exception
{
MockitoAnnotations.initMocks(this);
ServerLocator serverLocator = ActiveMQClient.createServerLocator("vm://0");
factory=new ActiveMQJMSConnectionFactory(serverLocator);
queue = ActiveMQJMSClient.createQueue(AsyncJobConst.QUEUE_NAME);
Connection conn=factory.createConnection();
session=conn.createSession(false,Session.CLIENT_ACKNOWLEDGE);
}
@Test
public void easyPeasy() throws JMSException
{
MessageProducer producer=session.createProducer(queue);
CompletionListener listener=new CompletionListener() {
@Override
public void onException(Message message, Exception exception) {
System.err.println("message send failed:"+exception);
}
@Override
public void onCompletion(Message message) {
System.out.println("message send successfully");
}
};
producer.send(session.createTextMessage("testBody1"),listener);
producer.send(session.createTextMessage("testBody2"),listener);
// (1)
// let's try the consume the first message
MessageConsumer consumer=session.createConsumer(queue);
Message message=consumer.receiveNoWait();
Assert.assertNotNull(message);
}
}
At (1) the console shows twice "message send successfully". However when I try to read from the queue, the assert fails (=no message found). My session is not transacted.
What am I missing here ?
答案1
得分: 4
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论