private void populateRoutingInfoHeaders(final Message message, final Envelope envelope) {
   if (envelope != null) {
     message.setHeader(RabbitMQConstants.ROUTING_KEY, envelope.getRoutingKey());
     message.setHeader(RabbitMQConstants.EXCHANGE_NAME, envelope.getExchange());
     message.setHeader(RabbitMQConstants.DELIVERY_TAG, envelope.getDeliveryTag());
   }
 }
 public MessageProperties toMessageProperties(
     final BasicProperties source, final Envelope envelope, final String charset) {
   MessageProperties target = new MessageProperties();
   Map<String, Object> headers = source.getHeaders();
   if (!CollectionUtils.isEmpty(headers)) {
     for (Map.Entry<String, Object> entry : headers.entrySet()) {
       String key = entry.getKey();
       if (MessageProperties.X_DELAY.equals(key)) {
         Object value = entry.getValue();
         if (value instanceof Integer) {
           target.setReceivedDelay((Integer) value);
         }
       } else {
         target.setHeader(key, convertLongStringIfNecessary(entry.getValue(), charset));
       }
     }
   }
   target.setTimestamp(source.getTimestamp());
   target.setMessageId(source.getMessageId());
   target.setReceivedUserId(source.getUserId());
   target.setAppId(source.getAppId());
   target.setClusterId(source.getClusterId());
   target.setType(source.getType());
   Integer deliveryMode = source.getDeliveryMode();
   if (deliveryMode != null) {
     target.setReceivedDeliveryMode(MessageDeliveryMode.fromInt(deliveryMode));
   }
   target.setDeliveryMode(null);
   target.setExpiration(source.getExpiration());
   target.setPriority(source.getPriority());
   target.setContentType(source.getContentType());
   target.setContentEncoding(source.getContentEncoding());
   String correlationId = source.getCorrelationId();
   if (StringUtils.hasText(correlationId)) {
     target.setCorrelationId(source.getCorrelationId());
   }
   String replyTo = source.getReplyTo();
   if (replyTo != null) {
     target.setReplyTo(replyTo);
   }
   if (envelope != null) {
     target.setReceivedExchange(envelope.getExchange());
     target.setReceivedRoutingKey(envelope.getRoutingKey());
     target.setRedelivered(envelope.isRedeliver());
     target.setDeliveryTag(envelope.getDeliveryTag());
   }
   return target;
 }
Exemplo n.º 3
0
  public void setRabbitExchange(
      Exchange camelExchange, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
    Message message;
    if (camelExchange.getIn() != null) {
      // Use the existing message so we keep the headers
      message = camelExchange.getIn();
    } else {
      message = new DefaultMessage();
      camelExchange.setIn(message);
    }

    if (envelope != null) {
      message.setHeader(RabbitMQConstants.ROUTING_KEY, envelope.getRoutingKey());
      message.setHeader(RabbitMQConstants.EXCHANGE_NAME, envelope.getExchange());
      message.setHeader(RabbitMQConstants.DELIVERY_TAG, envelope.getDeliveryTag());
    }

    Map<String, Object> headers = properties.getHeaders();
    if (headers != null) {
      for (Map.Entry<String, Object> entry : headers.entrySet()) {
        // Convert LongStrings to String.
        if (entry.getValue() instanceof LongString) {
          message.setHeader(entry.getKey(), entry.getValue().toString());
        } else {
          message.setHeader(entry.getKey(), entry.getValue());
        }
      }
    }

    if (hasSerializeHeader(properties)) {
      Object messageBody = null;
      try (InputStream b = new ByteArrayInputStream(body);
          ObjectInputStream o = new ObjectInputStream(b); ) {
        messageBody = o.readObject();
      } catch (IOException | ClassNotFoundException e) {
        LOG.warn("Could not deserialize the object");
      }
      if (messageBody instanceof Throwable) {
        LOG.debug("Reply was an Exception. Setting the Exception on the Exchange");
        camelExchange.setException((Throwable) messageBody);
      } else {
        message.setBody(messageBody);
      }
    } else {
      // Set the body as a byte[] and let the type converter deal with it
      message.setBody(body);
    }
  }
  public void handleDelivery(
      String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
      throws java.io.IOException {

    String reply = null;
    try {
      String message = new String(body);
      Integer idToFind = (Integer) jsonReader.read(message);
      System.out.println("Searching for: " + idToFind);
      Book book = bookstore.GetBook(idToFind);

      reply = book != null ? jsonWriter.write(book) : "ERROR: BOOK not found";
      System.out.println("JSON response: " + reply);
    } catch (Exception e) {
      reply = "ERROR: internal server error";
      System.err.println(e);
    } finally {
      BasicProperties replyProperties =
          new BasicProperties.Builder().correlationId(properties.getCorrelationId()).build();
      // you have to send the reply on the client
      // the client is blocked until this publish.
      // In rpc mode it's important send always a reply.
      getChannel().basicPublish("", properties.getReplyTo(), replyProperties, reply.getBytes());
      // The message must be removed form the queue.
      // Anyway if the sever can't remove the message the client is safe
      // with the correlation id attribute.
      getChannel().basicAck(envelope.getDeliveryTag(), false);
    }
  }