Spring Integration系列《1》 Hello World项目

这个Hello World项目是从spring官网下载,本地运行学习的。

Spring Integration官网地址
https://spring.io/projects/spring-integration#overview

Spring Integration 官方学习列子
https://github.com/spring-projects/spring-integration-samples

本Spring Integration系列 Hello World项目项目源码地址: https://gitee.com/bseaworkspace/study_java_web/tree/master/springIntegrationHelloWorld

代码结构和运行结果

代码运行方式

  • 执行PollerApp main方法
  • 执行HelloWorldApp main方法
    在这里插入图片描述

运行时序图

代码解析

package com.xsz;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.GenericMessage;

/**
 * Demonstrates a basic Message Endpoint that simply prepends a greeting
 * ("Hello ") to an inbound String payload from a Message. This is a very
 * low-level example, using Message Channels directly for both input and
 * output. Notice that the output channel has a queue sub-element. It is
 * therefore a PollableChannel and its consumers must invoke receive() as
 * demonstrated below.
 * <p>
 * View the configuration of the channels and the endpoint (a &lt;service-activator/&gt;
 * element) in 'helloWorldDemo.xml' within this same package.
 *
 * @author Mark Fisher
 * @author Oleg Zhurakousky
 * @author Gary Russell
 */
public class HelloWorldApp {

    private static Log logger = LogFactory.getLog(HelloWorldApp.class);

    public static void main(String[] args) {
        /**
         * ClassPathXmlApplicationContext Spring IOC 容器
         * 第一个参数 xml文件路径
         * 第二个参数JVM 创建对应对象
         */
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/helloWorldDemo.xml",
                HelloWorldApp.class);
        /**
         * Channel: 消息发送者发送消息到通道(Channel),消息接受者从通道(Channel)接收消息
         *
         */
        MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class);
        PollableChannel outputChannel = context.getBean("outputChannel", PollableChannel.class);
        inputChannel.send(new GenericMessage<String>("World"));
        /**
         *  PollableChannel.receive(long timeout)
         *  Receive the first available message from this channel.
         *  If the specified timeout is 0, the method will return immediately
         */

        logger.info("==> HelloWorldDemo: " + outputChannel.receive(0).getPayload());
        context.close();
    }

}

技术要点:

  • service-activator

用来连接应用的接口和message framework消息框架的组件,一个输入的频道input message channel必须被设定,一个service activator的方法被执行并且返回了一个值,那么可以提供一个输出频道output message channel(如果消息提供自己的返回地址,那么这是可选的)。这个规则适用于所有的consumer endpoints。输入从input channel到service activator再到message handler,然后返回output message到service activator到output message channel。归根结底:被继承组件的主要对外接口。
1.inputChannel是在本列中发挥的作用是传递参数
2. 对象helloService的sayHello方法接收参数
3.sayHello方法的返回值 ,会传给outputChannel

  • channel

消息传输的通道。分两种,一种是point-to-point点对点的,一种是publish-subscribe发布订阅形式的。如果是点对点的channel,至少会有一个消费者consumer能收到发送的message,另一种订阅发布的channel,spring integration试图去用广播的形式发布message给那些订阅者subscriber。
capacity属性是表示timeout时间。

META-INF/spring/integration/helloWorldDemo.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
			https://www.springframework.org/schema/beans/spring-beans.xsd
			http://www.springframework.org/schema/integration
			https://www.springframework.org/schema/integration/spring-integration.xsd">

    <channel id="inputChannel"/>

    <channel id="outputChannel">
        <queue capacity="10"/>
    </channel>

    <service-activator input-channel="inputChannel"
                       output-channel="outputChannel"
                       ref="helloService"
                       method="sayHello"/>

    <beans:bean id="helloService" class="com.xsz.HelloService"/>

</beans:beans>

技术要点:

  • Channel Adapter

连接一个消息通道和其他实体之间的对象。channel adapter也分inbound内绑定和outbound外绑定。

Inbound通道适配:通常的作用是将一个外部系统的资源进行转换,通过消息通道输送到系统中,用于进行后续的处理。

Outbound通道适配:将系统中的资源通过消息通道发送给Outbound通道适配,然后该“Outbound通道适配”将其转换为外部的资源。

举例:<file:inbound-channel-adapter>的作用,将文件系统中的文件进行读取,将文件对象或者文件内容发送到消息通道中。

<file:outbound-channel-adapter>的作用,将处理好的文件资源输出到文件系统中。

META-INF/spring/integration/delay.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
		http://www.springframework.org/schema/task https://www.springframework.org/schema/task/spring-task.xsd"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:task="http://www.springframework.org/schema/task">


    <int:inbound-channel-adapter expression="T(java.lang.System).currentTimeMillis()" channel="logger">
        <int:poller fixed-delay="20000" max-messages-per-poll="2" />
    </int:inbound-channel-adapter>

    <int:logging-channel-adapter id="logger" logger-name="org.springframework.integration.samples.helloworld"/>

    <task:executor id="executor" queue-capacity="20" pool-size="5-20"/>
</beans>

更多资料关注微信公众平台
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值