title: How to use Ack or Nack in Spring AMQP
body: <p>I am new to Spring AMQP. I am having an application which is a producer sending messages to the other application which is a consumer.</p>
  
  <p>Once the consumer receives the message, we will do validation of the data. </p>
  
  <p><strong>If the data is proper we have to ACK and message should be removed from the Queue.
  If the data is improper we have to NACK(Negative Acknowledge) the data so that it will be re-queued in RabbitMQ</strong>.</p>
  
  <p>I came across</p>
  
  <p><code>**factory.setDefaultRequeueRejected(false);**</code>( It will not requeue the message at all)</p>
  
  <p><code>**factory.setDefaultRequeueRejected(true);**</code>( It will requeue the message when exception occurs)</p>
  
  <p>But my case i will acknowledge the message based on validation. Then it should remove the message. If NACK then requeue the message.</p>
  
  <p>I have read in RabbitMQ website</p>
  
  <p><strong>The AMQP specification defines the basic.reject method that allows clients to reject individual, delivered messages, instructing the broker to either discard them or requeue them</strong></p>
  
  <p>How to achieve the above scenario? Please provide me some examples.</p>
  
  <p>I tried a small Program</p>
  
  <pre><code>       logger.info("Job Queue Handler::::::::::" + new Date());
          try {
  
          }catch(Exception e){
  
              logger.info("Activity Object Not Found Exception so message should be Re-queued the Message::::::::::::::");
  
          }
  
          factory.setErrorHandler(new ConditionalRejectingErrorHandler(cause -&gt;{
              return cause instanceof XMLException;
          }));
  </code></pre>
  
  <p>Message is not re queuing for different exception
  <strong>factory.setDefaultRequeueRejected(true)</strong></p>
  
  <blockquote>
    <p>09:46:38,854 ERROR [stderr] (SimpleAsyncTaskExecutor-1)
    <strong>org.activiti.engine.ActivitiObjectNotFoundException</strong>: no processes deployed with key 'WF89012'</p>
    
    <p>09:46:39,102 INFO 
    [com.example.bip.rabbitmq.handler.ErrorQueueHandler]
    (SimpleAsyncTaskExecutor-1) Received from Error Queue: {ERROR=Could
    not commit JPA transaction; nested exception is
    <strong>javax.persistence.RollbackException: Transaction marked as
    rollbackOnly</strong>}</p>
  </blockquote>