Exemplo n.º 1
0
  public void doCounsumer() throws IOException, TimeoutException {
    factory.setHost("localhost");
    factory.setVirtualHost("epush");
    factory.setUsername("epush");
    factory.setPassword("epush");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    Consumer consumer =
        new DefaultConsumer(channel) {
          @Override
          public void handleDelivery(
              String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
              throws IOException {
            try {
              OtpInputStream InputStream = new OtpInputStream(body);
              OtpErlangTuple login = (OtpErlangTuple) OtpErlangTuple.decode(InputStream);

              LoginEvent loginEvent = new LoginEvent(login);

              System.out.println(loginEvent);

              // mapper.writeValueAsString(loginEvent);

            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        };

    channel.basicConsume("epush-login-queue", consumer);
  }
Exemplo n.º 2
0
  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    final Connection connection = factory.newConnection();
    final Channel channel = connection.createChannel();

    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    channel.basicQos(1);

    final Consumer consumer =
        new DefaultConsumer(channel) {
          @Override
          public void handleDelivery(
              String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
              throws IOException {
            String message = new String(body, "UTF-8");

            System.out.println(" [x] Received '" + message + "'");
            try {
              doWork(message);
            } finally {
              System.out.println(" [x] Done");
              channel.basicAck(envelope.getDeliveryTag(), false);
            }
          }
        };
    channel.basicConsume(TASK_QUEUE_NAME, false, consumer);
  }
Exemplo n.º 3
0
  private void declareConsumers() throws IOException {
    for (final EndPointMethod endPointMethod : endPointMethods) {
      final RouteSpec routeSpec = endPointMethod.getRouteIn();
      Route route = routeSpec.declareOn(channel, this);

      log.debug("consuming on " + route.getQueue() + " ==> " + endPointMethod);

      boolean autoAck = false;
      channel.basicConsume(
          route.getQueue(),
          autoAck,
          endPointMethod.toString(),
          new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(
                String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
                throws IOException {
              String routingKey = envelope.getRoutingKey();
              String contentType = properties.getContentType();
              long deliveryTag = envelope.getDeliveryTag();

              endPointMethod.call(new Event(Qarrot.this, channel, envelope, properties, body));
            }
          });
    }
  }
Exemplo n.º 4
0
  public static void t1() throws Exception {
    Channel channel = generateChannel();
    // channel.exchangeDeclare(TestAmqp.ExchangerName, "direct", true, false, null);
    // channel.exchangeDeclare(TestAmqp.FanoutExchangerName, "fanout", true, false, null);
    channel.exchangeDeclare(TestAmqp.TopicExchangerName, "topic", false, false, null);
    channel.queueDeclare(TestAmqp.TopicQueueName, false, false, false, null);
    // channel.queueBind(TestAmqp.TopicQueueName, TestAmqp.TopicExchangerName,
    // TestAmqp.RouteingName);
    // channel.queueBind(TestAmqp.TopicQueueName, TestAmqp.TopicExchangerName, "*");
    channel.queueBind(TestAmqp.TopicQueueName, TestAmqp.TopicExchangerName, "#.*");

    // channel.basicPublish(TestAmqp.ExchangerName, TestAmqp.RouteingName, null, "haha".getBytes());

    QueueingConsumer queueingConsumer = new QueueingConsumer(channel);

    channel.basicQos(1);
    channel.basicConsume(TestAmqp.TopicQueueName, queueingConsumer);

    while (true) {
      QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
      System.out.println(new String(delivery.getBody()));

      long deliveryTag = delivery.getEnvelope().getDeliveryTag();
      System.out.println("start basicAck:" + deliveryTag);
      // if (true)throw new RuntimeException("xxxxx");
      channel.basicAck(deliveryTag, false);
    }

    // channel.close();
    // connection.close();
  }
Exemplo n.º 5
0
  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("10.71.49.228");
    factory.setUsername("jinhd");
    factory.setPassword("admin");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, EXCHANGE_NAME, "");

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer =
        new DefaultConsumer(channel) {
          @Override
          public void handleDelivery(
              String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
              throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
          }
        };
    channel.basicConsume(queueName, true, consumer);
  }
Exemplo n.º 6
0
  public static void t2() throws Exception {
    Channel channel = generateChannel();
    AMQP.Queue.DeclareOk declareOk1 = channel.queueDeclare();
    AMQP.Queue.DeclareOk declareOk2 = channel.queueDeclare();
    AMQP.Queue.DeclareOk declareOk3 = channel.queueDeclare();

    String exchange = "amq.rabbitmq.log";
    channel.queueBind(declareOk1.getQueue(), exchange, "error");
    channel.queueBind(declareOk2.getQueue(), exchange, "waring");
    channel.queueBind(declareOk3.getQueue(), exchange, "info");

    QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
    channel.basicConsume(declareOk1.getQueue(), false, queueingConsumer);
    channel.basicConsume(declareOk2.getQueue(), false, queueingConsumer);
    channel.basicConsume(declareOk3.getQueue(), false, queueingConsumer);

    while (true) {
      QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
      System.out.println("log: " + new String(delivery.getBody()));
      channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
  }
Exemplo n.º 7
0
  public void run() throws IOException {
    if (!isConnected()) {
      connect();
    }

    final MessagePack msgpack = new MessagePack();

    channel.basicConsume(
        queue,
        false,
        new DefaultConsumer(channel) {
          @Override
          public void handleDelivery(
              String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
              throws IOException {
            long deliveryTag = envelope.getDeliveryTag();

            try {
              totalBytesRead.addAndGet(body.length);
              lastSecBytesReadTmp.addAndGet(body.length);

              RadioMessage msg = msgpack.read(body, RadioMessage.class);

              if (!msg.strings.containsKey("message")
                  || !msg.strings.containsKey("source")
                  || msg.timestamp <= 0) {
                LOG.error("Incomplete AMQP message. Skipping.");
                channel.basicAck(deliveryTag, false);
              }

              Message event =
                  new Message(
                      msg.strings.get("message"),
                      msg.strings.get("source"),
                      new DateTime(msg.timestamp));

              event.addStringFields(msg.strings);
              event.addLongFields(msg.longs);
              event.addDoubleFields(msg.doubles);

              processBuffer.insertCached(event, sourceInput);

            } catch (Exception e) {
              LOG.error("Error while trying to process AMQP message.", e);
            }

            channel.basicAck(deliveryTag, false);
          }
        });
  }
Exemplo n.º 8
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();
 }
Exemplo n.º 9
0
  public void open() {
    try {
      connection = createConnection();
      channel = connection.createChannel();
      if (prefetchCount > 0) {
        logger.info("setting basic.qos / prefetch count to " + prefetchCount + " for " + queueName);
        channel.basicQos(prefetchCount);
      }
      // run any declaration prior to queue consumption
      declarator.execute(channel);

      consumer = new QueueingConsumer(channel);
      consumerTag = channel.basicConsume(queueName, isAutoAcking(), consumer);
    } catch (Exception e) {
      reset();
      logger.error("could not open listener on queue " + queueName);
      reporter.reportError(e);
    }
  }
Exemplo n.º 10
0
  public static void main(String[] args) throws Exception {
    // 1.连接工厂
    ConnectionFactory factory = new ConnectionFactory();
    factory.setVirtualHost("/");
    factory.setHost("192.168.1.200");

    Connection connection;

    final Channel channel;
    // 2.创建连接
    connection = factory.newConnection();

    // 3.创建通道
    channel = connection.createChannel();

    // 4.Consumer 接口的默认实现
    DefaultConsumer defaultConsumer =
        new DefaultConsumer(channel) {
          @Override
          public void handleDelivery(
              String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
              throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received " + envelope.getRoutingKey() + " :" + message + "'");

            /*try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
            channel.basicAck(envelope.getDeliveryTag(), false);
          }
        };

    // 5.开始接收mq消息,开启监听器生
    channel.basicConsume(QUEUE_NAME, false, defaultConsumer);

    System.in.read();
    channel.close();
    connection.close();
    ;
  }
Exemplo n.º 11
0
  public static void t3() throws Exception {
    Channel channel = generateChannel();
    channel.exchangeDeclare(TestAmqp.PingExchangerName, "direct", true, false, null);
    channel.queueDeclare("rpc", false, false, false, null);
    // channel.queueBind(TestAmqp.TopicQueueName, TestAmqp.TopicExchangerName,
    // TestAmqp.RouteingName);
    // channel.queueBind(TestAmqp.TopicQueueName, TestAmqp.TopicExchangerName, "*");
    channel.queueBind("rpc", TestAmqp.PingExchangerName, "ping");

    QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
    channel.basicConsume("rpc", false, queueingConsumer);

    while (true) {
      QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
      System.out.println("log: " + new String(delivery.getBody()));
      channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

      System.out.println("reply to :" + delivery.getProperties().getReplyTo());
      channel.basicPublish("", delivery.getProperties().getReplyTo(), null, "asfasdfas".getBytes());
    }
  }
    @Override
    public void run() {
      while (true) {
        if (closed) {
          break;
        }
        try {
          connection = connectionFactory.newConnection(rabbitAddresses);
          channel = connection.createChannel();
        } catch (Exception e) {
          if (!closed) {
            logger.warn("failed to created a connection / channel", e);
          } else {
            continue;
          }
          cleanup(0, "failed to connect");
          try {
            Thread.sleep(5000);
          } catch (InterruptedException e1) {
            // ignore, if we are closing, we will exit later
          }
        }

        QueueingConsumer consumer = null;
        // define the queue
        try {
          if (rabbitQueueDeclare) {
            // only declare the queue if we should
            channel.queueDeclare(
                rabbitQueue /*queue*/,
                rabbitQueueDurable /*durable*/,
                false /*exclusive*/,
                rabbitQueueAutoDelete /*autoDelete*/,
                rabbitQueueArgs /*extra args*/);
          }
          if (rabbitExchangeDeclare) {
            // only declare the exchange if we should
            channel.exchangeDeclare(
                rabbitExchange /*exchange*/, rabbitExchangeType /*type*/, rabbitExchangeDurable);
          }

          channel.basicQos(
              rabbitQosPrefetchSize /*qos_prefetch_size*/,
              rabbitQosPrefetchCount /*qos_prefetch_count*/,
              false);

          if (rabbitQueueBind) {
            // only bind queue if we should
            channel.queueBind(
                rabbitQueue /*queue*/,
                rabbitExchange /*exchange*/,
                rabbitRoutingKey /*routingKey*/);
          }
          consumer = new QueueingConsumer(channel);
          channel.basicConsume(rabbitQueue /*queue*/, false /*noAck*/, consumer);
        } catch (Exception e) {
          if (!closed) {
            logger.warn("failed to create queue [{}]", e, rabbitQueue);
          }
          cleanup(0, "failed to create queue");
          continue;
        }

        // now use the queue to listen for messages
        while (true) {
          if (closed) {
            break;
          }
          QueueingConsumer.Delivery task;
          try {
            task = consumer.nextDelivery();
          } catch (Exception e) {
            if (!closed) {
              logger.error("failed to get next message, reconnecting...", e);
            }
            cleanup(0, "failed to get message");
            break;
          }

          if (task != null && task.getBody() != null) {
            final List<Long> deliveryTags = Lists.newArrayList();

            BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();

            try {
              processBody(task, bulkRequestBuilder);
            } catch (Exception e) {
              logger.warn(
                  "failed to parse request for delivery tag [{}], ack'ing...",
                  e,
                  task.getEnvelope().getDeliveryTag());
              try {
                channel.basicAck(task.getEnvelope().getDeliveryTag(), false);
              } catch (IOException e1) {
                logger.warn("failed to ack [{}]", e1, task.getEnvelope().getDeliveryTag());
              }
              continue;
            }

            deliveryTags.add(task.getEnvelope().getDeliveryTag());

            if (bulkRequestBuilder.numberOfActions() < bulkSize) {
              // try and spin some more of those without timeout, so we have a bigger bulk (bounded
              // by the bulk size)
              try {
                while ((task = consumer.nextDelivery(bulkTimeout.millis())) != null) {
                  try {
                    processBody(task, bulkRequestBuilder);
                    deliveryTags.add(task.getEnvelope().getDeliveryTag());
                  } catch (Throwable e) {
                    logger.warn(
                        "failed to parse request for delivery tag [{}], ack'ing...",
                        e,
                        task.getEnvelope().getDeliveryTag());
                    try {
                      channel.basicAck(task.getEnvelope().getDeliveryTag(), false);
                    } catch (Exception e1) {
                      logger.warn(
                          "failed to ack on failure [{}]", e1, task.getEnvelope().getDeliveryTag());
                    }
                  }
                  if (bulkRequestBuilder.numberOfActions() >= bulkSize) {
                    break;
                  }
                }
              } catch (InterruptedException e) {
                if (closed) {
                  break;
                }
              } catch (ShutdownSignalException sse) {
                logger.warn(
                    "Received a shutdown signal! initiatedByApplication: [{}], hard error: [{}]",
                    sse,
                    sse.isInitiatedByApplication(),
                    sse.isHardError());
                if (!closed && sse.isInitiatedByApplication()) {
                  logger.error("failed to get next message, reconnecting...", sse);
                }
                cleanup(0, "failed to get message");
                break;
              }
            }

            if (logger.isTraceEnabled()) {
              logger.trace(
                  "executing bulk with [{}] actions", bulkRequestBuilder.numberOfActions());
            }

            // if we have no bulk actions we might have processed custom commands, so ack them
            if (ordered || bulkRequestBuilder.numberOfActions() == 0) {
              try {
                if (bulkRequestBuilder.numberOfActions() > 0) {
                  BulkResponse response = bulkRequestBuilder.execute().actionGet();
                  if (response.hasFailures()) {
                    // TODO write to exception queue?
                    logger.warn("failed to execute: " + response.buildFailureMessage());
                  }
                }
              } catch (Exception e) {
                logger.warn("failed to execute bulk", e);
              }
              for (Long deliveryTag : deliveryTags) {
                try {
                  channel.basicAck(deliveryTag, false);
                } catch (Exception e1) {
                  logger.warn("failed to ack [{}]", e1, deliveryTag);
                }
              }
            } else {
              if (bulkRequestBuilder.numberOfActions() > 0) {
                bulkRequestBuilder.execute(
                    new ActionListener<BulkResponse>() {
                      @Override
                      public void onResponse(BulkResponse response) {
                        if (response.hasFailures()) {
                          // TODO write to exception queue?
                          logger.warn("failed to execute: " + response.buildFailureMessage());
                        }
                        for (Long deliveryTag : deliveryTags) {
                          try {
                            channel.basicAck(deliveryTag, false);
                          } catch (Exception e1) {
                            logger.warn("failed to ack [{}]", e1, deliveryTag);
                          }
                        }
                      }

                      @Override
                      public void onFailure(Throwable e) {
                        logger.warn(
                            "failed to execute bulk for delivery tags [{}], not ack'ing",
                            e,
                            deliveryTags);
                      }
                    });
              }
            }
          }
        }
      }
      cleanup(0, "closing river");
    }
Exemplo n.º 13
0
 /**
  * Registers a consumer on the reply queue.
  *
  * @throws IOException if an error is encountered
  * @return the newly created and registered consumer
  */
 protected QueueingConsumer setupConsumer() throws IOException {
   QueueingConsumer consumer = new QueueingConsumer(_channel);
   _channel.basicConsume(_queueName, consumer);
   return consumer;
 }