Пример #1
0
 public void stop() throws IOException {
   if (consumer != null && channel != null) {
     channel.basicCancel(consumer.getConsumerTag());
     channel.removeShutdownListener(this);
     channel = config.closeChannel(channel);
   }
 }
Пример #2
0
  public void StopConsumer() {

    try {
      channel.basicCancel(consumerTag);
      channel.close();
      connection.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Пример #3
0
 public void testDeclarationOfManyAutoDeleteQueuesWithTransientConsumer()
     throws IOException, TimeoutException {
   Channel ch = connection.createChannel();
   assertRecordedQueues(connection, 0);
   for (int i = 0; i < 5000; i++) {
     String q = UUID.randomUUID().toString();
     ch.queueDeclare(q, false, false, true, null);
     QueueingConsumer dummy = new QueueingConsumer(ch);
     String tag = ch.basicConsume(q, true, dummy);
     ch.basicCancel(tag);
   }
   assertRecordedQueues(connection, 0);
   ch.close();
 }
Пример #4
0
 public void close() {
   try {
     if (channel != null && channel.isOpen()) {
       if (consumerTag != null) channel.basicCancel(consumerTag);
       channel.close();
     }
   } catch (Exception e) {
     logger.debug("error closing channel and/or cancelling consumer", e);
   }
   try {
     logger.info("closing connection to rabbitmq: " + connection);
     connection.close();
   } catch (Exception e) {
     logger.debug("error closing connection", e);
   }
   consumer = null;
   consumerTag = null;
   channel = null;
   connection = null;
 }
Пример #5
0
  public void close() {
    try {
      if (amqpChannel != null) {
        if (amqpConsumerTag != null) {
          amqpChannel.basicCancel(amqpConsumerTag);
        }

        amqpChannel.close();
      }
    } catch (IOException e) {
      log.warn("Error closing AMQP channel", e);
    }

    try {
      if (amqpConnection != null) {
        amqpConnection.close();
      }
    } catch (IOException e) {
      log.warn("Error closing AMQP connection", e);
    }
  }
  /** {@inheritDoc} */
  @Override
  public SampleResult sample(Entry entry) {
    SampleResult result = new SampleResult();
    result.setSampleLabel(getName());
    result.setSuccessful(false);
    result.setResponseCode("500");

    QueueingConsumer consumer;
    String consumerTag;

    trace("AMQPConsumer.sample()");

    try {
      initChannel();

      consumer = new QueueingConsumer(channel);
      channel.basicQos(1); // TODO: make prefetchCount configurable?
      consumerTag = channel.basicConsume(getQueue(), autoAck(), consumer);
    } catch (IOException ex) {
      log.error("Failed to initialize channel", ex);
      return result;
    }

    result.setSampleLabel(getTitle());
    /*
     * Perform the sampling
     */
    result.sampleStart(); // Start timing
    try {
      QueueingConsumer.Delivery delivery = consumer.nextDelivery(getReceiveTimeoutAsInt());

      if (delivery == null) {
        log.warn("nextDelivery timed out");
        return result;
      }

      /*
       * Set up the sample result details
       */
      result.setSamplerData(new String(delivery.getBody()));

      result.setResponseData("OK", null);
      result.setDataType(SampleResult.TEXT);

      result.setResponseCodeOK();
      result.setResponseMessage("OK");
      result.setSuccessful(true);

      if (!autoAck()) channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

    } catch (ShutdownSignalException e) {
      log.warn("AMQP consumer failed to consume", e);
      result.setResponseCode("400");
      result.setResponseMessage(e.toString());
      interrupt();
    } catch (ConsumerCancelledException e) {
      log.warn("AMQP consumer failed to consume", e);
      result.setResponseCode("300");
      result.setResponseMessage(e.toString());
      interrupt();
    } catch (InterruptedException e) {
      log.info("interuppted while attempting to consume");
      result.setResponseCode("200");
      result.setResponseMessage(e.toString());
    } catch (IOException e) {
      log.warn("AMQP consumer failed to consume", e);
      result.setResponseCode("100");
      result.setResponseMessage(e.toString());
    } finally {
      try {
        channel.basicCancel(consumerTag);
      } catch (IOException e) {
        log.error("Couldn't safely cancel the sample's consumer", e);
      }
    }

    result.sampleEnd(); // End timimg
    trace("AMQPConsumer.sample ended");

    return result;
  }