示例#1
0
  /**
   * Emits the next message from the queue as a tuple.
   *
   * <p>Serialization schemes returning null will immediately ack and then emit unanchored on the
   * {@link #ERROR_STREAM_NAME} stream for further handling by the consumer.
   *
   * <p>If no message is ready to emit, this will wait a short time ({@link #WAIT_FOR_NEXT_MESSAGE})
   * for one to arrive on the queue, to avoid a tight loop in the spout worker.
   */
  @Override
  public void nextTuple() {
    if (spoutActive && amqpConsumer != null) {
      try {
        final QueueingConsumer.Delivery delivery = amqpConsumer.nextDelivery(WAIT_FOR_NEXT_MESSAGE);

        if (delivery == null) return;
        final long deliveryTag = delivery.getEnvelope().getDeliveryTag();
        final byte[] message = delivery.getBody();

        /////////////////// new rpc
        BasicProperties props = delivery.getProperties();
        BasicProperties replyProps =
            new BasicProperties.Builder().correlationId(props.getCorrelationId()).build();
        //////////////////////////////
        List<Object> deserializedMessage = serialisationScheme.deserialize(message);
        if (deserializedMessage != null && deserializedMessage.size() > 0) {
          // let's see what's inside the Object List (checking)
          /*System.out.println("Lenght of the list : "+ deserializedMessage.size() +"\n");
          for (int i =0; i< deserializedMessage.size(); i++){
          	Object obj=deserializedMessage.get(i);
          	System.out.println("Object value: "+  obj + "\n" );
          }*/
          ArrayList li = new ArrayList();
          li.add(deserializedMessage.get(0));
          li.add(props);
          li.add(replyProps);
          deserializedMessage = li;
          collector.emit(deserializedMessage, deliveryTag);
          /////////// new for AMQPSpout RPC+JSON
          try {
            String response = "your name was: " + deserializedMessage.get(0) + " ";
            this.amqpChannel.basicPublish(
                "", props.getReplyTo(), replyProps, response.getBytes("UTF-8"));
            // this.amqpChannel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            this.amqpChannel.basicAck(deliveryTag, false);
          }
          /// publishjson(props, replyProps, delivery.detEnvelope().getDeliveryTag());
          // try{
          //	publishjson(props, replyProps);
          // }
          catch (IOException e) {
            log.error("Error when publishing to the response queue", e);
          }
          ////////////////
        } else {
          handleMalformedDelivery(deliveryTag, message);
        }
      } catch (ShutdownSignalException e) {
        log.warn("AMQP connection dropped, will attempt to reconnect...");
        Utils.sleep(WAIT_AFTER_SHUTDOWN_SIGNAL);
        reconnect();
      } catch (InterruptedException e) {
        // interrupted while waiting for message, big deal
      }
    }
  }
示例#2
0
 public void nextTuple() {
   if (amqpConsumer != null) {
     try {
       final QueueingConsumer.Delivery delivery = amqpConsumer.nextDelivery(WAIT_FOR_NEXT_MESSAGE);
       if (delivery == null) return;
       final long deliveryTag = delivery.getEnvelope().getDeliveryTag();
       final byte[] message = delivery.getBody();
       log.debug("Sending message:" + new String(message));
       log.debug("Getting next tuple - " + (messageCount++));
       if (messageCount < 0) {
         log.debug("You just get a number of messages = max value of long! Wow! Reseting to 0");
         this.messageCount = 0;
       }
       collector.emit(serialisationScheme.deserialize(message), deliveryTag);
     } catch (InterruptedException e) {
       // interrupted while waiting for message, big deal
     }
   }
 }
示例#3
0
 /**
  * Declares the output fields of this spout according to the provided {@link
  * backtype.storm.spout.Scheme}.
  *
  * <p>Additionally declares an error stream (see {@link #ERROR_STREAM_NAME} for handling malformed
  * or empty messages to avoid infinite retry loops
  */
 @Override
 public void declareOutputFields(OutputFieldsDeclarer declarer) {
   declarer.declare(serialisationScheme.getOutputFields());
   declarer.declareStream(ERROR_STREAM_NAME, new Fields("deliveryTag", "bytes", "otherbytes"));
 }
示例#4
0
 public void declareOutputFields(OutputFieldsDeclarer declarer) {
   declarer.declare(serialisationScheme.getOutputFields());
 }