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