Language Support

AMQP Java Client

The recommended Java client is the RabbitMQ Java client. The RabbitMQ team maintains it, and it recovers connections, channels and consumers on its own. Add com.rabbitmq:amqp-client from Maven Central to the project’s dependencies.

Publish and consume

The example reads the broker URL from BROKER_URL and falls back to a local LavinMQ at amqp://guest:guest@localhost. The Maven project setup lives in the example’s pom.xml.

Save the connection helper as src/main/java/Amqp.java:

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Amqp {
  public static Connection connect() throws Exception {
    String uri = System.getenv().getOrDefault("BROKER_URL", "amqp://guest:guest@localhost");
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri(uri);
    return factory.newConnection();
  }
}

Save the publisher as src/main/java/Publisher.java:

import static java.nio.charset.StandardCharsets.UTF_8;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

public class Publisher {
  public static void main(String[] args) throws Exception {
    try (Connection connection = Amqp.connect()) {
      publish(connection.createChannel(), "hello", "Hello world!");
    }
  }

  public static void publish(Channel channel, String queue, String message) throws Exception {
    channel.queueDeclare(queue, true, false, false, null);
    channel.confirmSelect();
    channel.basicPublish("", queue, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes(UTF_8));
    channel.waitForConfirmsOrDie(5_000);
    System.out.println(" [x] Sent '" + message + "'");
  }
}

Save the consumer as src/main/java/Worker.java:

import static java.nio.charset.StandardCharsets.UTF_8;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.function.Consumer;

public class Worker {
  public static void main(String[] args) throws Exception {
    Connection connection = Amqp.connect();
    consume(connection.createChannel(), "hello", message -> System.out.println(" [x] Received '" + message + "'"));
    System.out.println(" [*] Waiting for messages");
  }

  public static void consume(Channel channel, String queue, Consumer<String> handler) throws IOException {
    channel.queueDeclare(queue, true, false, false, null);
    channel.basicQos(10);
    channel.basicConsume(queue, false, (consumerTag, delivery) -> {
      handler.accept(new String(delivery.getBody(), UTF_8));
      channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }, consumerTag -> {});
  }
}

Run mvn compile exec:java -Dexec.mainClass=Worker in one terminal and mvn compile exec:java -Dexec.mainClass=Publisher in another.

The full, runnable example lives in java/rabbitmq-client in cloudamqp/examples, and CI tests it against LavinMQ.

Good to know

  • The worker acks manually. It calls basicAck after the handler returns. If the handler throws, the client’s default exception handler closes the channel, LavinMQ requeues the unacked message, and that consumer stops.
  • Set a prefetch limit. The client sends no prefetch limit on its own, so LavinMQ pushes messages as fast as it can. The example caps it with channel.basicQos(10).
  • Recovery is on by default. After a network failure the client reconnects every 5 seconds, then restores channels, prefetch, queues and consumers. A message can arrive twice after a reconnect, so keep handlers idempotent.
  • Use amqps:// for TLS. The client then verifies the server certificate against the JVM trust store and checks the hostname.

The Java client API guide covers channels, confirms, recovery and TLS in depth.

Other Java clients


Ready to take the next steps?

Managed LavinMQ instance via CloudAMQP

LavinMQ has been built with performance and ease of use in mind - we've benchmarked a throughput of about 1,000,000 messages/sec. You can try LavinMQ without any installation hassle by creating a free instance on CloudAMQP. Signing up is a breeze.

Get started with CloudAMQP ->

Help and feedback

We welcome your feedback and are eager to address any questions you may have about this piece or using LavinMQ. Join our Slack channel to connect with us directly. You can also find LavinMQ on GitHub.


Can’t find what you’re looking for? Let us know
Was this helpful?

Search