junitjmsactivemq-artemis

Testing with embedded ActiveMQ Server with Vertex Broker client


I am using ActiveMQ Artemis with Vertex Broker client. I want to test below code:

public class ActiveMqMessageSender {

  private String queue = "myQueue";

  public void sendMessage(String message) throws ExecutionException, InterruptedException {
    AmqpMessage msg =
        AmqpMessage.create()
            .withBody(message)
            .build();
    client = createAmqpClient();
    var amqpConnectionFuture = client.connect();
    // Wait here to ensure that the connection is successful.
    var amqpConnection = amqpConnectionFuture.toCompletionStage().toCompletableFuture().get();
    log.info("Vert.x AMQP connection created successfully.");

    var amqpSenderFuture = amqpConnection.createSender(queue);
    // Wait here to ensure that the connection is successful.
    var amqpSender = amqpSenderFuture.toCompletionStage().toCompletableFuture().get();
    log.info("Vert.x AMQP sender created successfully.");

    amqpSender.sendWithAck(msg, ackResult -> {
      if (ackResult.succeeded()) {
        log.info("Message sent successfully.");
      } else {
        log.error("Failed to send message: " + ackResult.cause().getMessage());
      }
    });
  }

  private AmqpClient createAmqpClient() {
    var options =
        new AmqpClientOptions()
            .setHost("localhost")
            .setPort("61616")
            .setUsername("test")
            .setPassword("test");
    return AmqpClient.create(options);
  }

In my JUnit test:

@SpringBootTest
class ActiveMqMessageSenderTest {

  private String queue = "myQueue";

  @Autowired
  private ActiveMqMessageSender messageSender;
  private static EmbeddedActiveMQExtension embeddedActiveMQ;

  @BeforeAll
  public static void setUp() throws Exception {
    Configuration configuration =
        new ConfigurationImpl().setName("embedded-server")
            .setPersistenceEnabled(false)
            .setSecurityEnabled(false)
            .addAcceptorConfiguration("default", "tcp://localhost:61616")
            .addAddressSetting("#",
                new AddressSettings().setDeadLetterAddress(SimpleString.toSimpleString("dla"))
                    .setExpiryAddress(SimpleString.toSimpleString("expiry")));

    // Start the embedded ActiveMQ Artemis server
    embeddedActiveMQ = new EmbeddedActiveMQExtension(configuration);
    embeddedActiveMQ.start();
  }

  @AfterAll
  public static void tearDown() throws Exception {
    // Stop the embedded ActiveMQ Artemis server
    if (embeddedActiveMQ != null) {
      embeddedActiveMQ.stop();
    }
  }

  @Test
  void testSendMessage() throws Exception {

    String message = "Test Message";
    messageSender.sendMessage(message);

  }
}

My application.yml looks like

  artemis:
    broker:
      host: localhost
      port: 61616
      username: artemis
      password: artemis

When I run a local ActiveMQ server the code works. But when I try to run the embedded server via JUnit, I see the following in logs:

[Test worker] INFO org.apache.activemq.artemis.core.server -- AMQ221020: Started NIO Acceptor at localhost:61616 for protocols [CORE]
[Test worker] INFO org.apache.activemq.artemis.core.server -- AMQ221007: Server is now live
[Test worker] INFO org.apache.activemq.artemis.core.server -- AMQ221001: Apache ActiveMQ Artemis Message Broker version 2.31.2 [embedded-server, nodeID=9df62ceb-0c83-11ef-a6a7-00155ddd1843]

At this pont it waits:

INFO   [Thread-1 (activemq-netty-threads)] org.apache.activemq.audit.resource - AMQ601767: CORE connection a6e64464 for user unknown@127.0.0.1:54291 created

And finally I get:

WARN   [Thread-1 (ActiveMQ-server-org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl$6@72f3f14c)] o.a.activemq.artemis.core.client - AMQ212037: Connection failure to /127.0.0.1:54291 has been detected: AMQ229014: Did not receive data from /127.0.0.1:54291 within the 60000ms connection TTL. The connection will now be closed. [code=CONNECTION_TIMEDOUT]

io.vertx.core.VertxException: Disconnected
java.util.concurrent.ExecutionException: io.vertx.core.VertxException: Disconnected

Seems like the connection to the server was ok, but when I try to connect the client by calling client.connect() it doesn't work.


Solution

  • I believe the problem is that your embedded broker does not support AMQP. When the embedded broker starts you can see that it logs this:

    AMQ221020: Started NIO Acceptor at localhost:61616 for protocols [CORE]
    

    Notice, only CORE is supported. AMQP is not included.

    You need to include org.apache.activemq:artemis-amqp-protocol on your classpath. The broker will then automatically discover this and load it so that it supports AMQP 1.0 connections.