001/*
002 * Copyright 2012-2016 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;
020import javax.jms.Message;
021
022import org.springframework.beans.factory.ObjectProvider;
023import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
024import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
027import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
028import org.springframework.boot.context.properties.EnableConfigurationProperties;
029import org.springframework.context.annotation.Bean;
030import org.springframework.context.annotation.Configuration;
031import org.springframework.context.annotation.Import;
032import org.springframework.jms.core.JmsMessagingTemplate;
033import org.springframework.jms.core.JmsTemplate;
034import org.springframework.jms.support.converter.MessageConverter;
035import org.springframework.jms.support.destination.DestinationResolver;
036
037/**
038 * {@link EnableAutoConfiguration Auto-configuration} for Spring JMS.
039 *
040 * @author Greg Turnquist
041 * @author Stephane Nicoll
042 */
043@Configuration
044@ConditionalOnClass({ Message.class, JmsTemplate.class })
045@ConditionalOnBean(ConnectionFactory.class)
046@EnableConfigurationProperties(JmsProperties.class)
047@Import(JmsAnnotationDrivenConfiguration.class)
048public class JmsAutoConfiguration {
049
050        @Configuration
051        protected static class JmsTemplateConfiguration {
052
053                private final JmsProperties properties;
054
055                private final ObjectProvider<DestinationResolver> destinationResolver;
056
057                private final ObjectProvider<MessageConverter> messageConverter;
058
059                public JmsTemplateConfiguration(JmsProperties properties,
060                                ObjectProvider<DestinationResolver> destinationResolver,
061                                ObjectProvider<MessageConverter> messageConverter) {
062                        this.properties = properties;
063                        this.destinationResolver = destinationResolver;
064                        this.messageConverter = messageConverter;
065                }
066
067                @Bean
068                @ConditionalOnMissingBean
069                @ConditionalOnSingleCandidate(ConnectionFactory.class)
070                public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
071                        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
072                        jmsTemplate.setPubSubDomain(this.properties.isPubSubDomain());
073                        DestinationResolver destinationResolver = this.destinationResolver
074                                        .getIfUnique();
075                        if (destinationResolver != null) {
076                                jmsTemplate.setDestinationResolver(destinationResolver);
077                        }
078                        MessageConverter messageConverter = this.messageConverter.getIfUnique();
079                        if (messageConverter != null) {
080                                jmsTemplate.setMessageConverter(messageConverter);
081                        }
082                        JmsProperties.Template template = this.properties.getTemplate();
083                        if (template.getDefaultDestination() != null) {
084                                jmsTemplate.setDefaultDestinationName(template.getDefaultDestination());
085                        }
086                        if (template.getDeliveryDelay() != null) {
087                                jmsTemplate.setDeliveryDelay(template.getDeliveryDelay());
088                        }
089                        jmsTemplate.setExplicitQosEnabled(template.determineQosEnabled());
090                        if (template.getDeliveryMode() != null) {
091                                jmsTemplate.setDeliveryMode(template.getDeliveryMode().getValue());
092                        }
093                        if (template.getPriority() != null) {
094                                jmsTemplate.setPriority(template.getPriority());
095                        }
096                        if (template.getTimeToLive() != null) {
097                                jmsTemplate.setTimeToLive(template.getTimeToLive());
098                        }
099                        if (template.getReceiveTimeout() != null) {
100                                jmsTemplate.setReceiveTimeout(template.getReceiveTimeout());
101                        }
102                        return jmsTemplate;
103                }
104
105        }
106
107        @ConditionalOnClass(JmsMessagingTemplate.class)
108        @Import(JmsTemplateConfiguration.class)
109        protected static class MessagingTemplateConfiguration {
110
111                @Bean
112                @ConditionalOnMissingBean
113                @ConditionalOnSingleCandidate(JmsTemplate.class)
114                public JmsMessagingTemplate jmsMessagingTemplate(JmsTemplate jmsTemplate) {
115                        return new JmsMessagingTemplate(jmsTemplate);
116                }
117
118        }
119
120}