我在弹簧集成方面遇到了一些问题。 可以说,有我的传出通信配置:
<int:channel id="outputChannel">
<int:queue />
</int:channel>
<int:channel id="outputChannel-in"> <!-- for response from server -->
<int:queue />
</int:channel>
<int-ip:tcp-connection-factory
id="outputSocket"
type="client"
single-use="true"
host="localhost"
port="666" />
<int-ip:tcp-outbound-gateway id="outGateway"
request-channel="outputChannel"
reply-channel="outputChannel-in"
connection-factory="outputSocket"
reply-timeout="20000" />
<int:gateway id="myGateway"
service-interface="some.package.SocketGateway"
default-request-channel="outputChannel"
default-reply-channel="outputChannel-in" />
<int:service-activator
id="myServiceActivator"
input-channel="outputChannel-in"
ref="myService"
method="incomingDataHandlingMethod" />
无论我使用的是什么界面some.package.SocketGateway:
选项#1:
public interface SocketGateway{
byte[] send(String text);
}
选项#2:
public interface SocketGateway{
Future<byte[]> send(String text);
}
它没有收到任何消息。 我玩了很多配置。它只是我的解决方案的一个版本,但它们都没有工作。
这是服务器模拟:
ServerSocket someSocket = new ServerSocket(666);
Socket socket = someSocket.accept();
PrintWriter out =
new PrintWriter (socket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
System.out.println("socket accepted");
String data = in.readLine();
while (data != null) {
System.out.println(data);
data = in.readLine();
}
System.out.println("data received");
out.println("ACK");
out.flush();
System.out.println("data sent");
socket.close();
someSocket.close();
当out.println("ACK");
之后,连接关闭且不发送ACK。当out.println("ACK");
在之前或之内时,它会发送消息。
我该怎么做才能收到此消息?
编辑: 我也尝试过:
<int-ip:tcp-outbound-channel-adapter
id="outboundClient"
channel="outputChannel"
connection-factory="outputSocket" />
<int-ip:tcp-inbound-channel-adapter
id="outboundClient-in"
channel="outputChannel-in"
connection-factory="outputSocket" />
没有好结果。
编辑: 我有这个客户端代码:
Socket echoSocket = new Socket("10.20.30.40", 11111);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
String encodedMessage = "someMessage";
String result = String.format("%c%s%c%c", (char) 11, encodedMessage, (char) 28, (char) 13);
out.println(result);
out.flush();
System.out.println("data sent");
File file = new File("result.txt");
BufferedWriter output = new BufferedWriter(new FileWriter(file));
String data;
while ((data = in.readLine()) != null) {
output.write(data);
}
output.flush();
output.close();
System.out.println("after while");
out.close();
in.close();
echoSocket.close();
这适用于外部服务器(服务器正在同步发送ACK消息,这是在字符串数据中接收的。如何通过Spring Integration实现此结果?我无法接收任何内容。 ..
答案 0 :(得分:1)
TCP是一个流;它需要用于分隔消息的结构。默认的反序列化器最终需要CRLF。
发送out.println("ACK\r\n");
。
您可以阅读Serializer
和Deserializer
s here。
修改强>
你的逻辑在几个方面存在缺陷。
PrintWriter.println()
只附加LF;除非你改变反序列化器,否则你需要CRLF。readLine()
上,直到套接字关闭。这很好用:
ServerSocket someSocket = new ServerSocket(1666);
Socket socket = someSocket.accept();
OutputStream out = socket.getOutputStream();
BufferedReader in =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
System.out.println(Thread.currentThread().getName() + " socket accepted");
String data = in.readLine();
System.out.println(data);
System.out.println("data received");
out.write("ACK\r\n".getBytes());
out.flush();
System.out.println("data sent");
socket.close();
someSocket.close();