spring integration 入门例子 Channel Adapter

Submitted by Lizhe on Wed, 06/28/2017 - 02:28
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 {
    void sayHello(String name);
}
 

输出接口的实现, 调用system out那个

package name.lizhe.channeladapter.input;

public class MyHelloService implements HelloService {

    @Override
    public void sayHello(String name) {
        System.out.println("Hello " + name);
    }
}
 

一个返回dummy数据的实现类 (实际上这个玩意应该用来读文件或是http资源什么的)

package name.lizhe.channeladapter.input;

public class MyInputService {
    public String dummyInput() {
        return "dummy data";
    }
}
 

然后用一个配置文件把他们串起来

<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/integration"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://www.springframework.org/schema/integration  
            http://www.springframework.org/schema/integration/spring-integration.xsd">

    <!-- inboundChannel class -->
    <beans:bean id="inputService" class="name.lizhe.channeladapter.input.MyInputService" />

    <channel id="inboundChannel" />

    <inbound-channel-adapter ref="inputService" method="dummyInput"
        channel="inboundChannel">
        <poller fixed-rate="5000" />
    </inbound-channel-adapter>
    

    <!--     定义一个消费者, 输入源是上面定义的channel, 调用的是类(ref)中的方法(method) sayHello -->
    <service-activator input-channel="inboundChannel" ref="helloService"
        method="sayHello" />

    <!--     消费者类定义 -->
    <beans:bean id="helloService" class="name.lizhe.channeladapter.input.MyHelloService" />

<!--     <inbound-channel-adapter ref="source2" method="method2" -->
<!--         channel="channel2"> -->
<!--         <poller cron="30 * 9-17 * * MON-FRI" /> -->
<!--     </inbound-channel-adapter> -->

</beans:beans>

你可以看到除了那个负责输出结果的helloService之外, 我们多了一个inbound-channel-adapter

这个adapter需要一个不带参数的方法引用,这个方法不能是void的必需有返回值传递给channel指定的管道(这里是inboundChannel)

然后我们通过poller 控制它在什么时候被调用, 这里是每五秒钟, 这种模式其实完全可以代替springboot+springbatch或者是camel

 


 

以下是outbound例子

package name.lizhe.channeladapter.output;

public class MyOutputService {
    public void dummyOuput(String name) {
        System.out.println("hello " + name);
    }
}
 

<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/integration"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://www.springframework.org/schema/integration  
            http://www.springframework.org/schema/integration/spring-integration.xsd">

    <channel id="outboundChannel" />

    <outbound-channel-adapter method="dummyOuput"
        channel="outboundChannel">
        <beans:bean class="name.lizhe.channeladapter.output.MyOutputService"/>
    </outbound-channel-adapter>

</beans:beans>

package name.lizhe.channeladapter.output;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;

/**
 * Main entry-point into the test application
 */
public class App {

    public static void main(String args[]) {
        String cfg = "name/lizhe/channeladapter/output/applicationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(cfg);
        MessageChannel channel = context.getBean("outboundChannel", MessageChannel.class);
        Message<String> message = MessageBuilder.withPayload("World").build();
        channel.send(message);
    }

}

 

最后如果要查询定义好的Channel Adapter可以参考下面网址

http://docs.spring.io/spring-integration/reference/html/endpoint-summary.html