草庐IT

spring - 是否有任何 spring 集成 tcp-inbound-channel-adapter 示例?

coder 2023-09-18 原文

有谁知道使用 Spring 的 tcp-inbound-channel-adapter CLIENT 示例的简单示例?我想创建一个简单的 TCP 客户端,它向服务器发送一个短字符串,只接收一个字节作为应答,然后关闭套接字。这是我的 bean 定义:

<int-ip:tcp-connection-factory id="client2" type="client"
    host="localhost" port="${availableServerSocket}" single-use="true"
    so-timeout="10000" deserializer="climaxDeserializer"
    so-keep-alive="false" />

<int:service-activator input-channel="clientBytes2StringChannel"
    method="valaszjott" ref="echoService">
</int:service-activator>

<int:gateway
    service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
    id="gw2" default-request-channel="gwchannel">
</int:gateway>
<int:channel id="gwchannel"></int:channel>
<int:object-to-string-transformer input-channel="gwchannel"
    id="clientbyte2string" output-channel="outputchannel">
</int:object-to-string-transformer>
<int:channel id="outputchannel"></int:channel>
<int-ip:tcp-outbound-channel-adapter channel="outputchannel"
    id="clientoutboundadapter" connection-factory="client2">
</int-ip:tcp-outbound-channel-adapter>
<int-ip:tcp-inbound-channel-adapter id="clientinboundadapter"
    channel="inputchannel" connection-factory="client2" />
<int:channel id="inputchannel"></int:channel>
<int:service-activator ref="echoService" method="valaszjott"
    input-channel="inputchannel" id="sa2">
</int:service-activator>

所以,我从我的主要方法中以这种方式使用它:

....
SimpleGateway gateway = (SimpleGateway) context.getBean("gw2");
String result = gateway.send("foo");
....

于是客户端向服务器发送"foo"+/r/n。在服务器端,我收到此消息,服务器仅响应客户端一个字节,( 06H ) 而没有 /r/n。客户端收到它,反序列化器找到它。这是我的反序列化器类:

@Component("climaxDeserializer")public class ClimaxDeserializer implements
Deserializer<Integer>{
    public Integer deserialize(InputStream arg0) throws IOException {
        int ertek;
        do{
            ertek = arg0.read();
            if ( ertek == 6){
                System.out.println("We have the ack byte !");
                return 1;
            }
        } while( ertek >= 0);
        return null;
    }
}

解串器找到ack字节,方法返回一个整数,值为1。 服务激活器指向这个方法:

public String valaszjott ( int success){
        System.out.println("Answer: " + success);
        if ( success == 1){
            return "OK";
        } else {
            return "NOK";
        }
    }

此时一切正常,valaszjott 方法打印出 Answer: 1。但是 result 参数(在 main 方法中)永远不会得到 OKNOK 字符串值,并且套接字将保持打开状态。

我哪里弄错了?如果我将 tcp-inbound-channel-adaptertcp-outbound-channel-adapter 对更改为 tcp-outbound-gateway 它工作正常...

最佳答案

你的错误在于multi-threading .

<int:gateway>调用存在于一个线程中,但是 <int-ip:tcp-inbound-channel-adapter>message-driven组件,它是它自己的线程中的套接字的监听器。对于最后一个,无论您是否调用您的网关都无关紧要:服务器端始终可以将数据包发送到该套接字,您的适配器将接收它们。

为您的ack用例 <tcp-outbound-gateway>是最好的解决方案,因为你真的有一个 correlation在请求和回复之间。有了它,您就可以通过多个并发请求获得 yield 。

只需<tcp-outbound-channel-adapter><tcp-inbound-channel-adapter>不保证回复将按照与发送请求相同的顺序从服务器返回。

无论如何,在当前的解决方案中 gateway只是不知道回复,最后一个无法发送到 replyChannel来自请求消息 header 。

从另一边的套接字不会被关闭,因为它们是cached<int-ip:tcp-connection-factory> .

HTH

关于spring - 是否有任何 spring 集成 tcp-inbound-channel-adapter 示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26619138/

有关spring - 是否有任何 spring 集成 tcp-inbound-channel-adapter 示例?的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  3. ruby - 检查数组是否在增加 - 2

    这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife

  4. ruby-on-rails - 带 Spring 锁的 Rails 4 控制台 - 2

    我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.

  5. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  6. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  7. ruby-on-rails - Ruby 检查日期时间是否为 iso8601 并保存 - 2

    我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby​​是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查

  8. ruby-on-rails - 如何使辅助方法在 Rails 集成测试中可用? - 2

    我在app/helpers/sessions_helper.rb中有一个帮助程序文件,其中包含一个方法my_preference,它返回当前登录用户的首选项。我想在集成测试中访问该方法。例如,这样我就可以在测试中使用getuser_path(my_preference)。在其他帖子中,我读到这可以通过在测试文件中包含requiresessions_helper来实现,但我仍然收到错误NameError:undefinedlocalvariableormethod'my_preference'.我做错了什么?require'test_helper'require'sessions_hel

  9. ruby - 检查日期是否在过去 7 天内 - 2

    我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/

  10. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

随机推荐