-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add JmsTemplate overloads to JMS DSL channel specs #10933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
|
|
||
| package org.springframework.integration.jms.config; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.Executor; | ||
|
|
||
|
|
@@ -55,6 +56,7 @@ | |
| * @author Oleg Zhurakousky | ||
| * @author Gary Russell | ||
| * @author Artem Bilan | ||
| * @author Glenn Renfro | ||
| * | ||
| * @since 2.0 | ||
| */ | ||
|
|
@@ -68,7 +70,11 @@ public class JmsChannelFactoryBean extends AbstractFactoryBean<AbstractJmsChanne | |
|
|
||
| private final boolean messageDriven; | ||
|
|
||
| private final JmsTemplate jmsTemplate = new DynamicJmsTemplate(); | ||
| private JmsTemplate jmsTemplate = new DynamicJmsTemplate(); | ||
|
|
||
| private boolean jmsTemplateExplicitlySet; | ||
|
|
||
| private final List<String> templateDelegatingOptions = new ArrayList<>(); | ||
|
|
||
| private @Nullable Class<? extends AbstractMessageListenerContainer> containerType; | ||
|
|
||
|
|
@@ -164,30 +170,37 @@ public void setInterceptors(List<ChannelInterceptor> interceptors) { | |
| */ | ||
|
|
||
| public void setDeliveryPersistent(boolean deliveryPersistent) { | ||
| recordJmsTemplateOption("deliveryPersistent"); | ||
| this.jmsTemplate.setDeliveryPersistent(deliveryPersistent); | ||
| } | ||
|
|
||
| public void setExplicitQosEnabled(boolean explicitQosEnabled) { | ||
| recordJmsTemplateOption("explicitQosEnabled"); | ||
| this.jmsTemplate.setExplicitQosEnabled(explicitQosEnabled); | ||
| } | ||
|
|
||
| public void setMessageConverter(MessageConverter messageConverter) { | ||
| recordJmsTemplateOption("messageConverter"); | ||
| this.jmsTemplate.setMessageConverter(messageConverter); | ||
| } | ||
|
|
||
| public void setMessageIdEnabled(boolean messageIdEnabled) { | ||
| recordJmsTemplateOption("messageIdEnabled"); | ||
| this.jmsTemplate.setMessageIdEnabled(messageIdEnabled); | ||
| } | ||
|
|
||
| public void setMessageTimestampEnabled(boolean messageTimestampEnabled) { | ||
| recordJmsTemplateOption("messageTimestampEnabled"); | ||
| this.jmsTemplate.setMessageTimestampEnabled(messageTimestampEnabled); | ||
| } | ||
|
|
||
| public void setPriority(int priority) { | ||
| recordJmsTemplateOption("priority"); | ||
| this.jmsTemplate.setPriority(priority); | ||
| } | ||
|
|
||
| public void setTimeToLive(long timeToLive) { | ||
| recordJmsTemplateOption("timeToLive"); | ||
| this.jmsTemplate.setTimeToLive(timeToLive); | ||
| } | ||
|
|
||
|
|
@@ -236,6 +249,7 @@ public void setConcurrentConsumers(int concurrentConsumers) { | |
|
|
||
| public void setConnectionFactory(ConnectionFactory connectionFactory) { | ||
| this.connectionFactory = connectionFactory; | ||
| recordJmsTemplateOption("connectionFactory"); | ||
| this.jmsTemplate.setConnectionFactory(this.connectionFactory); | ||
| } | ||
|
|
||
|
|
@@ -254,6 +268,7 @@ public void setDestinationName(String destinationName) { | |
|
|
||
| public void setDestinationResolver(DestinationResolver destinationResolver) { | ||
| this.destinationResolver = destinationResolver; | ||
| recordJmsTemplateOption("destinationResolver"); | ||
| this.jmsTemplate.setDestinationResolver(destinationResolver); | ||
| } | ||
|
|
||
|
|
@@ -302,16 +317,19 @@ public void setPhase(int phase) { | |
|
|
||
| public void setPubSubDomain(boolean pubSubDomain) { | ||
| this.pubSubDomain = pubSubDomain; | ||
| recordJmsTemplateOption("pubSubDomain"); | ||
| this.jmsTemplate.setPubSubDomain(pubSubDomain); | ||
| } | ||
|
|
||
| public void setPubSubNoLocal(boolean pubSubNoLocal) { | ||
| this.pubSubNoLocal = pubSubNoLocal; | ||
| recordJmsTemplateOption("pubSubNoLocal"); | ||
| this.jmsTemplate.setPubSubNoLocal(pubSubNoLocal); | ||
| } | ||
|
|
||
| public void setReceiveTimeout(long receiveTimeout) { | ||
| this.receiveTimeout = receiveTimeout; | ||
| recordJmsTemplateOption("receiveTimeout"); | ||
| this.jmsTemplate.setReceiveTimeout(receiveTimeout); | ||
| } | ||
|
|
||
|
|
@@ -322,11 +340,30 @@ public void setRecoveryInterval(long recoveryInterval) { | |
|
|
||
| public void setSessionAcknowledgeMode(int sessionAcknowledgeMode) { | ||
| this.sessionAcknowledgeMode = sessionAcknowledgeMode; | ||
| recordJmsTemplateOption("sessionAcknowledgeMode"); | ||
| this.jmsTemplate.setSessionAcknowledgeMode(sessionAcknowledgeMode); | ||
| } | ||
|
|
||
| /** | ||
| * The template to be used by the Factory. | ||
| * When an external {@link JmsTemplate} is provided, none of the individual template options | ||
| * (e.g. {@code deliveryPersistent}, {@code connectionFactory}, {@code receiveTimeout}, etc.) | ||
| * may be set on this factory bean — configure them directly on the provided template instead. | ||
| * @param jmsTemplate the {@link JmsTemplate} to build on | ||
| * @since 7.1 | ||
| */ | ||
| public void setJmsTemplate(JmsTemplate jmsTemplate) { | ||
| this.jmsTemplate = jmsTemplate; | ||
| this.jmsTemplateExplicitlySet = true; | ||
| } | ||
|
|
||
| private void recordJmsTemplateOption(String option) { | ||
| this.templateDelegatingOptions.add(option); | ||
| } | ||
|
|
||
| public void setSessionTransacted(boolean sessionTransacted) { | ||
| this.sessionTransacted = sessionTransacted; | ||
| recordJmsTemplateOption("sessionTransacted"); | ||
| this.jmsTemplate.setSessionTransacted(sessionTransacted); | ||
| } | ||
|
|
||
|
|
@@ -406,6 +443,15 @@ protected AbstractJmsChannel createInstance() { | |
| private void initializeJmsTemplate() { | ||
| Assert.isTrue(this.destination != null ^ this.destinationName != null, | ||
| "Exactly one of destination or destinationName is required."); | ||
| Assert.isTrue(!this.jmsTemplateExplicitlySet || this.templateDelegatingOptions.isEmpty(), | ||
| () -> { | ||
| String quoted = this.templateDelegatingOptions.stream() | ||
| .map(opt -> "'" + opt + "'") | ||
| .reduce((a, b) -> a + ", " + b) | ||
| .orElse(""); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is interesting, but I think we don't need to track options being set. |
||
| return "The following options must be provided on the externally configured JmsTemplate " + | ||
| "instead of on this factory bean: " + quoted; | ||
| }); | ||
| JavaUtils.INSTANCE | ||
| .acceptIfNotNull(this.destination, this.jmsTemplate::setDefaultDestination) | ||
| .acceptIfNotNull(this.destinationName, this.jmsTemplate::setDefaultDestinationName); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright 2026-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.integration.jms.config; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import org.springframework.beans.factory.support.StaticListableBeanFactory; | ||
| import org.springframework.jms.core.JmsTemplate; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
|
|
||
| /** | ||
| * @author Glenn Renfro | ||
| */ | ||
|
|
||
| class JmsChannelFactoryBeanTests { | ||
|
|
||
| @Test | ||
| void optionSetBeforeExternalTemplateAlsoThrows() { | ||
| JmsChannelFactoryBean fb = new JmsChannelFactoryBean(false); | ||
| fb.setBeanName("testChannel"); | ||
| fb.setDestinationName("testQueue"); | ||
| fb.setSessionTransacted(true); | ||
| fb.setJmsTemplate(new JmsTemplate()); | ||
|
|
||
| assertThatIllegalArgumentException() | ||
| .isThrownBy(fb::createInstance) | ||
| .withMessageContaining("'sessionTransacted'") | ||
| .withMessageContaining("must be provided on the externally configured JmsTemplate"); | ||
| } | ||
|
|
||
| @Test | ||
| void externalTemplateAloneDoesNotThrowGuard() { | ||
| JmsChannelFactoryBean fb = new JmsChannelFactoryBean(false); | ||
| fb.setBeanName("testChannel"); | ||
| fb.setJmsTemplate(new JmsTemplate()); | ||
| fb.setDestinationName("testQueue"); | ||
| fb.setBeanFactory(new StaticListableBeanFactory()); | ||
| assertThat(fb.createInstance()).isNotNull(); | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.