예제 #1
0
  public void testSingleNack() throws Exception {
    String q = channel.queueDeclare("", false, true, false, null).getQueue();

    byte[] m1 = "1".getBytes();
    byte[] m2 = "2".getBytes();

    basicPublishVolatile(m1, q);
    basicPublishVolatile(m2, q);

    long tag1 = checkDelivery(channel.basicGet(q, false), m1, false);
    long tag2 = checkDelivery(channel.basicGet(q, false), m2, false);

    QueueingConsumer c = new QueueingConsumer(secondaryChannel);
    String consumerTag = secondaryChannel.basicConsume(q, false, c);

    // requeue
    channel.basicNack(tag2, false, true);

    long tag3 = checkDelivery(c.nextDelivery(), m2, true);
    secondaryChannel.basicCancel(consumerTag);

    // no requeue
    secondaryChannel.basicNack(tag3, false, false);

    assertNull(channel.basicGet(q, false));
    channel.basicAck(tag1, false);
    channel.basicNack(tag3, false, true);

    expectError(AMQP.PRECONDITION_FAILED);
  }
예제 #2
0
  private long checkDeliveries(QueueingConsumer c, byte[]... messages) throws InterruptedException {

    Set<String> msgSet = new HashSet<String>();
    for (byte[] message : messages) {
      msgSet.add(new String(message));
    }

    long lastTag = -1;
    for (int x = 0; x < messages.length; x++) {
      QueueingConsumer.Delivery delivery = c.nextDelivery();
      String m = new String(delivery.getBody());
      assertTrue("Unexpected message", msgSet.remove(m));
      checkDelivery(delivery, m.getBytes(), true);
      lastTag = delivery.getEnvelope().getDeliveryTag();
    }

    assertTrue(msgSet.isEmpty());
    return lastTag;
  }