001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.jms;
018
019import javax.jms.ConnectionFactory;
020
021import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
022import org.springframework.jms.support.converter.MessageConverter;
023import org.springframework.jms.support.destination.DestinationResolver;
024import org.springframework.transaction.jta.JtaTransactionManager;
025import org.springframework.util.Assert;
026
027/**
028 * Configure {@link DefaultJmsListenerContainerFactory} with sensible defaults.
029 *
030 * @author Stephane Nicoll
031 * @since 1.3.3
032 */
033public final class DefaultJmsListenerContainerFactoryConfigurer {
034
035        private DestinationResolver destinationResolver;
036
037        private MessageConverter messageConverter;
038
039        private JtaTransactionManager transactionManager;
040
041        private JmsProperties jmsProperties;
042
043        /**
044         * Set the {@link DestinationResolver} to use or {@code null} if no destination
045         * resolver should be associated with the factory by default.
046         * @param destinationResolver the {@link DestinationResolver}
047         */
048        void setDestinationResolver(DestinationResolver destinationResolver) {
049                this.destinationResolver = destinationResolver;
050        }
051
052        /**
053         * Set the {@link MessageConverter} to use or {@code null} if the out-of-the-box
054         * converter should be used.
055         * @param messageConverter the {@link MessageConverter}
056         */
057        void setMessageConverter(MessageConverter messageConverter) {
058                this.messageConverter = messageConverter;
059        }
060
061        /**
062         * Set the {@link JtaTransactionManager} to use or {@code null} if the JTA support
063         * should not be used.
064         * @param transactionManager the {@link JtaTransactionManager}
065         */
066        void setTransactionManager(JtaTransactionManager transactionManager) {
067                this.transactionManager = transactionManager;
068        }
069
070        /**
071         * Set the {@link JmsProperties} to use.
072         * @param jmsProperties the {@link JmsProperties}
073         */
074        void setJmsProperties(JmsProperties jmsProperties) {
075                this.jmsProperties = jmsProperties;
076        }
077
078        /**
079         * Configure the specified jms listener container factory. The factory can be further
080         * tuned and default settings can be overridden.
081         * @param factory the {@link DefaultJmsListenerContainerFactory} instance to configure
082         * @param connectionFactory the {@link ConnectionFactory} to use
083         */
084        public void configure(DefaultJmsListenerContainerFactory factory,
085                        ConnectionFactory connectionFactory) {
086                Assert.notNull(factory, "Factory must not be null");
087                Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
088                factory.setConnectionFactory(connectionFactory);
089                factory.setPubSubDomain(this.jmsProperties.isPubSubDomain());
090                if (this.transactionManager != null) {
091                        factory.setTransactionManager(this.transactionManager);
092                }
093                else {
094                        factory.setSessionTransacted(true);
095                }
096                if (this.destinationResolver != null) {
097                        factory.setDestinationResolver(this.destinationResolver);
098                }
099                if (this.messageConverter != null) {
100                        factory.setMessageConverter(this.messageConverter);
101                }
102                JmsProperties.Listener listener = this.jmsProperties.getListener();
103                factory.setAutoStartup(listener.isAutoStartup());
104                if (listener.getAcknowledgeMode() != null) {
105                        factory.setSessionAcknowledgeMode(listener.getAcknowledgeMode().getMode());
106                }
107                String concurrency = listener.formatConcurrency();
108                if (concurrency != null) {
109                        factory.setConcurrency(concurrency);
110                }
111        }
112
113}