使用嵌入式Artemis单元测试JMS 2.0

huangapple go评论66阅读模式
英文:

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

调用连接上的 start() 方法是启动连接传送传入消息所必需的。您可以在此处找到一个简单的示例。

英文:

A call to start() on the connection is required to start the connection's delivery of incoming messages. You can find a simple example here.

huangapple
  • 本文由 发表于 2020年8月18日 18:32:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63466793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定