RabbitMQ - Negatively acknowledge a message on a queue in Java

Create a Dynamic Web Project in Eclipse. Let's say the name of the project is rabbitmq. Download amqp-client-<version>.jar and javax.jms-api-<version>.jar and add the JAR files to your build path in Eclipse.


In the left panel of Eclipse, at the name of your project (rabbitmq in this example) > Java Resources > src, create a new package. For the sake of this tutorial, let's name the package com.rabbitmq.demo.


In the package, create a class called consumer.


In consumer.java, add the following markup. In this example, the messages on foo.queue will be negatively acknowledge.

package com.rabbitmq.demo;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

public class producer {
  public void amqConsumer() throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException {		
    ConnectionFactory cf = new ConnectionFactory();

    // use amqp if the server is not using SSL (e.g. http)

    // use amqps if the server is using SSL (e.g. https)

    Connection conn = null;
    try {
      System.out.println("Trying to connect to " + conn);

      conn = cf.newConnection();

      if (conn != null) {    
        System.out.println("Successfully connected to " + conn);
	  else {
	    System.out.println("Failed to connect to " + conn);   		  

      Channel channel = conn.createChannel();
      String queue    = "foo.queue";
      boolean autoAck = false;

	  channel.basicConsume(queue, autoAck, "a-consumer-tag",
	    new DefaultConsumer(channel) {
	      public void handleDelivery(String consumerTag,
	                                 Envelope envelope,
	                                 AMQP.BasicProperties properties,
	                                 byte[] body)
	              throws IOException
	                long deliveryTag = envelope.getDeliveryTag();
                    channel.basicNack(deliveryTag, false, autoAck);
	                System.out.println("Negatively acknowledged message on " + queue);
    catch (Exception e) {


In the left panel of Eclipse, right click on the name of your project (rabbitmq in this example) and select New JSP File. Select the WebContent folder, give the file a name such as index.jsp, and select Finish.


Add the following to line 1 of the JSP file. This is the combination of the package (com.rabbitmq.main) and the class (consumer.java).

<%@page import="com.rabbitmq.main.consumer"%>


Add the following inside the <body> tags.

  • In this example, "consumer" must be used because the name of the class in consumer.java is consumer.
  • In this example, "amqConsumer" must be used because the name of the method in consumer.java is amqConsumer.
  consumer jms = new consumer();


Select the green play button in Eclipse should consume the messages on foo.queue.

Likewise, the main RabbitMQ log file should have something like this.

accepting AMQP connection <0.7993.1338> ( ->

connection <0.7993.1338> ( -> user 'john.doe' authenticated and granted access to vhost 'foo'

closing AMQP connection <0.21250.1339> ( ->, vhost: 'foo', user: 'john.doe')


Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 639e8 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |