Merge pull request #339 from garyrussell/INT-2414

INT-2414 Error When container-class Supplied
This commit is contained in:
Mark Fisher
2012-01-30 10:08:13 -05:00
3 changed files with 80 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 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.
@@ -24,7 +24,6 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.AbstractChannelParser;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
@@ -33,6 +32,7 @@ import org.springframework.util.StringUtils;
*
* @author Mark Fisher
* @author Oleg Zhurakusky
* @author Gary Russell
* @since 2.0
*/
public class JmsChannelParser extends AbstractChannelParser {
@@ -65,20 +65,23 @@ public class JmsChannelParser extends AbstractChannelParser {
}
String containerType = element.getAttribute(CONTAINER_TYPE_ATTRIBUTE);
String containerClass = element.getAttribute(CONTAINER_CLASS_ATTRIBUTE);
Assert.isTrue(!(StringUtils.hasText(containerType) && StringUtils.hasText(containerClass)),
"At most one of '" + CONTAINER_TYPE_ATTRIBUTE + "' or '" + CONTAINER_CLASS_ATTRIBUTE + "' may be configured.");
if ("default".equals(containerType)) {
containerClass = "org.springframework.jms.listener.DefaultMessageListenerContainer";
}
else if ("simple".equals(containerType)) {
containerClass = "org.springframework.jms.listener.SimpleMessageListenerContainer";
}
if (StringUtils.hasText(containerClass)) {
builder.addPropertyValue("containerType", containerClass);
if (containerClass.contains("DefaultMessageListenerContainer")) {
containerType = "default";
if (!StringUtils.hasText(containerClass)) {
if ("default".equals(containerType)) {
containerClass = "org.springframework.jms.listener.DefaultMessageListenerContainer";
}
else if ("simple".equals(containerType)) {
containerClass = "org.springframework.jms.listener.SimpleMessageListenerContainer";
}
}
/*
* Schema docs tell the user to ensure that, if they supply a container-class, it
* is their responsibility to set the container-type appropriately, based on the superclass
* of their implementation (default="default"). If it is set incorrectly, some attributes
* may not be applied.
*
* We cannot reliably infer it here.
*/
builder.addPropertyValue("containerType", containerClass);
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "receive-timeout");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "task-executor");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "transaction-manager");

View File

@@ -59,9 +59,19 @@
</bean>
<alias name="connectionFactory" alias="connFact"/>
<jms:channel id="withPlaceholders" queue="${queue}"
concurrency="${concurrency}"/>
<jms:channel id="withDefaultContainer" queue-name="default.container.queue" />
<jms:channel id="withExplicitDefaultContainer" queue-name="explicit.default.container.queue"
container-type="default" />
<jms:channel id="withSimpleContainer" queue-name="simple.container.queue"
container-type="simple"/>
<jms:channel id="withContainerClass" queue-name="custom.container.queue"
container-class="org.springframework.integration.jms.config.JmsChannelParserTests$CustomTestMessageListenerContainer" />
</beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 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.
@@ -29,7 +29,6 @@ import javax.jms.Topic;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.AbstractApplicationContext;
@@ -42,12 +41,14 @@ import org.springframework.integration.test.util.TestUtils;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.AbstractMessageListenerContainer;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.listener.SimpleMessageListenerContainer;
import org.springframework.jms.support.destination.DestinationResolver;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author Gary Russell
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@@ -95,6 +96,18 @@ public class JmsChannelParserTests {
@Autowired
private Topic topic;
@Autowired
private MessageChannel withDefaultContainer;
@Autowired
private MessageChannel withExplicitDefaultContainer;
@Autowired
private MessageChannel withSimpleContainer;
@Autowired
private MessageChannel withContainerClass;
@Autowired
private AbstractApplicationContext context;
@@ -231,6 +244,38 @@ public class JmsChannelParserTests {
System.out.println(container.getMaxConcurrentConsumers());
}
@Test
public void withDefaultContainer() {
DefaultMessageListenerContainer container = TestUtils.getPropertyValue(
withDefaultContainer, "container",
DefaultMessageListenerContainer.class);
assertEquals("default.container.queue", container.getDestinationName());
}
@Test
public void withExplicitDefaultContainer() {
DefaultMessageListenerContainer container = TestUtils.getPropertyValue(
withExplicitDefaultContainer, "container",
DefaultMessageListenerContainer.class);
assertEquals("explicit.default.container.queue", container.getDestinationName());
}
@Test
public void withSimpleContainer() {
SimpleMessageListenerContainer container = TestUtils.getPropertyValue(
withSimpleContainer, "container",
SimpleMessageListenerContainer.class);
assertEquals("simple.container.queue", container.getDestinationName());
}
@Test
public void withContainerClass() {
CustomTestMessageListenerContainer container = TestUtils.getPropertyValue(
withContainerClass, "container",
CustomTestMessageListenerContainer.class);
assertEquals("custom.container.queue", container.getDestinationName());
}
static class TestDestinationResolver implements DestinationResolver {
@@ -253,4 +298,8 @@ public class JmsChannelParserTests {
static class TestInterceptor extends ChannelInterceptorAdapter {
}
static class CustomTestMessageListenerContainer extends DefaultMessageListenerContainer {
}
}