Search in sources :

Example 1 with JavaUtils

use of org.springframework.amqp.utils.JavaUtils in project spring-amqp by spring-projects.

the class SimpleAmqpHeaderMapper method toHeaders.

@Override
public MessageHeaders toHeaders(MessageProperties amqpMessageProperties) {
    Map<String, Object> headers = new HashMap<String, Object>();
    try {
        BiConsumer<String, Object> putObject = headers::put;
        BiConsumer<String, String> putString = headers::put;
        JavaUtils javaUtils = JavaUtils.INSTANCE.acceptIfNotNull(AmqpHeaders.APP_ID, amqpMessageProperties.getAppId(), putObject).acceptIfNotNull(AmqpHeaders.CLUSTER_ID, amqpMessageProperties.getClusterId(), putObject).acceptIfNotNull(AmqpHeaders.CONTENT_ENCODING, amqpMessageProperties.getContentEncoding(), putObject);
        long contentLength = amqpMessageProperties.getContentLength();
        javaUtils.acceptIfCondition(contentLength > 0, AmqpHeaders.CONTENT_LENGTH, contentLength, putObject).acceptIfHasText(AmqpHeaders.CONTENT_TYPE, amqpMessageProperties.getContentType(), putString).acceptIfHasText(AmqpHeaders.CORRELATION_ID, amqpMessageProperties.getCorrelationId(), putString).acceptIfNotNull(AmqpHeaders.RECEIVED_DELIVERY_MODE, amqpMessageProperties.getReceivedDeliveryMode(), putObject);
        long deliveryTag = amqpMessageProperties.getDeliveryTag();
        javaUtils.acceptIfCondition(deliveryTag > 0, AmqpHeaders.DELIVERY_TAG, deliveryTag, putObject).acceptIfHasText(AmqpHeaders.EXPIRATION, amqpMessageProperties.getExpiration(), putString).acceptIfNotNull(AmqpHeaders.MESSAGE_COUNT, amqpMessageProperties.getMessageCount(), putObject).acceptIfNotNull(AmqpHeaders.MESSAGE_ID, amqpMessageProperties.getMessageId(), putObject);
        Integer priority = amqpMessageProperties.getPriority();
        javaUtils.acceptIfCondition(priority != null && priority > 0, AmqpMessageHeaderAccessor.PRIORITY, priority, putObject).acceptIfNotNull(AmqpHeaders.RECEIVED_DELAY, amqpMessageProperties.getReceivedDelay(), putObject).acceptIfHasText(AmqpHeaders.RECEIVED_EXCHANGE, amqpMessageProperties.getReceivedExchange(), putString).acceptIfHasText(AmqpHeaders.RECEIVED_ROUTING_KEY, amqpMessageProperties.getReceivedRoutingKey(), putString).acceptIfNotNull(AmqpHeaders.REDELIVERED, amqpMessageProperties.isRedelivered(), putObject).acceptIfNotNull(AmqpHeaders.REPLY_TO, amqpMessageProperties.getReplyTo(), putObject).acceptIfNotNull(AmqpHeaders.TIMESTAMP, amqpMessageProperties.getTimestamp(), putObject).acceptIfHasText(AmqpHeaders.TYPE, amqpMessageProperties.getType(), putString).acceptIfHasText(AmqpHeaders.RECEIVED_USER_ID, amqpMessageProperties.getReceivedUserId(), putString).acceptIfHasText(AmqpHeaders.CONSUMER_TAG, amqpMessageProperties.getConsumerTag(), putString).acceptIfHasText(AmqpHeaders.CONSUMER_QUEUE, amqpMessageProperties.getConsumerQueue(), putString);
        headers.put(AmqpHeaders.LAST_IN_BATCH, amqpMessageProperties.isLastInBatch());
        // Map custom headers
        for (Map.Entry<String, Object> entry : amqpMessageProperties.getHeaders().entrySet()) {
            headers.put(entry.getKey(), entry.getValue());
        }
    } catch (Exception e) {
        if (logger.isWarnEnabled()) {
            logger.warn("error occurred while mapping from AMQP properties to MessageHeaders", e);
        }
    }
    return new MessageHeaders(headers);
}
Also used : HashMap(java.util.HashMap) JavaUtils(org.springframework.amqp.utils.JavaUtils) MessageHeaders(org.springframework.messaging.MessageHeaders) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with JavaUtils

use of org.springframework.amqp.utils.JavaUtils in project spring-amqp by spring-projects.

the class DirectRabbitListenerContainerFactory method initializeContainer.

@Override
protected void initializeContainer(DirectMessageListenerContainer instance, RabbitListenerEndpoint endpoint) {
    super.initializeContainer(instance, endpoint);
    JavaUtils javaUtils = JavaUtils.INSTANCE.acceptIfNotNull(this.taskScheduler, instance::setTaskScheduler).acceptIfNotNull(this.monitorInterval, instance::setMonitorInterval).acceptIfNotNull(this.messagesPerAck, instance::setMessagesPerAck).acceptIfNotNull(this.ackTimeout, instance::setAckTimeout);
    if (endpoint != null && endpoint.getConcurrency() != null) {
        try {
            instance.setConsumersPerQueue(Integer.parseInt(endpoint.getConcurrency()));
        } catch (NumberFormatException e) {
            throw new IllegalStateException("Failed to parse concurrency: " + e.getMessage(), e);
        }
    } else {
        javaUtils.acceptIfNotNull(this.consumersPerQueue, instance::setConsumersPerQueue);
    }
}
Also used : JavaUtils(org.springframework.amqp.utils.JavaUtils)

Example 3 with JavaUtils

use of org.springframework.amqp.utils.JavaUtils in project spring-amqp by spring-projects.

the class SimpleAmqpHeaderMapper method fromHeaders.

@Override
public void fromHeaders(MessageHeaders headers, MessageProperties amqpMessageProperties) {
    JavaUtils javaUtils = JavaUtils.INSTANCE.acceptIfHasText(getHeaderIfAvailable(headers, AmqpHeaders.APP_ID, String.class), amqpMessageProperties::setAppId).acceptIfHasText(getHeaderIfAvailable(headers, AmqpHeaders.CLUSTER_ID, String.class), amqpMessageProperties::setClusterId).acceptIfHasText(getHeaderIfAvailable(headers, AmqpHeaders.CONTENT_ENCODING, String.class), amqpMessageProperties::setContentEncoding).acceptIfNotNull(getHeaderIfAvailable(headers, AmqpHeaders.CONTENT_LENGTH, Long.class), amqpMessageProperties::setContentLength).acceptIfHasText(extractContentTypeAsString(headers), amqpMessageProperties::setContentType);
    Object correlationId = headers.get(AmqpHeaders.CORRELATION_ID);
    if (correlationId instanceof String) {
        amqpMessageProperties.setCorrelationId((String) correlationId);
    }
    javaUtils.acceptIfNotNull(getHeaderIfAvailable(headers, AmqpHeaders.DELAY, Integer.class), amqpMessageProperties::setDelay).acceptIfNotNull(getHeaderIfAvailable(headers, AmqpHeaders.DELIVERY_MODE, MessageDeliveryMode.class), amqpMessageProperties::setDeliveryMode).acceptIfNotNull(getHeaderIfAvailable(headers, AmqpHeaders.DELIVERY_TAG, Long.class), amqpMessageProperties::setDeliveryTag).acceptIfHasText(getHeaderIfAvailable(headers, AmqpHeaders.EXPIRATION, String.class), amqpMessageProperties::setExpiration).acceptIfNotNull(getHeaderIfAvailable(headers, AmqpHeaders.MESSAGE_COUNT, Integer.class), amqpMessageProperties::setMessageCount).acceptIfHasText(getHeaderIfAvailable(headers, AmqpHeaders.MESSAGE_ID, String.class), amqpMessageProperties::setMessageId).acceptIfNotNull(getHeaderIfAvailable(headers, AmqpMessageHeaderAccessor.PRIORITY, Integer.class), amqpMessageProperties::setPriority).acceptIfHasText(getHeaderIfAvailable(headers, AmqpHeaders.RECEIVED_EXCHANGE, String.class), amqpMessageProperties::setReceivedExchange).acceptIfHasText(getHeaderIfAvailable(headers, AmqpHeaders.RECEIVED_ROUTING_KEY, String.class), amqpMessageProperties::setReceivedRoutingKey).acceptIfNotNull(getHeaderIfAvailable(headers, AmqpHeaders.REDELIVERED, Boolean.class), amqpMessageProperties::setRedelivered).acceptIfNotNull(getHeaderIfAvailable(headers, AmqpHeaders.REPLY_TO, String.class), amqpMessageProperties::setReplyTo).acceptIfNotNull(getHeaderIfAvailable(headers, AmqpHeaders.TIMESTAMP, Date.class), amqpMessageProperties::setTimestamp).acceptIfNotNull(getHeaderIfAvailable(headers, AmqpHeaders.TYPE, String.class), amqpMessageProperties::setType).acceptIfHasText(getHeaderIfAvailable(headers, AmqpHeaders.USER_ID, String.class), amqpMessageProperties::setUserId);
    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);
    }
    // Map custom headers
    for (Map.Entry<String, Object> entry : headers.entrySet()) {
        String headerName = entry.getKey();
        if (StringUtils.hasText(headerName) && !headerName.startsWith(AmqpHeaders.PREFIX)) {
            Object value = entry.getValue();
            if (value != null) {
                String propertyName = this.fromHeaderName(headerName);
                if (!amqpMessageProperties.getHeaders().containsKey(headerName)) {
                    amqpMessageProperties.setHeader(propertyName, value);
                }
            }
        }
    }
}
Also used : JavaUtils(org.springframework.amqp.utils.JavaUtils) Map(java.util.Map) HashMap(java.util.HashMap) Date(java.util.Date) MessageDeliveryMode(org.springframework.amqp.core.MessageDeliveryMode)

Example 4 with JavaUtils

use of org.springframework.amqp.utils.JavaUtils in project spring-amqp by spring-projects.

the class AbstractRabbitListenerContainerFactory method createListenerContainer.

@Override
public C createListenerContainer(RabbitListenerEndpoint endpoint) {
    C instance = createContainerInstance();
    JavaUtils javaUtils = JavaUtils.INSTANCE.acceptIfNotNull(this.connectionFactory, instance::setConnectionFactory).acceptIfNotNull(this.errorHandler, instance::setErrorHandler);
    if (this.messageConverter != null && endpoint != null && endpoint.getMessageConverter() == null) {
        endpoint.setMessageConverter(this.messageConverter);
    }
    Advice[] adviceChain = getAdviceChain();
    javaUtils.acceptIfNotNull(this.acknowledgeMode, instance::setAcknowledgeMode).acceptIfNotNull(this.channelTransacted, instance::setChannelTransacted).acceptIfNotNull(this.applicationContext, instance::setApplicationContext).acceptIfNotNull(this.taskExecutor, instance::setTaskExecutor).acceptIfNotNull(this.transactionManager, instance::setTransactionManager).acceptIfNotNull(this.prefetchCount, instance::setPrefetchCount).acceptIfNotNull(this.globalQos, instance::setGlobalQos).acceptIfNotNull(getDefaultRequeueRejected(), instance::setDefaultRequeueRejected).acceptIfNotNull(adviceChain, instance::setAdviceChain).acceptIfNotNull(this.recoveryBackOff, instance::setRecoveryBackOff).acceptIfNotNull(this.mismatchedQueuesFatal, instance::setMismatchedQueuesFatal).acceptIfNotNull(this.missingQueuesFatal, instance::setMissingQueuesFatal).acceptIfNotNull(this.consumerTagStrategy, instance::setConsumerTagStrategy).acceptIfNotNull(this.idleEventInterval, instance::setIdleEventInterval).acceptIfNotNull(this.failedDeclarationRetryInterval, instance::setFailedDeclarationRetryInterval).acceptIfNotNull(this.applicationEventPublisher, instance::setApplicationEventPublisher).acceptIfNotNull(this.autoStartup, instance::setAutoStartup).acceptIfNotNull(this.phase, instance::setPhase).acceptIfNotNull(this.afterReceivePostProcessors, instance::setAfterReceivePostProcessors).acceptIfNotNull(this.deBatchingEnabled, instance::setDeBatchingEnabled);
    if (this.batchListener && this.deBatchingEnabled == null) {
        // turn off container debatching by default for batch listeners
        instance.setDeBatchingEnabled(false);
    }
    if (endpoint != null) {
        // endpoint settings overriding default factory settings
        javaUtils.acceptIfNotNull(endpoint.getTaskExecutor(), instance::setTaskExecutor).acceptIfNotNull(endpoint.getAckMode(), instance::setAcknowledgeMode).acceptIfNotNull(this.batchingStrategy, endpoint::setBatchingStrategy);
        instance.setListenerId(endpoint.getId());
        endpoint.setBatchListener(this.batchListener);
    }
    applyCommonOverrides(endpoint, instance);
    initializeContainer(instance, endpoint);
    if (this.containerCustomizer != null) {
        this.containerCustomizer.configure(instance);
    }
    return instance;
}
Also used : Advice(org.aopalliance.aop.Advice) JavaUtils(org.springframework.amqp.utils.JavaUtils)

Example 5 with JavaUtils

use of org.springframework.amqp.utils.JavaUtils in project spring-amqp by spring-projects.

the class SimpleRabbitListenerContainerFactory method initializeContainer.

@Override
protected void initializeContainer(SimpleMessageListenerContainer instance, RabbitListenerEndpoint endpoint) {
    super.initializeContainer(instance, endpoint);
    JavaUtils javaUtils = JavaUtils.INSTANCE.acceptIfNotNull(this.batchSize, instance::setBatchSize);
    String concurrency = null;
    if (endpoint != null) {
        concurrency = endpoint.getConcurrency();
        javaUtils.acceptIfNotNull(concurrency, instance::setConcurrency);
    }
    javaUtils.acceptIfCondition(concurrency == null && this.concurrentConsumers != null, this.concurrentConsumers, instance::setConcurrentConsumers).acceptIfCondition((concurrency == null || !(concurrency.contains("-"))) && this.maxConcurrentConsumers != null, this.maxConcurrentConsumers, instance::setMaxConcurrentConsumers).acceptIfNotNull(this.startConsumerMinInterval, instance::setStartConsumerMinInterval).acceptIfNotNull(this.stopConsumerMinInterval, instance::setStopConsumerMinInterval).acceptIfNotNull(this.consecutiveActiveTrigger, instance::setConsecutiveActiveTrigger).acceptIfNotNull(this.consecutiveIdleTrigger, instance::setConsecutiveIdleTrigger).acceptIfNotNull(this.receiveTimeout, instance::setReceiveTimeout);
    if (Boolean.TRUE.equals(this.consumerBatchEnabled)) {
        instance.setConsumerBatchEnabled(true);
        /*
			 * 'batchListener=true' turns off container debatching by default, it must be
			 * true when consumer batching is enabled.
			 */
        instance.setDeBatchingEnabled(true);
    }
}
Also used : JavaUtils(org.springframework.amqp.utils.JavaUtils)

Aggregations

JavaUtils (org.springframework.amqp.utils.JavaUtils)5 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Date (java.util.Date)1 Advice (org.aopalliance.aop.Advice)1 MessageDeliveryMode (org.springframework.amqp.core.MessageDeliveryMode)1 MessageHeaders (org.springframework.messaging.MessageHeaders)1