List of usage examples for org.springframework.amqp.core MessagePostProcessor MessagePostProcessor
MessagePostProcessor
From source file:cu.uci.uengine.amqp.SubmitsListener.java
@Override public void onMessage(Message message, Channel channel) throws Exception { message.getMessageProperties().setHeader("__TypeId__", "cu.uci.uengine.model.dto.SubmissionDTO"); try {//from w ww . j a v a 2 s . c o m SubmissionDTO submissionDTO = (SubmissionDTO) jsonMessageConverter.fromMessage(message); Submission submit = new SubmissionDTOToSubmissionAdapter(submissionDTO); log.info(String.format("Working on submission: %s", submit.toString())); Submission result = engine.call(submit); log.info(String.format("Sending sid %s: %s. %s", result.getId(), result.getVerdict(), result.getErrorMessage() == null ? "" : result.getErrorMessage())); VerdictDTO verdict = new SubmissionToVerdictDTOAdapter(result); submitTemplate.convertAndSend(verdict, new MessagePostProcessor() { @Override public Message postProcessMessage(Message message) throws AmqpException { message.getMessageProperties().setHeader("__TypeId__", "UEngineVerdict"); return message; } }); channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } catch (MessageConversionException messageConversionException) { channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false); log.error(String.format("Wrong submission format: %s", messageConversionException.getMessage())); } catch (Exception ex) { channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false); log.error(String.format("Error preocessing message with correlationId: %s", message.getMessageProperties().getCorrelationId())); } }
From source file:org.carewebframework.amqp.rabbitmq.Broker.java
/** * Sends an event to the default exchange. * //from w w w . j a va 2s . c om * @param eventName Name of the event. * @param eventData Associated event data. * @param sender Sender of the event. * @param recipients Recipients of the event. */ public void sendEvent(String eventName, Object eventData, final String sender, final String recipients) { getRabbitTemplate().convertAndSend(exchange.getName(), eventName, eventData, new MessagePostProcessor() { @Override public Message postProcessMessage(Message message) throws AmqpException { return decorateMessage(message, sender, recipients); } }); }
From source file:com.ushahidi.swiftriver.core.dropqueue.DropFilterPublisher.java
public void publishDrop(String dropsMapKey) { LOG.debug(String.format("Sending drop with correlation id %s to rules processor", dropsMapKey)); synchronized (dropsMap) { RawDrop drop = dropsMap.get(dropsMapKey); // Drop doesn't exist; purge if (drop == null) { LOG.info("Drop with correlation ID '{}' not found", dropsMapKey); return; }//from www.ja va2s. c o m final byte[] correlationId = dropsMapKey.getBytes(); final String replyTo = this.getCallbackQueueName(); amqpTemplate.convertAndSend(drop, new MessagePostProcessor() { public Message postProcessMessage(Message message) throws AmqpException { message.getMessageProperties().setCorrelationId(correlationId); message.getMessageProperties().setReplyTo(replyTo); return message; } }); } }
From source file:com.ushahidi.swiftriver.core.rules.DropFilterQueueConsumer.java
public synchronized void onMessage(Message message, Channel channel) throws JsonGenerationException, JsonMappingException, IOException { // Serialize the message into a Drop POJO RawDrop drop = objectMapper.readValue(new String(message.getBody()), RawDrop.class); ConcurrentMap<Long, Map<Long, Rule>> rulesMap = rulesRegistry.getRulesMap(); // Send the drop for rules processing rulesExecutor.applyRules(drop, rulesMap); // Set the source to "rules" drop.setSource("rules"); // Get he correlation id and callback queue name final String correlationId = new String(message.getMessageProperties().getCorrelationId()); String routingKey = message.getMessageProperties().getReplyTo(); long deliveryTag = message.getMessageProperties().getDeliveryTag(); // Place drop on the publishing queue amqpTemplate.convertAndSend(routingKey, drop, new MessagePostProcessor() { public Message postProcessMessage(Message message) throws AmqpException { try { message.getMessageProperties().setCorrelationId(correlationId.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { logger.error("An error occurred while setting the correlation ID {}", correlationId, e); }/* w w w. j a va 2 s .c o m*/ return message; } }); // Only acknowledge the current message channel.basicAck(deliveryTag, false); logger.debug("Drop with correlation id {} has completed rules processing", correlationId); }
From source file:com.ushahidi.swiftriver.core.dropqueue.DropHandler.java
/** * Receive drops placed on the DROPLET_QUEUE by channel apps. * /*ww w .j a va2s . c om*/ * Caches the drop in the dropsMap for metadata updates and then publishes * the drop to the metadata exchange for metadata extraction to be * performed. * * @param message * @throws IOException * @throws JsonMappingException * @throws JsonParseException */ public synchronized void onMessage(Message message, Channel channel) throws JsonParseException, JsonMappingException, IOException { RawDrop drop = objectMapper.readValue(new String(message.getBody()), RawDrop.class); final String correlationId = UUID.randomUUID().toString(); final String replyTo = callbackQueue.getName(); long deliveryTag = message.getMessageProperties().getDeliveryTag(); dropsMap.put(correlationId, drop); DeliveryFrame deliveryFrame = new DeliveryFrame(deliveryTag, channel); deliveryFramesMap.put(correlationId, deliveryFrame); logger.debug("Sending drop with correlation ID {} to {}", correlationId, replyTo); amqpTemplate.convertAndSend(drop, new MessagePostProcessor() { public Message postProcessMessage(Message message) throws AmqpException { message.getMessageProperties().setReplyTo(replyTo); try { message.getMessageProperties().setCorrelationId(correlationId.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new AmqpException(e); } return message; } }); logger.debug("Drop sent for metadata extraction with correlation id '{}'", correlationId); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java
@Test public void testSendAndReceiveWithPostProcessor() throws Exception { template.convertAndSend(ROUTE, (Object) "message", new MessagePostProcessor() { public Message postProcessMessage(Message message) throws AmqpException { message.getMessageProperties().setContentType("text/other"); // message.getMessageProperties().setUserId("foo"); return message; }/* ww w . j a v a 2 s . c om*/ }); String result = (String) template.receiveAndConvert(ROUTE); assertEquals("message", result); result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java
@Test public void testAtomicSendAndReceiveWithConversionAndMessagePostProcessor() throws Exception { final RabbitTemplate template = new RabbitTemplate(new CachingConnectionFactory()); template.setRoutingKey(ROUTE);//from www. j a v a 2 s. c o m template.setQueue(ROUTE); ExecutorService executor = Executors.newFixedThreadPool(1); // Set up a consumer to respond to our producer Future<String> received = executor.submit(new Callable<String>() { public String call() throws Exception { Message message = null; for (int i = 0; i < 10; i++) { message = template.receive(); if (message != null) { break; } Thread.sleep(100L); } assertNotNull("No message received", message); template.send(message.getMessageProperties().getReplyTo(), message); return (String) template.getMessageConverter().fromMessage(message); } }); String result = (String) template.convertSendAndReceive((Object) "message", new MessagePostProcessor() { public Message postProcessMessage(Message message) throws AmqpException { try { byte[] newBody = new String(message.getBody(), "UTF-8").toUpperCase().getBytes("UTF-8"); return new Message(newBody, message.getMessageProperties()); } catch (Exception e) { throw new AmqpException("unexpected failure in test", e); } } }); assertEquals("MESSAGE", received.get(1000, TimeUnit.MILLISECONDS)); assertEquals("MESSAGE", result); // Message was consumed so nothing left on queue result = (String) template.receiveAndConvert(); assertEquals(null, result); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java
@Test public void testAtomicSendAndReceiveWithConversionAndMessagePostProcessorUsingRoutingKey() throws Exception { ExecutorService executor = Executors.newFixedThreadPool(1); // Set up a consumer to respond to our producer Future<String> received = executor.submit(new Callable<String>() { public String call() throws Exception { Message message = null;//w w w. j av a2 s . co m for (int i = 0; i < 10; i++) { message = template.receive(ROUTE); if (message != null) { break; } Thread.sleep(100L); } assertNotNull("No message received", message); template.send(message.getMessageProperties().getReplyTo(), message); return (String) template.getMessageConverter().fromMessage(message); } }); String result = (String) template.convertSendAndReceive(ROUTE, (Object) "message", new MessagePostProcessor() { public Message postProcessMessage(Message message) throws AmqpException { try { byte[] newBody = new String(message.getBody(), "UTF-8").toUpperCase().getBytes("UTF-8"); return new Message(newBody, message.getMessageProperties()); } catch (Exception e) { throw new AmqpException("unexpected failure in test", e); } } }); assertEquals("MESSAGE", received.get(1000, TimeUnit.MILLISECONDS)); assertEquals("MESSAGE", result); // Message was consumed so nothing left on queue result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java
@Test public void testAtomicSendAndReceiveWithConversionAndMessagePostProcessorUsingExchangeAndRoutingKey() throws Exception { ExecutorService executor = Executors.newFixedThreadPool(1); // Set up a consumer to respond to our producer Future<String> received = executor.submit(new Callable<String>() { public String call() throws Exception { Message message = null;/*from w ww .j av a 2s. c o m*/ for (int i = 0; i < 10; i++) { message = template.receive(ROUTE); if (message != null) { break; } Thread.sleep(100L); } assertNotNull("No message received", message); template.send(message.getMessageProperties().getReplyTo(), message); return (String) template.getMessageConverter().fromMessage(message); } }); String result = (String) template.convertSendAndReceive("", ROUTE, "message", new MessagePostProcessor() { public Message postProcessMessage(Message message) throws AmqpException { try { byte[] newBody = new String(message.getBody(), "UTF-8").toUpperCase().getBytes("UTF-8"); return new Message(newBody, message.getMessageProperties()); } catch (Exception e) { throw new AmqpException("unexpected failure in test", e); } } }); assertEquals("MESSAGE", received.get(1000, TimeUnit.MILLISECONDS)); assertEquals("MESSAGE", result); // Message was consumed so nothing left on queue result = (String) template.receiveAndConvert(ROUTE); assertEquals(null, result); }