Search in sources :

Example 1 with DefaultSpringErrorHandler

use of org.apache.camel.component.jms.DefaultSpringErrorHandler in project camel by apache.

the class QueueReplyManager method createListenerContainer.

protected AbstractMessageListenerContainer createListenerContainer() throws Exception {
    DefaultMessageListenerContainer answer;
    ReplyToType type = endpoint.getConfiguration().getReplyToType();
    if (type == null) {
        // use shared by default for reply queues
        type = ReplyToType.Shared;
    }
    if (ReplyToType.Shared == type) {
        // shared reply to queues support either a fixed or dynamic JMS message selector
        String replyToSelectorName = endpoint.getReplyToDestinationSelectorName();
        if (replyToSelectorName != null) {
            // create a random selector value we will use for the reply queue
            replyToSelectorValue = "ID:" + new BigInteger(24 * 8, new Random()).toString(16);
            String fixedMessageSelector = replyToSelectorName + "='" + replyToSelectorValue + "'";
            answer = new SharedQueueMessageListenerContainer(endpoint, fixedMessageSelector);
            // must use cache level consumer for fixed message selector
            answer.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER);
            log.debug("Using shared queue: " + endpoint.getReplyTo() + " with fixed message selector [" + fixedMessageSelector + "] as reply listener: " + answer);
        } else {
            // use a dynamic message selector which will select the message we want to receive as reply
            dynamicMessageSelector = new MessageSelectorCreator(correlation);
            answer = new SharedQueueMessageListenerContainer(endpoint, dynamicMessageSelector);
            // must use cache level session for dynamic message selector,
            // as otherwise the dynamic message selector will not be updated on-the-fly
            answer.setCacheLevel(DefaultMessageListenerContainer.CACHE_SESSION);
            log.debug("Using shared queue: " + endpoint.getReplyTo() + " with dynamic message selector as reply listener: " + answer);
        }
        // shared is not as fast as temporary or exclusive, so log this so the end user may be aware of this
        log.warn("{} is using a shared reply queue, which is not as fast as alternatives." + " See more detail at the section 'Request-reply over JMS' at http://camel.apache.org/jms", endpoint);
    } else if (ReplyToType.Exclusive == type) {
        answer = new ExclusiveQueueMessageListenerContainer(endpoint);
        // must use cache level consumer for exclusive as there is no message selector
        answer.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER);
        log.debug("Using exclusive queue:" + endpoint.getReplyTo() + " as reply listener: " + answer);
    } else {
        throw new IllegalArgumentException("ReplyToType " + type + " is not supported for reply queues");
    }
    String replyToCacheLevelName = endpoint.getConfiguration().getReplyToCacheLevelName();
    if (replyToCacheLevelName != null) {
        answer.setCacheLevelName(replyToCacheLevelName);
        log.debug("Setting the replyCacheLevel to be " + replyToCacheLevelName);
    }
    DestinationResolver resolver = endpoint.getDestinationResolver();
    if (resolver == null) {
        resolver = answer.getDestinationResolver();
    }
    answer.setDestinationResolver(new DestinationResolverDelegate(resolver));
    answer.setDestinationName(endpoint.getReplyTo());
    answer.setAutoStartup(true);
    answer.setIdleConsumerLimit(endpoint.getIdleConsumerLimit());
    answer.setIdleTaskExecutionLimit(endpoint.getIdleTaskExecutionLimit());
    if (endpoint.getMaxMessagesPerTask() >= 0) {
        answer.setMaxMessagesPerTask(endpoint.getMaxMessagesPerTask());
    }
    answer.setMessageListener(this);
    answer.setPubSubDomain(false);
    answer.setSubscriptionDurable(false);
    answer.setConcurrentConsumers(endpoint.getReplyToConcurrentConsumers());
    if (endpoint.getReplyToMaxConcurrentConsumers() > 0) {
        answer.setMaxConcurrentConsumers(endpoint.getReplyToMaxConcurrentConsumers());
    }
    answer.setConnectionFactory(endpoint.getConnectionFactory());
    String clientId = endpoint.getClientId();
    if (clientId != null) {
        clientId += ".CamelReplyManager";
        answer.setClientId(clientId);
    }
    // we cannot do request-reply over JMS with transaction
    answer.setSessionTransacted(false);
    // other optional properties
    if (endpoint.getExceptionListener() != null) {
        answer.setExceptionListener(endpoint.getExceptionListener());
    }
    if (endpoint.getErrorHandler() != null) {
        answer.setErrorHandler(endpoint.getErrorHandler());
    } else {
        answer.setErrorHandler(new DefaultSpringErrorHandler(endpoint.getCamelContext(), QueueReplyManager.class, endpoint.getErrorHandlerLoggingLevel(), endpoint.isErrorHandlerLogStackTrace()));
    }
    if (endpoint.getReceiveTimeout() >= 0) {
        answer.setReceiveTimeout(endpoint.getReceiveTimeout());
    }
    if (endpoint.getRecoveryInterval() >= 0) {
        answer.setRecoveryInterval(endpoint.getRecoveryInterval());
    }
    // set task executor
    if (endpoint.getTaskExecutor() != null) {
        if (log.isDebugEnabled()) {
            log.debug("Using custom TaskExecutor: {} on listener container: {}", endpoint.getTaskExecutor(), answer);
        }
        answer.setTaskExecutor(endpoint.getTaskExecutor());
    }
    // setup a bean name which is used by Spring JMS as the thread name
    String name = "QueueReplyManager[" + answer.getDestinationName() + "]";
    answer.setBeanName(name);
    if (answer.getConcurrentConsumers() > 1) {
        if (ReplyToType.Shared == type) {
            // warn if using concurrent consumer with shared reply queue as that may not work properly
            log.warn("Using {}-{} concurrent consumer on {} with shared queue {} may not work properly with all message brokers.", new Object[] { answer.getConcurrentConsumers(), answer.getMaxConcurrentConsumers(), name, endpoint.getReplyTo() });
        } else {
            // log that we are using concurrent consumers
            log.info("Using {}-{} concurrent consumers on {}", new Object[] { answer.getConcurrentConsumers(), answer.getMaxConcurrentConsumers(), name });
        }
    }
    return answer;
}
Also used : DestinationResolver(org.springframework.jms.support.destination.DestinationResolver) ReplyToType(org.apache.camel.component.jms.ReplyToType) DefaultSpringErrorHandler(org.apache.camel.component.jms.DefaultSpringErrorHandler) DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) Random(java.util.Random) BigInteger(java.math.BigInteger)

Example 2 with DefaultSpringErrorHandler

use of org.apache.camel.component.jms.DefaultSpringErrorHandler in project camel by apache.

the class TemporaryQueueReplyManager method createListenerContainer.

@Override
protected AbstractMessageListenerContainer createListenerContainer() throws Exception {
    // Use DefaultMessageListenerContainer as it supports reconnects (see CAMEL-3193)
    DefaultMessageListenerContainer answer = new DefaultJmsMessageListenerContainer(endpoint, endpoint.isAllowReplyManagerQuickStop());
    answer.setDestinationName("temporary");
    answer.setDestinationResolver(destResolver);
    answer.setAutoStartup(true);
    if (endpoint.getMaxMessagesPerTask() >= 0) {
        answer.setMaxMessagesPerTask(endpoint.getMaxMessagesPerTask());
    }
    answer.setIdleConsumerLimit(endpoint.getIdleConsumerLimit());
    answer.setIdleTaskExecutionLimit(endpoint.getIdleTaskExecutionLimit());
    answer.setMessageListener(this);
    answer.setPubSubDomain(false);
    answer.setSubscriptionDurable(false);
    answer.setConcurrentConsumers(endpoint.getReplyToConcurrentConsumers());
    if (endpoint.getReplyToMaxConcurrentConsumers() > 0) {
        answer.setMaxConcurrentConsumers(endpoint.getReplyToMaxConcurrentConsumers());
    }
    answer.setConnectionFactory(endpoint.getConnectionFactory());
    // msgs from the JMS Connection that created the temp destination in the first place
    if (endpoint.getReplyToCacheLevelName() != null) {
        if ("CACHE_NONE".equals(endpoint.getReplyToCacheLevelName())) {
            throw new IllegalArgumentException("ReplyToCacheLevelName cannot be CACHE_NONE when using temporary reply queues. The value must be either CACHE_CONSUMER, or CACHE_SESSION");
        }
        answer.setCacheLevelName(endpoint.getReplyToCacheLevelName());
    } else {
        answer.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER);
    }
    String clientId = endpoint.getClientId();
    if (clientId != null) {
        clientId += ".CamelReplyManager";
        answer.setClientId(clientId);
    }
    // we cannot do request-reply over JMS with transaction
    answer.setSessionTransacted(false);
    // other optional properties
    answer.setExceptionListener(new TemporaryReplyQueueExceptionListener(destResolver, endpoint.getExceptionListener()));
    if (endpoint.getErrorHandler() != null) {
        answer.setErrorHandler(endpoint.getErrorHandler());
    } else {
        answer.setErrorHandler(new DefaultSpringErrorHandler(endpoint.getCamelContext(), TemporaryQueueReplyManager.class, endpoint.getErrorHandlerLoggingLevel(), endpoint.isErrorHandlerLogStackTrace()));
    }
    if (endpoint.getReceiveTimeout() >= 0) {
        answer.setReceiveTimeout(endpoint.getReceiveTimeout());
    }
    if (endpoint.getRecoveryInterval() >= 0) {
        answer.setRecoveryInterval(endpoint.getRecoveryInterval());
    }
    if (endpoint.getTaskExecutor() != null) {
        if (log.isDebugEnabled()) {
            log.debug("Using custom TaskExecutor: {} on listener container: {}", endpoint.getTaskExecutor(), answer);
        }
        answer.setTaskExecutor(endpoint.getTaskExecutor());
    }
    // setup a bean name which is used by Spring JMS as the thread name
    // use the name of the request destination
    String name = "TemporaryQueueReplyManager[" + endpoint.getDestinationName() + "]";
    answer.setBeanName(name);
    if (answer.getConcurrentConsumers() > 1) {
        // log that we are using concurrent consumers
        log.info("Using {}-{} concurrent consumers on {}", new Object[] { answer.getConcurrentConsumers(), answer.getMaxConcurrentConsumers(), name });
    }
    return answer;
}
Also used : DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) DefaultJmsMessageListenerContainer(org.apache.camel.component.jms.DefaultJmsMessageListenerContainer) DefaultSpringErrorHandler(org.apache.camel.component.jms.DefaultSpringErrorHandler)

Aggregations

DefaultSpringErrorHandler (org.apache.camel.component.jms.DefaultSpringErrorHandler)2 DefaultMessageListenerContainer (org.springframework.jms.listener.DefaultMessageListenerContainer)2 BigInteger (java.math.BigInteger)1 Random (java.util.Random)1 DefaultJmsMessageListenerContainer (org.apache.camel.component.jms.DefaultJmsMessageListenerContainer)1 ReplyToType (org.apache.camel.component.jms.ReplyToType)1 DestinationResolver (org.springframework.jms.support.destination.DestinationResolver)1