跳至主要內容

bridge endpoint


bridge endpoint

使用连接桥(bridge)可以简单地将两个消息通道连接起来。

例子

HelloBridge.java

package com.supremepole.bridge;

public class HelloBridge {

    public void helloBridge(String string) {
        System.out.println("Hello: "+string);
    }

}

BridgeDemo.java

package com.supremepole.bridge;

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

public class BridgeDemo {

    private ApplicationContext applicationContext=null;

    private MessageChannel messageChannel=null;

    public BridgeDemo(){
        applicationContext=new ClassPathXmlApplicationContext("bridge.xml");
        messageChannel=applicationContext.getBean("bridge-in-channel", MessageChannel.class);
    }

    public void publish(){
        Message<String> message= MessageBuilder.withPayload("bridge").build();
        messageChannel.send(message, 10000);
    }

    public static void main(String[] args) {
        BridgeDemo bridgeDemo=new BridgeDemo();
        bridgeDemo.publish();
    }

}

bridge.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"
       xmlns:int="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-2.1.xsd">

    <int:publish-subscribe-channel id="bridge-in-channel" />

    <int:bridge input-channel="bridge-in-channel"
                output-channel="bridge-out-channel">
    </int:bridge>

    <int:channel id="bridge-out-channel">
        <int:queue capacity="10" />
    </int:channel>

    <int:service-activator input-channel="bridge-out-channel" ref="HelloBridge" method="helloBridge">
        <int:poller fixed-delay="1000" />
    </int:service-activator>

    <bean id="HelloBridge" class="com.supremepole.bridge.HelloBridge"/>

</beans>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.supremepole</groupId>
    <artifactId>bridge</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-core</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-stream</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
    </dependencies>

</project>
上次编辑于:
贡献者: Neil