The <router/> element now creates its own endpoint (i.e. it is no longer necessary to create a <handler-endpoint/> as well) (INT-284).

This commit is contained in:
Mark Fisher
2008-07-06 18:08:31 +00:00
parent a83e39b6ce
commit b7f31fcbf0
10 changed files with 69 additions and 70 deletions

View File

@@ -18,20 +18,13 @@ package org.springframework.integration.samples.oddeven;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.MessageSource;
import org.springframework.integration.annotation.Polled;
/**
* @author Mark Fisher
*/
@MessageEndpoint(output="numbers")
@Polled(initialDelay=1000, period=3000)
public class Counter {
private final AtomicInteger count = new AtomicInteger();
@MessageSource
public int next() {
return count.incrementAndGet();
}

View File

@@ -16,17 +16,15 @@
package org.springframework.integration.samples.oddeven;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.MessageTarget;
import org.springframework.integration.annotation.Handler;
/**
* @author Mark Fisher
*/
@MessageEndpoint(input="even")
public class EvenLogger {
@MessageTarget
public void even(int i) {
@Handler
public void log(int i) {
System.out.println("even: " + i);
}

View File

@@ -16,17 +16,15 @@
package org.springframework.integration.samples.oddeven;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.MessageTarget;
import org.springframework.integration.annotation.Handler;
/**
* @author Mark Fisher
*/
@MessageEndpoint(input="odd")
public class OddLogger {
@MessageTarget
public void odd(int i) {
@Handler
public void log(int i) {
System.out.println("odd: " + i);
}

View File

@@ -16,17 +16,12 @@
package org.springframework.integration.samples.oddeven;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.Router;
/**
* @author Mark Fisher
*/
@MessageEndpoint(input="numbers")
public class NumberRouter {
public class ParityResolver {
@Router
public String resolveChannel(int i) {
public String getParity(int i) {
if (i % 2 == 0) {
return "even";
}

View File

@@ -2,22 +2,34 @@
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<context:component-scan base-package="org.springframework.integration.samples.oddeven"/>
<message-bus/>
<annotation-driven/>
<channel id="numbers"/>
<channel id="even"/>
<channel id="odd"/>
<annotation-driven/>
<channel-adapter source="counter" method="next" channel="numbers">
<schedule period="3000"/>
</channel-adapter>
<router ref="parityResolver" method="getParity" input-channel="numbers"/>
<handler-endpoint ref="oddLogger" input-channel="odd"/>
<handler-endpoint ref="evenLogger" input-channel="even"/>
<beans:bean id="counter" class="org.springframework.integration.samples.oddeven.Counter"/>
<beans:bean id="parityResolver" class="org.springframework.integration.samples.oddeven.ParityResolver"/>
<beans:bean id="oddLogger" class="org.springframework.integration.samples.oddeven.OddLogger"/>
<beans:bean id="evenLogger" class="org.springframework.integration.samples.oddeven.EvenLogger"/>
</beans:beans>

View File

@@ -200,17 +200,32 @@
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="handlerEndpointType">
<xsd:extension base="inputOutputHandlerEndpointType">
<xsd:attribute name="reply-handler" type="xsd:string"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="handlerEndpointType">
<xsd:complexType name="inputOutputHandlerEndpointType">
<xsd:annotation>
<xsd:documentation>
Base type for handler endpoint elements.
Base type for handler endpoint elements that accept Messages from an input-channel
and also produce Messages to be sent to an output-channel.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="inputHandlerEndpointType">
<xsd:attribute name="output-channel" type="xsd:string"/>
<xsd:attribute name="return-address-overrides" type="xsd:boolean" default="false"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="inputHandlerEndpointType">
<xsd:annotation>
<xsd:documentation>
Base type for handler endpoint elements that accept Messages from an input-channel.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
@@ -222,9 +237,7 @@
<xsd:attribute name="ref" type="xsd:string" use="required"/>
<xsd:attribute name="method" type="xsd:string"/>
<xsd:attribute name="input-channel" type="xsd:string" use="required"/>
<xsd:attribute name="output-channel" type="xsd:string"/>
<xsd:attribute name="selector" type="xsd:string"/>
<xsd:attribute name="return-address-overrides" type="xsd:boolean" default="false"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
@@ -318,20 +331,15 @@
<xsd:attribute name="keep-alive" type="xsd:int"/>
</xsd:complexType>
<xsd:element name="router">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
<xsd:element name="router" type="inputHandlerEndpointType">
<xsd:annotation>
<xsd:documentation>
Defines a Router.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:ID"/>
<xsd:attribute name="ref" type="xsd:string" use="required"/>
<xsd:attribute name="method" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="splitter" type="handlerEndpointType">
<xsd:element name="splitter" type="inputOutputHandlerEndpointType">
<xsd:annotation>
<xsd:documentation>
Defines a Splitter.

View File

@@ -24,6 +24,7 @@ import org.aopalliance.aop.Advice;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.Lifecycle;
import org.springframework.integration.ConfigurationException;
@@ -231,7 +232,16 @@ public abstract class AbstractEndpoint implements MessageEndpoint, BeanNameAware
throw new MessageHandlerNotRunningException(message);
}
if (message.getPayload() instanceof EndpointVisitor) {
((EndpointVisitor) message.getPayload()).visitEndpoint(this);
MessageEndpoint endpoint = null;
try {
endpoint = (MessageEndpoint) AopContext.currentProxy();
}
catch (IllegalStateException e) {
if (logger.isDebugEnabled()) {
logger.debug("The currenty proxy is not exposed.");
}
}
((EndpointVisitor) message.getPayload()).visitEndpoint((endpoint != null) ? endpoint : this);
return true;
}
if (!this.supports(message)) {

View File

@@ -54,6 +54,9 @@ public class RouterMessageHandlerAdapter extends AbstractMessageHandlerAdapter i
this.setMethodName(methodName);
}
public RouterMessageHandlerAdapter() {
}
public void setChannelRegistry(ChannelRegistry channelRegistry) {
this.channelRegistry = channelRegistry;

View File

@@ -16,36 +16,20 @@
package org.springframework.integration.router.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.config.AbstractHandlerEndpointParser;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.router.RouterMessageHandlerAdapter;
import org.springframework.util.StringUtils;
/**
* Parser for the &lt;router/&gt; element.
*
* @author Mark Fisher
*/
public class RouterParser extends AbstractSingleBeanDefinitionParser {
public class RouterParser extends AbstractHandlerEndpointParser {
@Override
protected Class<?> getBeanClass(Element element) {
protected Class<? extends MessageHandler> getHandlerAdapterClass() {
return RouterMessageHandlerAdapter.class;
}
@Override
protected void doParse(Element element, BeanDefinitionBuilder builder) {
String ref = element.getAttribute("ref");
String methodName = element.getAttribute("method");
if (!StringUtils.hasText(ref) || !StringUtils.hasText(methodName)) {
throw new ConfigurationException(
"The 'ref' and 'method' attributes are both required.");
}
builder.addConstructorArgReference(ref);
builder.addConstructorArgValue(methodName);
}
}

View File

@@ -15,9 +15,7 @@
<channel id="output2"/>
<handler-endpoint ref="router" input-channel="input"/>
<router id="router" ref="pojo" method="route"/>
<router id="router" ref="pojo" method="route" input-channel="input"/>
<beans:bean id="pojo" class="org.springframework.integration.router.config.TestRouter"/>