private void processBody(QueueingConsumer.Delivery task, BulkRequestBuilder bulkRequestBuilder)
        throws Exception {
      if (null == task) return;
      byte[] body = task.getBody();
      if (body == null) return;

      // check for custom commands, which can be specified in the task header
      String customCommand = null;
      Map<String, Object> headers = task.getProperties().getHeaders();
      if (null != headers) {
        Object headerVal = headers.get("X-ES-Command");
        if (null != headerVal) customCommand = headerVal.toString();
      }

      if (null == customCommand || customCommand.isEmpty()) {
        // first, the "full bulk" script
        if (bulkScript != null) {
          String bodyStr = new String(body);
          bulkScript.setNextVar("body", bodyStr);
          String newBodyStr = (String) bulkScript.run();
          if (newBodyStr == null) return;
          body = newBodyStr.getBytes();
        }

        // second, the "doc per doc" script
        if (script != null) {
          processBodyPerLine(body, bulkRequestBuilder);
        } else {
          bulkRequestBuilder.add(body, 0, body.length, false);
        }
      } else {
        // handle the custom command
        handleCustomCommand(customCommand, task);
      }
    }
示例#2
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());
    }
  }
 /** Private API - Process a single request. Called from mainloop(). */
 public void processRequest(QueueingConsumer.Delivery request) throws IOException {
   AMQP.BasicProperties requestProperties = request.getProperties();
   String correlationId = requestProperties.getCorrelationId();
   String replyTo = requestProperties.getReplyTo();
   if (correlationId != null && replyTo != null) {
     AMQP.BasicProperties replyProperties =
         new AMQP.BasicProperties.Builder().correlationId(correlationId).build();
     byte[] replyBody = handleCall(request, replyProperties);
     _channel.basicPublish("", replyTo, replyProperties, replyBody);
   } else {
     handleCast(request);
   }
 }
 /** Lowest-level handler method. Calls handleCast(AMQP.BasicProperties,byte[]). */
 public void handleCast(QueueingConsumer.Delivery request) {
   handleCast(request.getProperties(), request.getBody());
 }
 /**
  * Lowest-level response method. Calls
  * handleCall(AMQP.BasicProperties,byte[],AMQP.BasicProperties).
  */
 public byte[] handleCall(
     QueueingConsumer.Delivery request, AMQP.BasicProperties replyProperties) {
   return handleCall(request.getProperties(), request.getBody(), replyProperties);
 }