|
| 1 | +/* |
| 2 | + * Copyright 2012-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.jms.autoconfigure; |
| 18 | + |
| 19 | +import io.micrometer.observation.ObservationRegistry; |
| 20 | +import jakarta.jms.ConnectionFactory; |
| 21 | +import jakarta.jms.ExceptionListener; |
| 22 | +import org.jspecify.annotations.Nullable; |
| 23 | + |
| 24 | +import org.springframework.boot.context.properties.PropertyMapper; |
| 25 | +import org.springframework.boot.jms.autoconfigure.JmsProperties.Listener.Session; |
| 26 | +import org.springframework.jms.config.AbstractJmsListenerContainerFactory; |
| 27 | +import org.springframework.jms.config.JmsListenerContainerFactory; |
| 28 | +import org.springframework.jms.support.converter.MessageConverter; |
| 29 | +import org.springframework.jms.support.destination.DestinationResolver; |
| 30 | +import org.springframework.util.Assert; |
| 31 | + |
| 32 | +/** |
| 33 | + * Configure common {@link JmsListenerContainerFactory} settings with sensible defaults. |
| 34 | + * <p> |
| 35 | + * This includes: |
| 36 | + * <li>A {@link DestinationResolver} is such a component is present.</li> |
| 37 | + * <li>A {@link MessageConverter} is such a component is present.</li> |
| 38 | + * <li>An {@link ExceptionListener} is such a component is present.</li> |
| 39 | + * <li>An {@link ObservationRegistry} is such a component is present.</li> |
| 40 | + * <li>Configuration properties of the {@code spring.jms} namespace that are common to all |
| 41 | + * implementations.</li> |
| 42 | + * |
| 43 | + * @param <T> the connection factory type. |
| 44 | + * @author Stephane Nicoll |
| 45 | + * @author Eddú Meléndez |
| 46 | + * @author Vedran Pavic |
| 47 | + * @author Lasse Wulff |
| 48 | + * @since 4.1.0 |
| 49 | + */ |
| 50 | +public abstract class AbstractJmsListenerContainerFactoryConfigurer<T extends AbstractJmsListenerContainerFactory<?>> { |
| 51 | + |
| 52 | + private @Nullable DestinationResolver destinationResolver; |
| 53 | + |
| 54 | + private @Nullable MessageConverter messageConverter; |
| 55 | + |
| 56 | + private @Nullable ExceptionListener exceptionListener; |
| 57 | + |
| 58 | + private @Nullable ObservationRegistry observationRegistry; |
| 59 | + |
| 60 | + private @Nullable JmsProperties jmsProperties; |
| 61 | + |
| 62 | + /** |
| 63 | + * Set the {@link DestinationResolver} to use or {@code null} if no destination |
| 64 | + * resolver should be associated with the factory by default. |
| 65 | + * @param destinationResolver the {@link DestinationResolver} |
| 66 | + */ |
| 67 | + void setDestinationResolver(@Nullable DestinationResolver destinationResolver) { |
| 68 | + this.destinationResolver = destinationResolver; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Set the {@link MessageConverter} to use or {@code null} if the out-of-the-box |
| 73 | + * converter should be used. |
| 74 | + * @param messageConverter the {@link MessageConverter} |
| 75 | + */ |
| 76 | + void setMessageConverter(@Nullable MessageConverter messageConverter) { |
| 77 | + this.messageConverter = messageConverter; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Set the {@link ExceptionListener} to use or {@code null} if no exception listener |
| 82 | + * should be associated by default. |
| 83 | + * @param exceptionListener the {@link ExceptionListener} |
| 84 | + */ |
| 85 | + void setExceptionListener(@Nullable ExceptionListener exceptionListener) { |
| 86 | + this.exceptionListener = exceptionListener; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Set the {@link JmsProperties} to use. |
| 91 | + * @param jmsProperties the {@link JmsProperties} |
| 92 | + */ |
| 93 | + void setJmsProperties(@Nullable JmsProperties jmsProperties) { |
| 94 | + this.jmsProperties = jmsProperties; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Set the {@link ObservationRegistry} to use. |
| 99 | + * @param observationRegistry the {@link ObservationRegistry} |
| 100 | + */ |
| 101 | + void setObservationRegistry(@Nullable ObservationRegistry observationRegistry) { |
| 102 | + this.observationRegistry = observationRegistry; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Return the {@link JmsProperties}. |
| 107 | + * @return the jms properties |
| 108 | + */ |
| 109 | + protected JmsProperties getJmsProperties() { |
| 110 | + Assert.state(this.jmsProperties != null, "'jmsProperties' must not be null"); |
| 111 | + return this.jmsProperties; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Configure the specified jms listener container factory. The factory can be further |
| 116 | + * tuned and default settings can be overridden. |
| 117 | + * @param factory the {@link AbstractJmsListenerContainerFactory} instance to |
| 118 | + * configure |
| 119 | + * @param connectionFactory the {@link ConnectionFactory} to use |
| 120 | + */ |
| 121 | + public void configure(T factory, ConnectionFactory connectionFactory) { |
| 122 | + Assert.notNull(factory, "'factory' must not be null"); |
| 123 | + Assert.notNull(connectionFactory, "'connectionFactory' must not be null"); |
| 124 | + JmsProperties properties = getJmsProperties(); |
| 125 | + JmsProperties.Listener listenerProperties = properties.getListener(); |
| 126 | + Session sessionProperties = listenerProperties.getSession(); |
| 127 | + factory.setConnectionFactory(connectionFactory); |
| 128 | + PropertyMapper map = PropertyMapper.get(); |
| 129 | + map.from(properties::isPubSubDomain).to(factory::setPubSubDomain); |
| 130 | + map.from(properties::isSubscriptionDurable).to(factory::setSubscriptionDurable); |
| 131 | + map.from(properties::getClientId).to(factory::setClientId); |
| 132 | + map.from(this.destinationResolver).to(factory::setDestinationResolver); |
| 133 | + map.from(this.messageConverter).to(factory::setMessageConverter); |
| 134 | + map.from(this.exceptionListener).to(factory::setExceptionListener); |
| 135 | + map.from(sessionProperties.getAcknowledgeMode()::getMode).to(factory::setSessionAcknowledgeMode); |
| 136 | + map.from(this.observationRegistry).to(factory::setObservationRegistry); |
| 137 | + map.from(sessionProperties::getTransacted).to(factory::setSessionTransacted); |
| 138 | + map.from(listenerProperties::isAutoStartup).to(factory::setAutoStartup); |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments