A Channel Adapter is a Message Endpoint that enables connecting a single sender or receiver to a Message Channel. Spring Integration provides a number of adapters out of the box to support various transports, such as JMS, File, HTTP, Web Services, and Mail. Those will be discussed in upcoming chapters of this reference guide. However, this chapter focuses on the simple but flexible Method-invoking Channel Adapter support. There are both inbound and outbound adapters, and each may be configured with XML elements provided in the core namespace. |
Channel Adapter 实际上是一个endpoint (和web service的endpoint应该是一个意思) ,
上文说它用来连接Message Channel, 不过你可以简单的理解成 它用来连接一个输入的外部系统或者一个输出的外部系统
通常是jms, file 或者http 之类的, 和camel差不多
这里我先写一个简单的helloworld
老办法我们先写一个输出接口
package name.lizhe.channeladapter.input; public interface HelloService { |
输出接口的实现, 调用system out那个
package name.lizhe.channeladapter.input; public class MyHelloService implements HelloService { @Override |
一个返回dummy数据的实现类 (实际上这个玩意应该用来读文件或是http资源什么的)
package name.lizhe.channeladapter.input; public class MyInputService { |
然后用一个配置文件把他们串起来
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans" <!-- inboundChannel class --> <channel id="inboundChannel" /> <inbound-channel-adapter ref="inputService" method="dummyInput" <!-- 定义一个消费者, 输入源是上面定义的channel, 调用的是类(ref)中的方法(method) sayHello --> <!-- 消费者类定义 --> <!-- <inbound-channel-adapter ref="source2" method="method2" --> </beans:beans> |
你可以看到除了那个负责输出结果的helloService之外, 我们多了一个inbound-channel-adapter
这个adapter需要一个不带参数的方法引用,这个方法不能是void的必需有返回值传递给channel指定的管道(这里是inboundChannel)
然后我们通过poller 控制它在什么时候被调用, 这里是每五秒钟, 这种模式其实完全可以代替springboot+springbatch或者是camel
以下是outbound例子
package name.lizhe.channeladapter.output; public class MyOutputService { |
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans" <channel id="outboundChannel" /> <outbound-channel-adapter method="dummyOuput" </beans:beans> |
package name.lizhe.channeladapter.output; import org.springframework.context.ApplicationContext; /** public static void main(String args[]) { } |
最后如果要查询定义好的Channel Adapter可以参考下面网址
http://docs.spring.io/spring-integration/reference/html/endpoint-summary.html