INT-762 Added "channel" and "publish-subscribe-channel" elements to the JMS namespace.

This commit is contained in:
Mark Fisher
2009-10-01 02:51:44 +00:00
parent 645ab548b3
commit 6afd9427e9
5 changed files with 363 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2002-2009 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
*
* http://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.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
/**
* Parser for the 'channel' and 'publish-subscribe-channel' elements of the
* Spring Integration JMS namespace.
*
* @author Mark Fisher
* @since 2.0
*/
public class JmsChannelParser extends AbstractSingleBeanDefinitionParser {
@Override
protected String getBeanClassName(Element element) {
return "org.springframework.integration.jms.JmsDestinationBackedMessageChannel";
}
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String connectionFactory = element.getAttribute("connection-factory");
if (!StringUtils.hasText(connectionFactory)) {
connectionFactory = "connectionFactory";
}
builder.addConstructorArgReference(connectionFactory);
if ("channel".equals(element.getLocalName())) {
this.parseDestination(element, parserContext, builder, "queue");
}
else if ("publish-subscribe-channel".equals(element.getLocalName())) {
this.parseDestination(element, parserContext, builder, "topic");
}
}
private void parseDestination(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, String type) {
boolean isPubSub = "topic".equals(type);
String ref = element.getAttribute(type);
String name = element.getAttribute(type + "-name");
boolean isReference = StringUtils.hasText(ref);
boolean isName = StringUtils.hasText(name);
if (!(isReference ^ isName)) {
parserContext.getReaderContext().error("Exactly one of the '" + type +
"' or '" + type + "-name' attributes is required.", element);
}
if (isReference) {
builder.addConstructorArgReference(ref);
}
else if (isName) {
builder.addConstructorArgValue(name);
builder.addConstructorArgValue(isPubSub);
String destinationResolver = element.getAttribute("destination-resolver");
if (StringUtils.hasText(destinationResolver)) {
builder.addConstructorArgReference(destinationResolver);
}
}
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.jms.config;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHandler;
import org.springframework.integration.config.xml.SimpleHeaderEnricherParser;
import org.springframework.integration.jms.JmsHeaders;
@@ -33,6 +34,9 @@ public class JmsNamespaceHandler extends AbstractIntegrationNamespaceHandler {
this.registerBeanDefinitionParser("inbound-channel-adapter", new JmsInboundChannelAdapterParser());
this.registerBeanDefinitionParser("outbound-gateway", new JmsOutboundGatewayParser());
this.registerBeanDefinitionParser("outbound-channel-adapter", new JmsOutboundChannelAdapterParser());
BeanDefinitionParser channelParser = new JmsChannelParser();
this.registerBeanDefinitionParser("channel", channelParser);
this.registerBeanDefinitionParser("publish-subscribe-channel", channelParser);
this.registerBeanDefinitionParser("header-enricher",
new SimpleHeaderEnricherParser(JmsHeaders.PREFIX, new String[] { "replyTo" }));
}

View File

@@ -19,6 +19,90 @@
]]></xsd:documentation>
</xsd:annotation>
<xsd:element name="channel">
<xsd:annotation>
<xsd:documentation>
Defines a Message Channel that is backed by a JMS Queue.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="channelType">
<xsd:attribute name="queue" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Reference to a JMS Queue. Either this attribute or the 'queue-name'
must be provided, but only one.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="queue-name" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Name of a JMS Queue to be resolved by this channel's DestinationResolver.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="publish-subscribe-channel">
<xsd:annotation>
<xsd:documentation>
Defines a Message Channel that is backed by a JMS Topic.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="channelType">
<xsd:attribute name="topic" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Reference to a JMS Topic. Either this attribute or the 'topic-name'
must be provided, but only one.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="topic-name" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Name of a JMS Topic to be resolved by this channel's DestinationResolver.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="channelType">
<xsd:attribute name="id" type="xsd:ID" use="required">
<xsd:annotation>
<xsd:documentation>
ID for this channel. Required.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="connection-factory" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Reference to a JMS ConnectionFactory. If none is provided, the default
bean name for the reference will be "connectionFactory".
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="destination-resolver" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Reference to a DestinationResolver. If none is provided, the default will
be a DynamicDestinationResolver.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:element name="message-driven-channel-adapter">
<xsd:annotation>
<xsd:documentation>

View File

@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jms="http://www.springframework.org/schema/integration/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/jms
http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">
<jms:channel id="queueReferenceChannel" queue="testQueue"/>
<jms:channel id="queueNameChannel" queue-name="test.queue"/>
<jms:channel id="queueNameWithResolverChannel" queue-name="foo"
destination-resolver="destinationResolver" connection-factory="connFact"/>
<jms:publish-subscribe-channel id="topicReferenceChannel" topic="testTopic"/>
<jms:publish-subscribe-channel id="topicNameChannel" topic-name="test.topic"/>
<jms:publish-subscribe-channel id="topicNameWithResolverChannel" topic-name="foo"
destination-resolver="destinationResolver" connection-factory="connFact"/>
<bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue">
<property name="physicalName" value="test.queue"/>
</bean>
<bean id="testTopic" class="org.apache.activemq.command.ActiveMQTopic">
<property name="physicalName" value="test.topic"/>
</bean>
<bean id="destinationResolver"
class="org.springframework.integration.jms.config.JmsChannelParserTests$TestDestinationResolver"/>
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://localhost?broker.persistent=false"/>
</bean>
<alias name="connectionFactory" alias="connFact"/>
</beans>

View File

@@ -0,0 +1,156 @@
/*
* Copyright 2002-2009 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
*
* http://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 static org.junit.Assert.assertEquals;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Topic;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.jms.JmsDestinationBackedMessageChannel;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.AbstractMessageListenerContainer;
import org.springframework.jms.support.destination.DestinationResolver;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class JmsChannelParserTests {
@Autowired
private MessageChannel queueReferenceChannel;
@Autowired
private MessageChannel queueNameChannel;
@Autowired
private MessageChannel queueNameWithResolverChannel;
@Autowired
private Queue queue;
@Autowired
private MessageChannel topicReferenceChannel;
@Autowired
private MessageChannel topicNameChannel;
@Autowired
private MessageChannel topicNameWithResolverChannel;
@Autowired
private Topic topic;
@Test
public void queueReferenceChannel() {
assertEquals(JmsDestinationBackedMessageChannel.class, queueReferenceChannel.getClass());
JmsDestinationBackedMessageChannel channel = (JmsDestinationBackedMessageChannel) queueReferenceChannel;
DirectFieldAccessor accessor = new DirectFieldAccessor(channel);
JmsTemplate jmsTemplate = (JmsTemplate) accessor.getPropertyValue("jmsTemplate");
AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) accessor.getPropertyValue("container");
assertEquals(queue, jmsTemplate.getDefaultDestination());
assertEquals(queue, container.getDestination());
}
@Test
public void queueNameChannel() {
assertEquals(JmsDestinationBackedMessageChannel.class, queueNameChannel.getClass());
JmsDestinationBackedMessageChannel channel = (JmsDestinationBackedMessageChannel) queueNameChannel;
DirectFieldAccessor accessor = new DirectFieldAccessor(channel);
JmsTemplate jmsTemplate = (JmsTemplate) accessor.getPropertyValue("jmsTemplate");
AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) accessor.getPropertyValue("container");
assertEquals("test.queue", jmsTemplate.getDefaultDestinationName());
assertEquals("test.queue", container.getDestinationName());
}
@Test
public void queueNameWithResolverChannel() {
assertEquals(JmsDestinationBackedMessageChannel.class, queueNameWithResolverChannel.getClass());
JmsDestinationBackedMessageChannel channel = (JmsDestinationBackedMessageChannel) queueNameWithResolverChannel;
DirectFieldAccessor accessor = new DirectFieldAccessor(channel);
JmsTemplate jmsTemplate = (JmsTemplate) accessor.getPropertyValue("jmsTemplate");
AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) accessor.getPropertyValue("container");
assertEquals("foo", jmsTemplate.getDefaultDestinationName());
assertEquals("foo", container.getDestinationName());
}
@Test
public void topicReferenceChannel() {
assertEquals(JmsDestinationBackedMessageChannel.class, topicReferenceChannel.getClass());
JmsDestinationBackedMessageChannel channel = (JmsDestinationBackedMessageChannel) topicReferenceChannel;
DirectFieldAccessor accessor = new DirectFieldAccessor(channel);
JmsTemplate jmsTemplate = (JmsTemplate) accessor.getPropertyValue("jmsTemplate");
AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) accessor.getPropertyValue("container");
assertEquals(topic, jmsTemplate.getDefaultDestination());
assertEquals(topic, container.getDestination());
}
@Test
public void topicNameChannel() {
assertEquals(JmsDestinationBackedMessageChannel.class, topicNameChannel.getClass());
JmsDestinationBackedMessageChannel channel = (JmsDestinationBackedMessageChannel) topicNameChannel;
DirectFieldAccessor accessor = new DirectFieldAccessor(channel);
JmsTemplate jmsTemplate = (JmsTemplate) accessor.getPropertyValue("jmsTemplate");
AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) accessor.getPropertyValue("container");
assertEquals("test.topic", jmsTemplate.getDefaultDestinationName());
assertEquals("test.topic", container.getDestinationName());
}
@Test
public void topicNameWithResolverChannel() {
assertEquals(JmsDestinationBackedMessageChannel.class, topicNameWithResolverChannel.getClass());
JmsDestinationBackedMessageChannel channel = (JmsDestinationBackedMessageChannel) topicNameWithResolverChannel;
DirectFieldAccessor accessor = new DirectFieldAccessor(channel);
JmsTemplate jmsTemplate = (JmsTemplate) accessor.getPropertyValue("jmsTemplate");
AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) accessor.getPropertyValue("container");
assertEquals("foo", jmsTemplate.getDefaultDestinationName());
assertEquals("foo", container.getDestinationName());
}
static class TestDestinationResolver implements DestinationResolver {
@Autowired
private Queue queue;
@Autowired
private Topic topic;
public Destination resolveDestinationName(Session session, String destinationName, boolean pubSubDomain)
throws JMSException {
if (!"foo".equals(destinationName)) {
throw new IllegalStateException("only destination name of 'foo' is supported");
}
return pubSubDomain ? topic : queue;
}
}
}