示例#1
0
  private void consumeMessages() {
    while (true) {
      QueueingConsumer.Delivery delivery;

      try {
        delivery = consumer.nextDelivery();

        final String message = new String(delivery.getBody());

        runOnUiThread(
            new Runnable() {
              public void run() {
                Toast.makeText(ReceiveActivity.this, message, Toast.LENGTH_LONG).show();
              }
            });

        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

      } catch (ShutdownSignalException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      } catch (ConsumerCancelledException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
  /** {@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;
  }