Search in sources :

Example 1 with MessageDeliveryMode

use of org.springframework.amqp.core.MessageDeliveryMode in project spring-integration by spring-projects.

the class MappingUtilsTests method testMapping.

@Test
public void testMapping() {
    Message<?> requestMessage = MessageBuilder.withPayload("foo").setHeader(AmqpHeaders.CONTENT_TYPE, "my/ct").build();
    MessageConverter converter = new SimpleMessageConverter();
    AmqpHeaderMapper headerMapper = DefaultAmqpHeaderMapper.outboundMapper();
    MessageDeliveryMode defaultDeliveryMode = MessageDeliveryMode.NON_PERSISTENT;
    boolean headersMappedLast = false;
    org.springframework.amqp.core.Message mapped = MappingUtils.mapMessage(requestMessage, converter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("text/plain"));
    headersMappedLast = true;
    mapped = MappingUtils.mapMessage(requestMessage, converter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("my/ct"));
    ContentTypeDelegatingMessageConverter ctdConverter = new ContentTypeDelegatingMessageConverter();
    ctdConverter.addDelegate("my/ct", converter);
    mapped = MappingUtils.mapMessage(requestMessage, ctdConverter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("my/ct"));
    headersMappedLast = false;
    mapped = MappingUtils.mapMessage(requestMessage, ctdConverter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("text/plain"));
    headersMappedLast = true;
    requestMessage = MessageBuilder.withPayload("foo").setHeader(AmqpHeaders.CONTENT_TYPE, 42).build();
    try {
        mapped = MappingUtils.mapMessage(requestMessage, ctdConverter, headerMapper, defaultDeliveryMode, headersMappedLast);
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), equalTo("contentType header must be a MimeType or String, found: java.lang.Integer"));
    }
}
Also used : SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) ContentTypeDelegatingMessageConverter(org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) ContentTypeDelegatingMessageConverter(org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter) MessageDeliveryMode(org.springframework.amqp.core.MessageDeliveryMode) Test(org.junit.Test)

Example 2 with MessageDeliveryMode

use of org.springframework.amqp.core.MessageDeliveryMode in project spring-integration by spring-projects.

the class DefaultAmqpHeaderMapper method populateStandardHeaders.

/**
 * 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);
    }
    String correlationId = getHeaderIfAvailable(headers, AmqpHeaders.CORRELATION_ID, String.class);
    if (StringUtils.hasText(correlationId)) {
        amqpMessageProperties.setCorrelationId(correlationId);
    }
    Integer delay = getHeaderIfAvailable(headers, AmqpHeaders.DELAY, Integer.class);
    if (delay != null) {
        amqpMessageProperties.setDelay(delay);
    }
    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, IntegrationMessageHeaderAccessor.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);
    }
    Map<String, String> jsonHeaders = new HashMap<String, String>();
    for (String jsonHeader : JsonHeaders.HEADERS) {
        Object value = getHeaderIfAvailable(headers, jsonHeader, Object.class);
        if (value != null) {
            headers.remove(jsonHeader);
            if (value instanceof Class<?>) {
                value = ((Class<?>) value).getName();
            }
            jsonHeaders.put(jsonHeader.replaceFirst(JsonHeaders.PREFIX, ""), value.toString());
        }
    }
    /*
		 * If the MessageProperties already contains JsonHeaders, don't overwrite them here because they were
		 * set up by a message converter.
		 */
    if (!amqpMessageProperties.getHeaders().containsKey(JsonHeaders.TYPE_ID.replaceFirst(JsonHeaders.PREFIX, ""))) {
        amqpMessageProperties.getHeaders().putAll(jsonHeaders);
    }
    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);
    }
}
Also used : HashMap(java.util.HashMap) Date(java.util.Date) MessageDeliveryMode(org.springframework.amqp.core.MessageDeliveryMode)

Example 3 with MessageDeliveryMode

use of org.springframework.amqp.core.MessageDeliveryMode in project spring-integration by spring-projects.

the class DefaultAmqpHeaderMapper method extractStandardHeaders.

/**
 * Extract "standard" headers from an AMQP MessageProperties instance.
 */
@Override
protected Map<String, Object> extractStandardHeaders(MessageProperties amqpMessageProperties) {
    Map<String, Object> headers = new HashMap<String, Object>();
    try {
        String appId = amqpMessageProperties.getAppId();
        if (StringUtils.hasText(appId)) {
            headers.put(AmqpHeaders.APP_ID, appId);
        }
        String clusterId = amqpMessageProperties.getClusterId();
        if (StringUtils.hasText(clusterId)) {
            headers.put(AmqpHeaders.CLUSTER_ID, clusterId);
        }
        String contentEncoding = amqpMessageProperties.getContentEncoding();
        if (StringUtils.hasText(contentEncoding)) {
            headers.put(AmqpHeaders.CONTENT_ENCODING, contentEncoding);
        }
        long contentLength = amqpMessageProperties.getContentLength();
        if (contentLength > 0) {
            headers.put(AmqpHeaders.CONTENT_LENGTH, contentLength);
        }
        String contentType = amqpMessageProperties.getContentType();
        if (StringUtils.hasText(contentType)) {
            headers.put(AmqpHeaders.CONTENT_TYPE, contentType);
        }
        String correlationId = amqpMessageProperties.getCorrelationId();
        if (StringUtils.hasText(contentType)) {
            headers.put(AmqpHeaders.CORRELATION_ID, correlationId);
        }
        MessageDeliveryMode receivedDeliveryMode = amqpMessageProperties.getReceivedDeliveryMode();
        if (receivedDeliveryMode != null) {
            headers.put(AmqpHeaders.RECEIVED_DELIVERY_MODE, receivedDeliveryMode);
        }
        long deliveryTag = amqpMessageProperties.getDeliveryTag();
        if (deliveryTag > 0) {
            headers.put(AmqpHeaders.DELIVERY_TAG, deliveryTag);
        }
        String expiration = amqpMessageProperties.getExpiration();
        if (StringUtils.hasText(expiration)) {
            headers.put(AmqpHeaders.EXPIRATION, expiration);
        }
        Integer messageCount = amqpMessageProperties.getMessageCount();
        if (messageCount != null && messageCount > 0) {
            headers.put(AmqpHeaders.MESSAGE_COUNT, messageCount);
        }
        String messageId = amqpMessageProperties.getMessageId();
        if (StringUtils.hasText(messageId)) {
            headers.put(AmqpHeaders.MESSAGE_ID, messageId);
        }
        Integer priority = amqpMessageProperties.getPriority();
        if (priority != null && priority > 0) {
            headers.put(IntegrationMessageHeaderAccessor.PRIORITY, priority);
        }
        Integer receivedDelay = amqpMessageProperties.getReceivedDelay();
        if (receivedDelay != null) {
            headers.put(AmqpHeaders.RECEIVED_DELAY, receivedDelay);
        }
        String receivedExchange = amqpMessageProperties.getReceivedExchange();
        if (StringUtils.hasText(receivedExchange)) {
            headers.put(AmqpHeaders.RECEIVED_EXCHANGE, receivedExchange);
        }
        String receivedRoutingKey = amqpMessageProperties.getReceivedRoutingKey();
        if (StringUtils.hasText(receivedRoutingKey)) {
            headers.put(AmqpHeaders.RECEIVED_ROUTING_KEY, receivedRoutingKey);
        }
        Boolean redelivered = amqpMessageProperties.isRedelivered();
        if (redelivered != null) {
            headers.put(AmqpHeaders.REDELIVERED, redelivered);
        }
        String replyTo = amqpMessageProperties.getReplyTo();
        if (replyTo != null) {
            headers.put(AmqpHeaders.REPLY_TO, replyTo);
        }
        Date timestamp = amqpMessageProperties.getTimestamp();
        if (timestamp != null) {
            headers.put(AmqpHeaders.TIMESTAMP, timestamp);
        }
        String type = amqpMessageProperties.getType();
        if (StringUtils.hasText(type)) {
            headers.put(AmqpHeaders.TYPE, type);
        }
        String userId = amqpMessageProperties.getReceivedUserId();
        if (StringUtils.hasText(userId)) {
            headers.put(AmqpHeaders.RECEIVED_USER_ID, userId);
        }
        for (String jsonHeader : JsonHeaders.HEADERS) {
            Object value = amqpMessageProperties.getHeaders().get(jsonHeader.replaceFirst(JsonHeaders.PREFIX, ""));
            if (value instanceof String && StringUtils.hasText((String) value)) {
                headers.put(jsonHeader, value);
            }
        }
    } catch (Exception e) {
        if (logger.isWarnEnabled()) {
            logger.warn("error occurred while mapping from AMQP properties to MessageHeaders", e);
        }
    }
    return headers;
}
Also used : HashMap(java.util.HashMap) Date(java.util.Date) MessageDeliveryMode(org.springframework.amqp.core.MessageDeliveryMode)

Aggregations

MessageDeliveryMode (org.springframework.amqp.core.MessageDeliveryMode)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)1 ContentTypeDelegatingMessageConverter (org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter)1 MessageConverter (org.springframework.amqp.support.converter.MessageConverter)1 SimpleMessageConverter (org.springframework.amqp.support.converter.SimpleMessageConverter)1