Spring integration总结

一、概念

  能够理解成一个队列,在该队列不一样节点部分进行相应的逻辑操做,实现轻量级消息传递java

 二、结构流程

 

 三、代码结构:

  3.1配置文件:

<?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:context="http://www.springframework.org/schema/context" xmlns:integration="http://www.springframework.org/schema/integration" xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">

    <context:annotation-config />

    <context:component-scan base-package="ch.javaee.integration.example.helloWorld"/>

    <!-- this channel is called by the application and the message is passed to it -->
    <integration:channel id="inputChannel"/>

    <integration:channel id="middleChannel"/>

    <!-- this channel receive the modified message -->
    <integration:channel id="outputChannel"/>

    <integration:channel id="finalChannel"/>

    <!-- this service transform the message in input-channel and send the result to output-channel -->
    <!-- the service method to call is referenced in explicitly -->
    <integration:service-activator input-channel="inputChannel" ref="myTestProducerService" method="poll" output-channel="middleChannel"/>

    <!-- this service receives a message and pass it to printerService -->
    <!-- the method that consumes the message is implicitly defined by the @ServiceActivator annotation or it should be the only method in the class -->
    <integration:service-activator input-channel="middleChannel" ref="myTestConsumerService" output-channel="outputChannel"/>

    <integration:service-activator input-channel="outputChannel" ref="myTestFinalConsumerService" output-channel="finalChannel"/>

</beans>
View Code

 

             

 

 

 其中一、二、三、4都是定义的channel spring

 五、六、7是针对各个channel中数据处理,ref是处理input-channel数据的bean名称,method是ref中bean里的方法,output-channel存储是处理后的结果app

 Service Activator:可调用Spring的Bean来处理消息,并将处理后的结果输出到指定的消息通道,其做用使用起来和在配置文件中的method="****"相似ide

3.2  channel数据处理逻辑

以5为例:
<integration:service-activator input-channel="inputChannel" ref="myTestProducerService" method="poll" output-channel="middleChannel"/>

   3.2.1 input-channel内数据内容: 

public class MyTestDemo { public static void main(String[] args) { MyTestProducerService myTestService=new MyTestProducerService(); // create a message with the content "World" // buildMessageAndSend("notification", "identify");
        List<String> stringList=new ArrayList<String>(); stringList.add("Monday"); stringList.add("Tuesday"); stringList.add("Wednesday"); for(int i=0;i<stringList.size();i++){ myTestService.putMessage(stringList.get(i)); } buildMessageAndSend("notification", "shutdown"); } private static Map<String, String> buildMessageMap(String type, String msg) { Map<String, String> msgMap = new HashMap<String, String>(); msgMap.put("type", type); msgMap.put("message", msg); msgMap.put("context_id", "123"); return msgMap; } private static void buildMessageAndSend(String type, String msg) { Map<String, String> notifyMap = buildMessageMap(type, msg); // load the Spring context
        ApplicationContext context = new ClassPathXmlApplicationContext("my-spring-config.xml"); // get the reference to the message channel
        MessageChannel channel = context.getBean("inputChannel", MessageChannel.class); try { if(channel.send(MessageBuilder.withPayload(notifyMap).build()) == false) { throw new Exception("Unable to publish the shutdown notification."); } } catch (Exception e) { System.out.println(e); } } }
View Code

 

 

 

 

 

 

   其中 1:加载配置文件ui

      2:获取输入channel名字this

      3:发送数据到input-channel中spa

 3.2.2 处理input-channel中数据

@Service public class MyTestProducerService { /** The message queue. */
    private static BlockingQueue<Object> messageQueue = new ArrayBlockingQueue<Object>( 100, true); public static void putMessage(Object obj){ messageQueue.add(obj); } public final Message<Object> poll() throws InterruptedException { Object obj = null; obj=messageQueue.take(); Message<Object> message = null; if(obj instanceof String){ String objStr=(String) obj; message= MessageBuilder.withPayload((Object)objStr) .build(); } return message; } }
View Code

 

   其中1:对应3.1节中5中ref对应的beancode

    2:对应3.1节中5中method对应的方法component

    3:返回给3.1节中5中output-channel中的数据orm

 

3.3 其余channel处理逻辑与3.2节相似了

相关文章
相关标签/搜索