List of usage examples for com.rabbitmq.client Delivery getBody
public byte[] getBody()
From source file:org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.java
License:Apache License
/** * If this is a non-POISON non-null delivery simply return it. * If this is POISON we are in shutdown mode, throw * shutdown. If delivery is null, we may be in shutdown mode. Check and see. * @throws InterruptedException//ww w . j av a 2 s .c om */ private Message handle(Delivery delivery) throws InterruptedException { if ((delivery == null && shutdown != null)) { throw shutdown; } if (delivery == null) { return null; } byte[] body = delivery.getBody(); Envelope envelope = delivery.getEnvelope(); MessageProperties messageProperties = this.messagePropertiesConverter .toMessageProperties(delivery.getProperties(), envelope, "UTF-8"); messageProperties.setMessageCount(0); Message message = new Message(body, messageProperties); if (logger.isDebugEnabled()) { logger.debug("Received message: " + message); } deliveryTags.add(messageProperties.getDeliveryTag()); return message; }
From source file:org.zenoss.amqp.impl.ConsumerImpl.java
License:Open Source License
@SuppressWarnings("unchecked") private Message<T> createMessage(Delivery delivery) throws AmqpException { final T body; final byte[] rawBody; final MessageProperties properties = new BasicPropertiesWrapper(delivery.getProperties()); final MessageEnvelope envelope = new EnvelopeWrapper(delivery.getEnvelope()); if ("deflate".equalsIgnoreCase(properties.getContentEncoding())) { try {//w w w .ja v a2s .co m rawBody = deflateDecompress(delivery.getBody()); } catch (IOException e) { logger.warn("Failed to decompress message", e); // Throw MessageDecoderException so we don't loop attempting to read invalid message throw new MessageDecoderException( DefaultMessage.newMessage(delivery.getBody(), properties, envelope), e); } } else { rawBody = delivery.getBody(); } if (converter == null) { body = (T) rawBody; } else { try { body = converter.fromBytes(rawBody, properties); } catch (Exception e) { /* Throw exception with original received message on failure */ throw new MessageDecoderException(DefaultMessage.newMessage(rawBody, properties, envelope), e); } /* Throw exception if we failed to convert the message */ if (body == null) { throw new MessageDecoderException(DefaultMessage.newMessage(rawBody, properties, envelope)); } } return DefaultMessage.newMessage(body, properties, envelope); }
From source file:pl.nask.hsn2.DataStoreActiveCleaner.java
License:Open Source License
/** * Main loop. Listens for JobFinished and JobFinishedReminder and if one appears it starts job removing tasks. */// w w w .jav a2s. c o m private void listenAndClean() { try { QueueingConsumer consumer = initRabbitMqConnection(); LOGGER.info("Waiting for messages..."); while (true) { // Listen for message. Delivery delivery = consumer.nextDelivery(); String type = delivery.getProperties().getType(); LOGGER.debug("Got delivery {}", type); // Clean if job finished data. try { if ("JobFinished".equals(type)) { JobFinished jobFinishedData = JobFinished.parseFrom(delivery.getBody()); startJobDataRemoving(jobFinishedData.getJob(), jobFinishedData.getStatus()); } else if ("JobFinishedReminder".equals(type)) { JobFinishedReminder jobFinishedData = JobFinishedReminder.parseFrom(delivery.getBody()); startJobDataRemoving(jobFinishedData.getJob(), jobFinishedData.getStatus()); } } catch (InvalidProtocolBufferException e) { LOGGER.warn("Invalid message! Expected: " + type, e); } } } catch (ShutdownSignalException e) { LOGGER.warn("Shutdown signal received.", e); } catch (ConsumerCancelledException e) { LOGGER.info("Cancell signal received.", e); } catch (InterruptedException e) { LOGGER.error("Interrupted.", e); } catch (IOException e) { LOGGER.error("Connection issue.", e); } }
From source file:pl.nask.hsn2.unicorn.connector.ConnectorImpl.java
License:Open Source License
public Response receive() throws ConnectionException { try {//ww w. j a v a2 s.c om Delivery delivery = consumer.nextDelivery(); BasicProperties properties = delivery.getProperties(); return new Response(properties.getType(), properties.getContentType(), delivery.getBody()); } catch (ShutdownSignalException e) { throw new ConnectionException("Receiving error!", e); } catch (InterruptedException e) { throw new ConnectionException("Receiving error!", e); } }