List of usage examples for org.springframework.amqp.core Message getBody
public byte[] getBody()
From source file:com.ushahidi.swiftriver.core.dropqueue.DropHandlerTest.java
@Test public void onMessage() throws JsonParseException, JsonMappingException, IOException { Message mockMessage = mock(Message.class); MessageProperties mockMessageProperties = mock(MessageProperties.class); Channel mockChannel = mock(Channel.class); String body = "{\"identity_orig_id\": \"http://feeds.bbci.co.uk/news/rss.xml\", \"droplet_raw\": \"The danger of growing resistance to antibiotics should be treated as seriously as the threat of terrorism, England's chief medical officer says.\", \"droplet_orig_id\": \"c558d88a44fc70da36d04746574e05e4\", \"droplet_locale\": \"en-gb\", \"identity_username\": \"http://www.bbc.co.uk/news/#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa\", \"droplet_date_pub\": \"Mon, 11 Mar 2013 07:32:59 +0000\", \"droplet_type\": \"original\", \"identity_avatar\": \"http://news.bbcimg.co.uk/nol/shared/img/bbc_news_120x60.gif\", \"droplet_title\": \"Antibiotic resistance 'threat to UK'\", \"links\": [{\"url\": \"http://www.bbc.co.uk/news/health-21737844#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa\", \"original_url\": true}], \"droplet_content\": \"The danger of growing resistance to antibiotics should be treated as seriously as the threat of terrorism, England's chief medical officer says.\", \"identity_name\": \"BBC News - Home\", \"channel\": \"rss\", \"river_id\": [2]}"; when(mockMessage.getBody()).thenReturn(body.getBytes()); when(mockMessage.getMessageProperties()).thenReturn(mockMessageProperties); when(mockMessageProperties.getDeliveryTag()).thenReturn(22L); when(mockCallbackQueue.getName()).thenReturn("callback"); dropHandler.onMessage(mockMessage, mockChannel); assertTrue(dropsMap.size() > 0);/*from w ww.j a v a 2 s. com*/ assertTrue(deliveryFramesMap.size() > 0); String correlationId = (String) dropsMap.keySet().toArray()[0]; ArgumentCaptor<RawDrop> dropArgument = ArgumentCaptor.forClass(RawDrop.class); ArgumentCaptor<MessagePostProcessor> processorArgument = ArgumentCaptor .forClass(MessagePostProcessor.class); verify(mockAmqpTemplate).convertAndSend(dropArgument.capture(), processorArgument.capture()); RawDrop drop = dropArgument.getValue(); assertTrue(dropsMap.containsValue(drop)); assertEquals("Antibiotic resistance 'threat to UK'", drop.getTitle()); MessagePostProcessor postProcessor = processorArgument.getValue(); postProcessor.postProcessMessage(mockMessage); verify(mockMessageProperties).setReplyTo("callback"); verify(mockMessageProperties).setCorrelationId(correlationId.getBytes()); }
From source file:com.ushahidi.swiftriver.core.dropqueue.MetadataResponseHandlerTest.java
@Test public void onRulesMessage() throws Exception { Message mockMessage = mock(Message.class); MessageProperties mockMessageProperties = mock(MessageProperties.class); Channel mockChannel = mock(Channel.class); String body = "{\"source\":\"rules\",\"identity_orig_id\": \"http://feeds.bbci.co.uk/news/rss.xml\", \"droplet_raw\": \"The danger of growing resistance to antibiotics should be treated as seriously as the threat of terrorism, England's chief medical officer says.\", \"droplet_orig_id\": \"c558d88a44fc70da36d04746574e05e4\", \"droplet_locale\": \"en-gb\", \"identity_username\": \"http://www.bbc.co.uk/news/#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa\", \"droplet_date_pub\": \"Mon, 11 Mar 2013 07:32:59 +0000\", \"droplet_type\": \"original\", \"identity_avatar\": \"http://news.bbcimg.co.uk/nol/shared/img/bbc_news_120x60.gif\", \"droplet_title\": \"Antibiotic resistance 'threat to UK'\", \"links\": [{\"url\": \"http://www.bbc.co.uk/news/health-21737844#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa\", \"original_url\": true}], \"droplet_content\": \"The danger of growing resistance to antibiotics should be treated as seriously as the threat of terrorism, England's chief medical officer says.\", \"identity_name\": \"BBC News - Home\", \"channel\": \"rss\", \"river_id\": [2]}"; when(mockMessage.getBody()).thenReturn(body.getBytes()); when(mockMessage.getMessageProperties()).thenReturn(mockMessageProperties); when(mockMessageProperties.getCorrelationId()).thenReturn("correlation_id".getBytes()); RawDrop rawDrop = new RawDrop(); dropsMap.put("correlation_id", rawDrop); metadataResponseHandler.onMessage(mockMessage, mockChannel); assertTrue(rawDrop.isRulesComplete()); }
From source file:com.ushahidi.swiftriver.core.rules.DropFilterQueueConsumerTest.java
@Test public void onMessage() throws Exception { Message mockMessage = mock(Message.class); Channel mockChannel = mock(Channel.class); MessageProperties mockMessageProperties = mock(MessageProperties.class); String dropJSON = "{\"source\":\"semantics\",\"identity_orig_id\": \"http://feeds.bbci.co.uk/news/rss.xml\", \"droplet_raw\": \"The danger of growing resistance to antibiotics should be treated as seriously as the threat of terrorism, England's chief medical officer says.\", \"droplet_orig_id\": \"c558d88a44fc70da36d04746574e05e4\", \"droplet_locale\": \"en-gb\", \"identity_username\": \"http://www.bbc.co.uk/news/#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa\", \"droplet_date_pub\": \"Mon, 11 Mar 2013 07:32:59 +0000\", \"droplet_type\": \"original\", \"identity_avatar\": \"http://news.bbcimg.co.uk/nol/shared/img/bbc_news_120x60.gif\", \"droplet_title\": \"Antibiotic resistance 'threat to UK'\", \"links\": [{\"url\": \"http://www.bbc.co.uk/news/health-21737844#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa\", \"original_url\": true}], \"droplet_content\": \"The danger of growing resistance to antibiotics should be treated as seriously as the threat of terrorism, England's chief medical officer says.\", \"identity_name\": \"BBC News - Home\", \"channel\": \"rss\", \"river_id\": [2], \"bucket_id\": []}"; when(mockMessage.getBody()).thenReturn(dropJSON.getBytes()); when(mockMessage.getMessageProperties()).thenReturn(mockMessageProperties); when(mockMessageProperties.getReplyTo()).thenReturn("reply-to-queue"); when(mockMessageProperties.getCorrelationId()).thenReturn("drop-correlation-id".getBytes()); // Send the drop to the rules executor dropFilterQueueConsumer.onMessage(mockMessage, mockChannel); ArgumentCaptor<RawDrop> dropArgument = ArgumentCaptor.forClass(RawDrop.class); ArgumentCaptor<String> routingKeyArgument = ArgumentCaptor.forClass(String.class); ArgumentCaptor<MessagePostProcessor> postProcessorArgument = ArgumentCaptor .forClass(MessagePostProcessor.class); verify(mockRulesExecutor).applyRules(dropArgument.capture(), rulesMapArgument.capture()); verify(mockAmqpTemplate).convertAndSend(routingKeyArgument.capture(), dropArgument.capture(), postProcessorArgument.capture()); String routingKey = routingKeyArgument.getValue(); assertEquals("reply-to-queue", routingKey); RawDrop drop = dropArgument.getValue(); assertEquals("rules", drop.getSource()); }
From source file:com.ushahidi.swiftriver.core.dropqueue.MetadataResponseHandlerTest.java
@Test public void onSemanticsMessage() throws Exception { Message mockMessage = mock(Message.class); MessageProperties mockMessageProperties = mock(MessageProperties.class); Channel mockChannel = mock(Channel.class); String body = "{\"source\":\"semantics\",\"identity_orig_id\": \"http://feeds.bbci.co.uk/news/rss.xml\", \"droplet_raw\": \"The danger of growing resistance to antibiotics should be treated as seriously as the threat of terrorism, England's chief medical officer says.\", \"droplet_orig_id\": \"c558d88a44fc70da36d04746574e05e4\", \"droplet_locale\": \"en-gb\", \"identity_username\": \"http://www.bbc.co.uk/news/#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa\", \"droplet_date_pub\": \"Mon, 11 Mar 2013 07:32:59 +0000\", \"droplet_type\": \"original\", \"identity_avatar\": \"http://news.bbcimg.co.uk/nol/shared/img/bbc_news_120x60.gif\", \"droplet_title\": \"Antibiotic resistance 'threat to UK'\", \"links\": [{\"url\": \"http://www.bbc.co.uk/news/health-21737844#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa\", \"original_url\": true}], \"droplet_content\": \"The danger of growing resistance to antibiotics should be treated as seriously as the threat of terrorism, England's chief medical officer says.\", \"identity_name\": \"BBC News - Home\", \"channel\": \"rss\", \"river_id\": [2]}"; when(mockMessage.getBody()).thenReturn(body.getBytes()); when(mockMessage.getMessageProperties()).thenReturn(mockMessageProperties); when(mockMessageProperties.getCorrelationId()).thenReturn("correlation_id".getBytes()); RawDrop rawDrop = new RawDrop(); dropsMap.put("correlation_id", rawDrop); metadataResponseHandler.onMessage(mockMessage, mockChannel); assertTrue(rawDrop.isSemanticsComplete()); }
From source file:com.ushahidi.swiftriver.core.dropqueue.MetadataResponseHandlerTest.java
@Test public void onMediaExtractorMessage() throws Exception { Message mockMessage = mock(Message.class); MessageProperties mockMessageProperties = mock(MessageProperties.class); Channel mockChannel = mock(Channel.class); String body = "{\"source\":\"mediaextractor\",\"identity_orig_id\": \"http://feeds.bbci.co.uk/news/rss.xml\", \"droplet_raw\": \"The danger of growing resistance to antibiotics should be treated as seriously as the threat of terrorism, England's chief medical officer says.\", \"droplet_orig_id\": \"c558d88a44fc70da36d04746574e05e4\", \"droplet_locale\": \"en-gb\", \"identity_username\": \"http://www.bbc.co.uk/news/#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa\", \"droplet_date_pub\": \"Mon, 11 Mar 2013 07:32:59 +0000\", \"droplet_type\": \"original\", \"identity_avatar\": \"http://news.bbcimg.co.uk/nol/shared/img/bbc_news_120x60.gif\", \"droplet_title\": \"Antibiotic resistance 'threat to UK'\", \"links\": [{\"url\": \"http://www.bbc.co.uk/news/health-21737844#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa\", \"original_url\": true}], \"droplet_content\": \"The danger of growing resistance to antibiotics should be treated as seriously as the threat of terrorism, England's chief medical officer says.\", \"identity_name\": \"BBC News - Home\", \"channel\": \"rss\", \"river_id\": [2]}"; when(mockMessage.getBody()).thenReturn(body.getBytes()); when(mockMessage.getMessageProperties()).thenReturn(mockMessageProperties); when(mockMessageProperties.getCorrelationId()).thenReturn("correlation_id".getBytes()); RawDrop rawDrop = new RawDrop(); dropsMap.put("correlation_id", rawDrop); metadataResponseHandler.onMessage(mockMessage, mockChannel); assertTrue(rawDrop.isMediaComplete()); }
From source file:com.ushahidi.swiftriver.core.dropqueue.MetadataResponseHandlerTest.java
@Test public void onMessage() throws Exception { Message mockMessage = mock(Message.class); MessageProperties mockMessageProperties = mock(MessageProperties.class); Channel mockChannel = mock(Channel.class); String body = "{\"source\":\"rules\",\"identity_orig_id\": \"http://feeds.bbci.co.uk/news/rss.xml\", \"droplet_raw\": \"The danger of growing resistance to antibiotics should be treated as seriously as the threat of terrorism, England's chief medical officer says.\", \"droplet_orig_id\": \"c558d88a44fc70da36d04746574e05e4\", \"droplet_locale\": \"en-gb\", \"identity_username\": \"http://www.bbc.co.uk/news/#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa\", \"droplet_date_pub\": \"Mon, 11 Mar 2013 07:32:59 +0000\", \"droplet_type\": \"original\", \"identity_avatar\": \"http://news.bbcimg.co.uk/nol/shared/img/bbc_news_120x60.gif\", \"droplet_title\": \"Antibiotic resistance 'threat to UK'\", \"links\": [{\"url\": \"http://www.bbc.co.uk/news/health-21737844#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa\", \"original_url\": true}], \"droplet_content\": \"The danger of growing resistance to antibiotics should be treated as seriously as the threat of terrorism, England's chief medical officer says.\", \"identity_name\": \"BBC News - Home\", \"channel\": \"rss\", \"river_id\": [2]}"; when(mockMessage.getBody()).thenReturn(body.getBytes()); when(mockMessage.getMessageProperties()).thenReturn(mockMessageProperties); when(mockMessageProperties.getCorrelationId()).thenReturn("correlation_id".getBytes()); RawDrop rawDrop = new RawDrop(); List<Long> riverIds = new ArrayList<Long>(); riverIds.add(2L);/*from w w w . j a va2 s . com*/ rawDrop.setSemanticsComplete(true); rawDrop.setMediaComplete(true); rawDrop.setRulesComplete(true); rawDrop.setRiverIds(riverIds); dropsMap.put("correlation_id", rawDrop); deliveryFramesMap.put("correlation_id", new DeliveryFrame(22, mockChannel)); int size = dropFilterQueue.size(); metadataResponseHandler.onMessage(mockMessage, mockChannel); assertTrue(dropsMap.isEmpty()); assertTrue(publishQueue.contains(rawDrop)); assertEquals(size + 1, dropFilterQueue.size()); assertTrue(deliveryFramesMap.isEmpty()); }
From source file:com.jbrisbin.vpc.jobsched.RequeueMessageListenerAdapter.java
@Override protected void handleResult(Object result, Message request, Channel channel) throws Exception { if (channel != null) { if (logger.isDebugEnabled()) { logger.debug(/*w w w . ja va 2 s .c o m*/ "Listener method returned result [" + result + "] - generating response message for it"); } Message response = buildMessage(channel, result); postProcessResponse(request, response); String replyTo = getResponseReplyTo(request, response, channel); String exchange = getReceivedExchange(request); boolean sendResponse = true; if (result instanceof SqlMessage) { SqlMessage msg = (SqlMessage) result; if (null != msg.getResults().getErrors()) { // Has errors, maybe retry List<String> errors = msg.getResults().getErrors(); if ("Connection timed out".equals(errors.get(errors.size() - 1))) { String messageId = new String(response.getMessageProperties().getCorrelationId()); Map<String, Object> obj = new LinkedHashMap<String, Object>(); obj.put("exchange", request.getMessageProperties().getReceivedExchange()); obj.put("route", request.getMessageProperties().getReceivedRoutingKey()); Map<String, String> headers = new LinkedHashMap<String, String>(); for (Map.Entry<String, Object> itm : request.getMessageProperties().getHeaders() .entrySet()) { headers.put(new String(itm.getKey()), itm.getValue().toString()); } obj.put("headers", headers); obj.put("body", new String(request.getBody())); // Store message and update schema ByteArrayOutputStream bytes = new ByteArrayOutputStream(); mapper.writeValue(bytes, obj); RiakObject riakObj = new RiakObject(riak, riakBucket, messageId, bytes.toByteArray(), "application/json"); StoreResponse storeResp = riakObj.store(); if (storeResp.isSuccess()) { // Don't send the response, requeue it sendResponse = false; } } } } if (sendResponse) { sendResponse(channel, exchange, replyTo, response); } } else if (logger.isWarnEnabled()) { logger.warn("Listener method returned result [" + result + "]: not generating response message for it because of no Rabbit Channel given"); } }
From source file:org.kurento.rabbitmq.RabbitTemplate.java
/** * Send the given message to the specified exchange. * * @param channel/*from w w w . j a v a2 s . c o m*/ * The RabbitMQ Channel to operate within. * @param exchange * The name of the RabbitMQ exchange to send to. * @param routingKey * The routing key. * @param message * The Message to send. * @param correlationData * The correlation data. * @throws IOException * If thrown by RabbitMQ API methods */ protected void doSend(Channel channel, String exchange, String routingKey, Message message, CorrelationData correlationData) throws Exception { if (logger.isDebugEnabled()) { logger.debug("Publishing message on exchange [" + exchange + "], routingKey = [" + routingKey + "]"); } if (exchange == null) { // try to send to configured exchange exchange = this.exchange; } if (routingKey == null) { // try to send to configured routing key routingKey = this.routingKey; } if (this.confirmCallback != null && channel instanceof PublisherCallbackChannel) { PublisherCallbackChannel publisherCallbackChannel = (PublisherCallbackChannel) channel; publisherCallbackChannel.addPendingConfirm(this, channel.getNextPublishSeqNo(), new PendingConfirm(correlationData, System.currentTimeMillis())); } boolean mandatory = this.returnCallback != null && this.mandatory; MessageProperties messageProperties = message.getMessageProperties(); if (mandatory) { messageProperties.getHeaders().put(PublisherCallbackChannel.RETURN_CORRELATION, this.uuid); } BasicProperties convertedMessageProperties = this.messagePropertiesConverter .fromMessageProperties(messageProperties, encoding); channel.basicPublish(exchange, routingKey, mandatory, convertedMessageProperties, message.getBody()); // Check if commit needed if (isChannelLocallyTransacted(channel)) { // Transacted channel created by this template -> commit. RabbitUtils.commitIfNecessary(channel); } }
From source file:org.springframework.amqp.rabbit.admin.RabbitTemplateConsumerExample.java
private static void receiveSync(RabbitTemplate template, int numMessages) { // receive response for (int i = 0; i < numMessages; i++) { Message message = template.receive(TestConstants.QUEUE_NAME); if (message == null) { System.out.println("Thread [" + Thread.currentThread().getId() + "] Received Null Message!"); } else {/*from w ww . j a v a2s .c om*/ System.out.println("Thread [" + Thread.currentThread().getId() + "] Received Message = " + new String(message.getBody())); Map<String, Object> headers = message.getMessageProperties().getHeaders(); Object objFloat = headers.get("float"); Object objcp = headers.get("object"); System.out.println("float header type = " + objFloat.getClass()); System.out.println("object header type = " + objcp.getClass()); } } }