【问题标题】:Send Acknowledgement to Sender in Spring Integration : tcp-in/out-channel-adapter在 Spring 集成中向 Sender 发送确认:tcp-in/out-channel-adapter
【发布时间】:2014-07-23 16:44:20
【问题描述】:

我制作了分布式应用程序。 1. Web 应用程序和 2. Spring 集成控制台应用程序。

我可以将数据从一个发送到另一个,并使用控制台应用程序进行后台工作,但我想确认接收方向发送方接收到的数据。

发件人应该得到确认。

我的代码:

Web 应用程序端(发送方/客户端):

xml:

    <bean id="javaSerializer" class="org.springframework.core.serializer.DefaultSerializer"/>
    <bean id="javaDeserializer" class="org.springframework.core.serializer.DefaultDeserializer"/>
    <int-ip:tcp-connection-factory id="client" type="client" host="localhost" port="56565" single-use="true" so-timeout="10000" deserializer="javaDeserializer" serializer="javaSerializer"/>
    <int:channel id="input" />
    <int-ip:tcp-outbound-channel-adapter id="outboundClient" channel="input" connection-factory="client"/>

登录时的代码:

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/esb/sender/sender-esb.xml");
    MessageChannel esbChannel = applicationContext.getBean("input",MessageChannel.class);
    Message<String> esbMsg = MessageBuilder.withPayload(form.getPatronID()).build();
    esbChannel.send(esbMsg);

Spring-Integration 应用端(服务器/接收器):

xml:

    <bean id="javaSerializer" class="org.springframework.core.serializer.DefaultSerializer"/>
    <bean id="javaDeserializer" class="org.springframework.core.serializer.DefaultDeserializer"/>
    <int-ip:tcp-connection-factory id="server" type="server" host="localhost" port="56565" single-use="true" so-timeout="10000" deserializer="javaDeserializer" serializer="javaSerializer"/>
    <int-ip:tcp-inbound-channel-adapter id="inboundServer" channel="inputChannel" connection-factory="server"/>
    <int:channel id="inputChannel"> <int:queue capacity="100" /></int:channel>
    <int:channel id="outputChannel"/>
    <int:service-activator id="loginChannel" input-channel="inputChannel" ref="ESBReceiver" method="getDataFromGL" output-channel="outputChannel">
        <int:poller fixed-rate="500" error-channel="outputChannel" />
    </int:service-activator>
    <int:logging-channel-adapter id="esbLogger" level="DEBUG"/>
    <int:wire-tap channel="esbLogger"></int:wire-tap>

获取请求的java代码:

运行应用程序:

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("/receiver/receiver-esb.xml");
    }

处理请求的类:

    @Component
    public class ESBReceiver {
        public void getDataFromGL(String msg){
            System.out.println("From GL We Get :: " + msg);
            System.out.println("Length :: " + msg.length());
        }
    }

在 ESBReceiver 类中,我正在记录有关用户登录时间和注销时间的数据。

我想从这个类发送确认到我的网络应用程序。 那么有可能吗?如果是,请提供任何指导。

【问题讨论】:

    标签: java spring-integration


    【解决方案1】:

    如果您只想在服务器完成后发送回复;使用 tcp 网关而不是通道适配器。

    如果要发送异步消息,请使用collaborating channel adapters

    【讨论】:

    • @Garu Russell :先生,我正在使用 spring 集成登录和注销条目。我在登录和注销时传递用户的用户 ID。我做对了吗?我已经完成了上面的代码,所以对它进行了任何修改。如果用户密码错误,那么我发送邮件给他只是为了测试目的,一件事是我想要两种方式的通信,两个应用程序都可以相互发送和接收数据所以为此我必须做什么?请任何指导先生..谢谢你。
    【解决方案2】:

    是的,正如@Gary Russell 告诉你的,你可以使用 tcp 网关。

    例子:

       <int-ip:tcp-inbound-gateway id="inGateway"
        request-channel="tcpChannel"
        reply-channel="replyChannel"
        connection-factory="cfServer"
        reply-timeout="10000"/>   
    
       <int-ip:tcp-outbound-gateway id="outGateway"
        request-channel="tcpChannel"
        reply-channel="replyChannel"
        connection-factory="cfClient"
        request-timeout="10000"
        remote-timeout="10000"/>
    

    Here's full documentation.看吧。

    【讨论】:

    • 先生,如果可能的话,你能给我一个小例子吗?我是新手,所以我很困惑。谢谢。
    • 有一个gateway sample here(客户端和服务器端)。完全异步双向有点复杂;您必须使用我在回答中描述的协作适配器。
    • @GaryRussell :先生,如果您不是我的,您能告诉我如何进行双向通信吗?是否有可能我想向所有订阅者发送一条消息,而只向选定的订阅者发送一条消息,那么该怎么办?任何过滤?
    • 当然;一切皆有可能,但我没有时间为你写申请。您需要自己做一些研究,如果有任何具体问题,请回到这里。
    猜你喜欢
    • 1970-01-01
    • 2014-12-24
    • 2016-11-26
    • 1970-01-01
    • 2017-04-28
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多