有谁知道使用Spring的tcp入站通道适配器CLIENT示例的简单示例?我想创建一个简单的TCP客户端,它向服务器发送一个简短的字符串,并且只接收一个字节作为答案,然后关闭套接字。这里是我的bean定义:是否有任何弹簧集成tcp入站通道适配器的例子?
<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
。但是,结果参数(在main
方法中)永远不会得到OK
或NOK
字符串值,并且套接字将保持打开状态。
我在哪里犯了一个错误?如果我改变了tcp-inbound-channel-adapter
和tcp-outbound-channel-adapter
对来tcp-outbound-gateway
它工作正常...
我感谢您的帮助! – 2014-10-29 21:25:51