@Test public void testPublisherReturnsWithMandatoryExpression() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final List<Message> returns = new ArrayList<Message>(); templateWithReturnsEnabled.setReturnCallback( new ReturnCallback() { @Override public void returnedMessage( Message message, int replyCode, String replyText, String exchange, String routingKey) { returns.add(message); latch.countDown(); } }); Expression mandatoryExpression = new SpelExpressionParser().parseExpression("'message'.bytes == body"); templateWithReturnsEnabled.setMandatoryExpression(mandatoryExpression); templateWithReturnsEnabled.convertAndSend( ROUTE + "junk", (Object) "message", new CorrelationData("abc")); templateWithReturnsEnabled.convertAndSend( ROUTE + "junk", (Object) "foo", new CorrelationData("abc")); assertTrue(latch.await(1000, TimeUnit.MILLISECONDS)); assertEquals(1, returns.size()); Message message = returns.get(0); assertEquals("message", new String(message.getBody(), "utf-8")); }
@SuppressWarnings("unchecked") public <T> T fromMessage(Message message, T t) { String json = ""; try { json = new String(message.getBody(), defaultCharset); } catch (Exception e) { e.printStackTrace(); } return (T) JSON.parseObject(json, t.getClass()); }
@Override public Object fromMessage(Message message) throws MessageConversionException { Object content = null; MessageProperties properties = message.getMessageProperties(); if (properties != null) { String contentType = properties.getContentType(); if (contentType != null && contentType.contains("protobuf")) { try { String classId = (String) message.getMessageProperties().getHeaders().get("__TypeId__"); if (classId == null) throw new Exception("no classId found"); Class<?> targetClass = Class.forName(classId); content = converter.deserialize(message.getBody(), targetClass); } catch (Exception e) { throw new MessageConversionException("Failed to convert json-based Message content", e); } } } if (content == null) { content = message.getBody(); } return content; }
@Override public void onMessage(Message msg) { String messageStr = new String(msg.getBody()); try { logger.debug("Facility Manager Get Request Message: " + messageStr.toString()); // TODO ResponseMessage ret = this.man.executeCommand( // ((ActionRequestMessage)engineMessage).getRequest() ); msg.getMessageProperties().getCorrelationId(); msg.getMessageProperties().getReplyTo(); // ret.setMessageID(engineMessage.getMessageID()); } catch (AmqpException e) { e.printStackTrace(); } }
@Test public void testAutoBindDLQwithRepublish() throws Exception { // pre-declare the queue with dead-lettering, users can also use a policy RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource()); Map<String, Object> args = new HashMap<String, Object>(); args.put("x-dead-letter-exchange", "xdbustest.DLX"); Queue queue = new Queue("xdbustest.dlqpubtest", true, false, false, args); admin.declareQueue(queue); MessageBus bus = getMessageBus(); Properties properties = new Properties(); properties.put("prefix", "xdbustest."); properties.put("autoBindDLQ", "true"); properties.put("republishToDLQ", "true"); properties.put("maxAttempts", "1"); // disable retry properties.put("requeue", "false"); DirectChannel moduleInputChannel = new DirectChannel(); moduleInputChannel.setBeanName("dlqPubTest"); moduleInputChannel.subscribe( new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { throw new RuntimeException("foo"); } }); bus.bindConsumer("dlqpubtest", moduleInputChannel, properties); RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource()); template.convertAndSend("", "xdbustest.dlqpubtest", "foo"); int n = 0; while (n++ < 100) { org.springframework.amqp.core.Message deadLetter = template.receive("xdbustest.dlqpubtest.dlq"); if (deadLetter != null) { assertEquals("foo", new String(deadLetter.getBody())); assertNotNull(deadLetter.getMessageProperties().getHeaders().get("x-exception-stacktrace")); break; } Thread.sleep(100); } assertTrue(n < 100); bus.unbindConsumer("dlqpubtest", moduleInputChannel); admin.deleteQueue("xdbustest.dlqpubtest.dlq"); admin.deleteQueue("xdbustest.dlqpubtest"); admin.deleteExchange("xdbustest.DLX"); }
@Override public void onMessage(Message message, Channel channel) throws Exception { String value = new String(message.getBody()); try { int counter = count.getAndIncrement(); if (logger.isDebugEnabled() && counter % 100 == 0) { logger.debug(value + counter); } if (fail) { throw new RuntimeException("Planned failure"); } } finally { latch.countDown(); } }
@Override public Object fromMessage(Message message) throws MessageConversionException { Object content = null; try { MessageProperties properties = message.getMessageProperties(); String encoding = properties.getContentEncoding(); if (encoding == null) { encoding = this.defaultCharset; } content = new String(message.getBody(), encoding); content = JSONObject.parse((String) content); } catch (UnsupportedEncodingException e) { throw new MessageConversionException("failed to convert text-based Message content", e); } return content; }
@Override public void onMessage(Message message, Channel channel) throws Exception { String value = new String(message.getBody()); try { logger.debug("Received: " + value); if (count.get() == null) { count.set(1); } else { count.set(count.get() + 1); } if (count.get() == txSize && fail) { logger.debug("Failing: " + value); count.set(0); throw new RuntimeException("Planned"); } } finally { latch.countDown(); } }
@Override public Object fromMessage(Message message) throws MessageConversionException { MessageProperties messageProperties = message.getMessageProperties(); if (messageProperties == null) throw new MessageConversionException("Cannot decode a message with no properties!"); byte[] body = message.getBody(); if (body == null) return null; String messageEncoding = messageProperties.getContentEncoding(); if (messageEncoding == null) messageEncoding = this.encoding; String messageContentType = messageProperties.getContentType(); if (this.contentType != null && !this.contentType.equalsIgnoreCase(messageContentType)) throw new MessageConversionException( "Cannot understand a message of type " + messageContentType); try { return new String(body, messageEncoding); } catch (UnsupportedEncodingException ex) { LOG.error("Cannot dencode strings as {}", this.encoding, ex); throw new MessageConversionException("Cannot dencode strings as " + this.encoding, ex); } }
@Override protected void executeListener(Channel channel, Message message) throws Throwable { super.executeListener(channel, message); channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); if (log.isDebugEnabled()) log.debug("Acknowledging message: " + new String(message.getBody())); }
@Override public void onMessage(Message message) { System.out.println("接收到消息:" + new String(message.getBody())); }
public Object fromMessage(Message message) throws MessageConversionException { return JSON.parseObject(message.getBody(), DataSyncCipher.class); }