Skip to content

Commit 0545a7b

Browse files
artembilangaryrussell
authored andcommitted
INT-4519: Improve JMS inbound-c-a receiveTimeout (#2540)
* INT-4519: Improve JMS inbound-c-a receiveTimeout JIRA: https://jira.spring.io/browse/INT-4519 * Modify `DynamicJmsTemplate` to default for `-1` if we get `CachingConnectionFactory` and it is with `cacheConsumers`, otherwise 1 second * Polishing for some JMS XML parsers, in particular remove `receiveTimeout` population **Cherry-pick to 5.0.x** * * Polishing according PR comments * * Properly use a `CachingConnectionFactory` in `JmsTests`
1 parent f9fe709 commit 0545a7b

File tree

7 files changed

+165
-119
lines changed

7 files changed

+165
-119
lines changed

spring-integration-jms/src/main/java/org/springframework/integration/jms/DynamicJmsTemplate.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,11 @@
1616

1717
package org.springframework.integration.jms;
1818

19+
import javax.jms.ConnectionFactory;
20+
21+
import org.springframework.jms.connection.CachingConnectionFactory;
1922
import org.springframework.jms.core.JmsTemplate;
23+
import org.springframework.jms.support.destination.JmsDestinationAccessor;
2024
import org.springframework.util.Assert;
2125

2226
/**
@@ -27,6 +31,30 @@
2731
*/
2832
public class DynamicJmsTemplate extends JmsTemplate {
2933

34+
private static final long NO_CACHING_RECEIVE_TIMEOUT = 1000L;
35+
36+
private boolean receiveTimeoutExplicitlySet;
37+
38+
@Override
39+
public void setReceiveTimeout(long receiveTimeout) {
40+
super.setReceiveTimeout(receiveTimeout);
41+
this.receiveTimeoutExplicitlySet = true;
42+
}
43+
44+
@Override
45+
public void setConnectionFactory(ConnectionFactory connectionFactory) {
46+
super.setConnectionFactory(connectionFactory);
47+
if (!this.receiveTimeoutExplicitlySet) {
48+
if (connectionFactory instanceof CachingConnectionFactory &&
49+
((CachingConnectionFactory) connectionFactory).isCacheConsumers()) {
50+
super.setReceiveTimeout(JmsDestinationAccessor.RECEIVE_TIMEOUT_NO_WAIT);
51+
}
52+
else {
53+
super.setReceiveTimeout(NO_CACHING_RECEIVE_TIMEOUT);
54+
}
55+
}
56+
}
57+
3058
@Override
3159
public int getPriority() {
3260
Integer priority = DynamicJmsTemplateProperties.getPriority();

spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsInboundChannelAdapterParser.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ protected BeanMetadataElement parseSource(Element element, ParserContext parserC
5050
String jmsTemplate = element.getAttribute(JmsParserUtils.JMS_TEMPLATE_ATTRIBUTE);
5151
String destination = element.getAttribute(JmsParserUtils.DESTINATION_ATTRIBUTE);
5252
String destinationName = element.getAttribute(JmsParserUtils.DESTINATION_NAME_ATTRIBUTE);
53-
String headerMapper = element.getAttribute(JmsParserUtils.HEADER_MAPPER_ATTRIBUTE);
5453
boolean hasJmsTemplate = StringUtils.hasText(jmsTemplate);
5554
boolean hasDestinationRef = StringUtils.hasText(destination);
5655
boolean hasDestinationName = StringUtils.hasText(destinationName);
@@ -80,9 +79,7 @@ else if (!hasJmsTemplate) {
8079
+ JmsParserUtils.DESTINATION_NAME_ATTRIBUTE +
8180
"' attributes must be provided for a polling JMS adapter", parserContext.extractSource(element));
8281
}
83-
if (StringUtils.hasText(headerMapper)) {
84-
builder.addPropertyReference(JmsParserUtils.HEADER_MAPPER_PROPERTY, headerMapper);
85-
}
82+
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, JmsParserUtils.HEADER_MAPPER_ATTRIBUTE);
8683
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "selector", "messageSelector");
8784
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-payload");
8885
return builder.getBeanDefinition();

spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsOutboundChannelAdapterParser.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
*
3333
* @author Mark Fisher
3434
* @author Gary Russell
35+
* @author Artem Bilan
3536
*/
3637
public class JmsOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser {
3738

@@ -42,7 +43,6 @@ protected AbstractBeanDefinition parseConsumer(Element element, ParserContext pa
4243
String destination = element.getAttribute(JmsParserUtils.DESTINATION_ATTRIBUTE);
4344
String destinationName = element.getAttribute(JmsParserUtils.DESTINATION_NAME_ATTRIBUTE);
4445
String destinationExpression = element.getAttribute(JmsParserUtils.DESTINATION_EXPRESSION_ATTRIBUTE);
45-
String headerMapper = element.getAttribute(JmsParserUtils.HEADER_MAPPER_ATTRIBUTE);
4646
boolean hasJmsTemplate = StringUtils.hasText(jmsTemplate);
4747
boolean hasDestinationRef = StringUtils.hasText(destination);
4848
boolean hasDestinationName = StringUtils.hasText(destinationName);
@@ -79,9 +79,8 @@ else if (!hasJmsTemplate) {
7979
JmsParserUtils.DESTINATION_EXPRESSION_ATTRIBUTE +
8080
"' attributes must be provided", parserContext.extractSource(element));
8181
}
82-
if (StringUtils.hasText(headerMapper)) {
83-
builder.addPropertyReference(JmsParserUtils.HEADER_MAPPER_PROPERTY, headerMapper);
84-
}
82+
83+
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, JmsParserUtils.HEADER_MAPPER_ATTRIBUTE);
8584
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-payload");
8685
return builder.getBeanDefinition();
8786
}

spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsParserUtils.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,14 +23,14 @@
2323
import org.springframework.beans.factory.xml.ParserContext;
2424
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
2525
import org.springframework.integration.jms.DynamicJmsTemplate;
26-
import org.springframework.jms.core.JmsTemplate;
2726
import org.springframework.util.StringUtils;
2827

2928
/**
3029
* Utility methods and constants for JMS adapter parsers.
3130
*
3231
* @author Mark Fisher
3332
* @author Gary Russell
33+
* @author Artem Bilan
3434
*/
3535
abstract class JmsParserUtils {
3636

@@ -63,7 +63,7 @@ abstract class JmsParserUtils {
6363
static final String HEADER_MAPPER_PROPERTY = "headerMapper";
6464

6565
private static final String[] JMS_TEMPLATE_ATTRIBUTES = {
66-
"connection-factory", "message-converter", "destination-resolver", "pub-sub-domain",
66+
CONNECTION_FACTORY_ATTRIBUTE, "message-converter", "destination-resolver", PUB_SUB_DOMAIN_ATTRIBUTE,
6767
"time-to-live", "priority", "delivery-persistent", "explicit-qos-enabled", "acknowledge",
6868
"receive-timeout", "session-transacted"
6969
};
@@ -86,18 +86,12 @@ static BeanDefinition parseJmsTemplateBeanDefinition(Element element, ParserCont
8686
determineConnectionFactoryBeanName(element, parserContext));
8787
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converter");
8888
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "destination-resolver");
89-
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "pub-sub-domain");
89+
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, PUB_SUB_DOMAIN_ATTRIBUTE);
9090
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "time-to-live");
9191
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "priority");
9292
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "delivery-persistent");
9393
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "explicit-qos-enabled");
94-
String receiveTimeout = element.getAttribute("receive-timeout");
95-
if (StringUtils.hasText(receiveTimeout)) {
96-
builder.addPropertyValue("receiveTimeout", receiveTimeout);
97-
}
98-
else {
99-
builder.addPropertyValue("receiveTimeout", JmsTemplate.RECEIVE_TIMEOUT_NO_WAIT);
100-
}
94+
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "receive-timeout");
10195
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "session-transacted");
10296
return builder.getBeanDefinition();
10397
}

0 commit comments

Comments
 (0)