@Test public void withHeaderMapperStandardAndCustomHeaders() { AmqpInboundChannelAdapter adapter = context.getBean( "withHeaderMapperStandardAndCustomHeaders", AmqpInboundChannelAdapter.class); AbstractMessageListenerContainer mlc = TestUtils.getPropertyValue( adapter, "messageListenerContainer", AbstractMessageListenerContainer.class); MessageListener listener = TestUtils.getPropertyValue(mlc, "messageListener", MessageListener.class); MessageProperties amqpProperties = new MessageProperties(); amqpProperties.setAppId("test.appId"); amqpProperties.setClusterId("test.clusterId"); amqpProperties.setContentEncoding("test.contentEncoding"); amqpProperties.setContentLength(99L); amqpProperties.setContentType("test.contentType"); amqpProperties.setHeader("foo", "foo"); amqpProperties.setHeader("bar", "bar"); Message amqpMessage = new Message("hello".getBytes(), amqpProperties); listener.onMessage(amqpMessage); QueueChannel requestChannel = context.getBean("requestChannel", QueueChannel.class); org.springframework.integration.Message<?> siMessage = requestChannel.receive(0); assertEquals("foo", siMessage.getHeaders().get("foo")); assertNull(siMessage.getHeaders().get("bar")); assertNotNull(siMessage.getHeaders().get(AmqpHeaders.CONTENT_ENCODING)); assertNotNull(siMessage.getHeaders().get(AmqpHeaders.CLUSTER_ID)); assertNotNull(siMessage.getHeaders().get(AmqpHeaders.APP_ID)); assertNotNull(siMessage.getHeaders().get(AmqpHeaders.CONTENT_TYPE)); }
@Override protected Message createMessage(Object object, MessageProperties messageProperties) { byte[] bytes = null; try { bytes = converter.serialize(object); } catch (Exception e) { throw new MessageConversionException("Failed to convert Message content", e); } messageProperties.setContentType("appication/protobuf"); if (bytes != null) { messageProperties.setContentLength(bytes.length); } messageProperties.getHeaders().put("__TypeId__", object.getClass().getName()); return new Message(bytes, messageProperties); }
protected Message createMessage(Object objectToConvert, MessageProperties messageProperties) throws MessageConversionException { byte[] bytes; try { String jsonString = JSON.toJSONString(objectToConvert); bytes = jsonString.getBytes(this.defaultCharset); } catch (Exception e) { throw new MessageConversionException("Failed to convert Message content", e); } messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON); messageProperties.setContentEncoding(this.defaultCharset); if (bytes != null) { messageProperties.setContentLength(bytes.length); } return new Message(bytes, messageProperties); }
@Override protected Message createMessage(Object objectToConvert, MessageProperties messageProperties) { byte[] bytes = null; try { String jsonString = JSONObject.toJSONString(objectToConvert); bytes = jsonString.getBytes(getDefaultCharset()); } catch (IOException e) { throw new MessageConversionException("Failed to convert Message content", e); } messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON); messageProperties.setContentEncoding(getDefaultCharset()); if (bytes != null) { messageProperties.setContentLength(bytes.length); } // 不要使用,会丢掉对象属性,缺少实现 return new Message(bytes, messageProperties); }
@Override protected Message createMessage(Object object, MessageProperties messageProperties) { try { byte[] body = null; if (object != null) { body = object.toString().getBytes(this.encoding); } String msgContentType = this.contentType == null ? DEFAULT_CONTENT_TYPE : this.contentType; messageProperties.setContentType(msgContentType); messageProperties.setContentEncoding(this.encoding); messageProperties.setContentLength(body != null ? body.length : 0); return new Message(body, messageProperties); } catch (UnsupportedEncodingException ex) { LOG.error("Cannot encode strings as {}", this.encoding, ex); throw new MessageConversionException("Cannot encode strings as " + this.encoding, ex); } }
/** * Maps headers from a Spring Integration MessageHeaders instance to the MessageProperties of an * AMQP Message. */ @Override protected void populateStandardHeaders( Map<String, Object> headers, MessageProperties amqpMessageProperties) { String appId = getHeaderIfAvailable(headers, AmqpHeaders.APP_ID, String.class); if (StringUtils.hasText(appId)) { amqpMessageProperties.setAppId(appId); } String clusterId = getHeaderIfAvailable(headers, AmqpHeaders.CLUSTER_ID, String.class); if (StringUtils.hasText(clusterId)) { amqpMessageProperties.setClusterId(clusterId); } String contentEncoding = getHeaderIfAvailable(headers, AmqpHeaders.CONTENT_ENCODING, String.class); if (StringUtils.hasText(contentEncoding)) { amqpMessageProperties.setContentEncoding(contentEncoding); } Long contentLength = getHeaderIfAvailable(headers, AmqpHeaders.CONTENT_LENGTH, Long.class); if (contentLength != null) { amqpMessageProperties.setContentLength(contentLength); } String contentType = this.extractContentTypeAsString(headers); if (StringUtils.hasText(contentType)) { amqpMessageProperties.setContentType(contentType); } Object correlationId = headers.get(AmqpHeaders.CORRELATION_ID); if (correlationId instanceof byte[]) { amqpMessageProperties.setCorrelationId((byte[]) correlationId); } MessageDeliveryMode deliveryMode = getHeaderIfAvailable(headers, AmqpHeaders.DELIVERY_MODE, MessageDeliveryMode.class); if (deliveryMode != null) { amqpMessageProperties.setDeliveryMode(deliveryMode); } Long deliveryTag = getHeaderIfAvailable(headers, AmqpHeaders.DELIVERY_TAG, Long.class); if (deliveryTag != null) { amqpMessageProperties.setDeliveryTag(deliveryTag); } String expiration = getHeaderIfAvailable(headers, AmqpHeaders.EXPIRATION, String.class); if (StringUtils.hasText(expiration)) { amqpMessageProperties.setExpiration(expiration); } Integer messageCount = getHeaderIfAvailable(headers, AmqpHeaders.MESSAGE_COUNT, Integer.class); if (messageCount != null) { amqpMessageProperties.setMessageCount(messageCount); } String messageId = getHeaderIfAvailable(headers, AmqpHeaders.MESSAGE_ID, String.class); if (StringUtils.hasText(messageId)) { amqpMessageProperties.setMessageId(messageId); } Integer priority = getHeaderIfAvailable(headers, MessageHeaders.PRIORITY, Integer.class); if (priority != null) { amqpMessageProperties.setPriority(priority); } String receivedExchange = getHeaderIfAvailable(headers, AmqpHeaders.RECEIVED_EXCHANGE, String.class); if (StringUtils.hasText(receivedExchange)) { amqpMessageProperties.setReceivedExchange(receivedExchange); } String receivedRoutingKey = getHeaderIfAvailable(headers, AmqpHeaders.RECEIVED_ROUTING_KEY, String.class); if (StringUtils.hasText(receivedRoutingKey)) { amqpMessageProperties.setReceivedRoutingKey(receivedRoutingKey); } Boolean redelivered = getHeaderIfAvailable(headers, AmqpHeaders.REDELIVERED, Boolean.class); if (redelivered != null) { amqpMessageProperties.setRedelivered(redelivered); } String replyTo = getHeaderIfAvailable(headers, AmqpHeaders.REPLY_TO, String.class); if (replyTo != null) { amqpMessageProperties.setReplyTo(replyTo); } Date timestamp = getHeaderIfAvailable(headers, AmqpHeaders.TIMESTAMP, Date.class); if (timestamp != null) { amqpMessageProperties.setTimestamp(timestamp); } String type = getHeaderIfAvailable(headers, AmqpHeaders.TYPE, String.class); if (type != null) { amqpMessageProperties.setType(type); } String userId = getHeaderIfAvailable(headers, AmqpHeaders.USER_ID, String.class); if (StringUtils.hasText(userId)) { amqpMessageProperties.setUserId(userId); } String replyCorrelation = getHeaderIfAvailable(headers, AmqpHeaders.SPRING_REPLY_CORRELATION, String.class); if (StringUtils.hasLength(replyCorrelation)) { amqpMessageProperties.setHeader("spring_reply_correlation", replyCorrelation); } String replyToStack = getHeaderIfAvailable(headers, AmqpHeaders.SPRING_REPLY_TO_STACK, String.class); if (StringUtils.hasLength(replyToStack)) { amqpMessageProperties.setHeader("spring_reply_to", replyToStack); } }