我跟着this https://github.com/spring-projects/spring-integration-samples/tree/master/basic/tcp-client-server在 Spring 中设置 TCP 客户端的示例。下面是我的tcpClientServerDemo-context.xml
变压器所在的文件。有人可以帮我移除变压器并按原样发送数据而不进行任何修改吗?如果我尝试删除该行reply-channel='clientBytes2StringChannel'
或者甚至将其设为空,我在构建项目时会遇到异常。
<?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"
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">
<context:property-placeholder />
<!-- Client side -->
<int:gateway id="gw"
service-interface="hello.SimpleGateway"
default-request-channel="input"/>
<int-ip:tcp-connection-factory id="client"
type="client"
host="192.86.33.61"
serializer="CustomSerializerDeserializer"
deserializer="CustomSerializerDeserializer"
port="${availableServerSocket}"
single-use="true"
so-timeout="10000"/>
<bean id="CustomSerializerDeserializer" class="hello.CustomSerializerDeserializer" />
<int:channel id="input" />
<int-ip:tcp-outbound-gateway id="outGateway"
request-channel="input"
connection-factory="client"
request-timeout="10000"
reply-timeout="10000"/>
<!-- Server side -->
<!-- When creating the socket factory on the server side, we specify both the serializer and deserializer
which deals with both accepting a stream formatted with the Stx-Etx bytes as well as sending a stream
formatted with the Stx-Etx bytes. -->
<int-ip:tcp-connection-factory id="serverConnectionFactory"
type="server"
port="${availableServerSocket}"
single-use="true"
so-linger="10000"
serializer="Custom1SerializerDeserializer"
deserializer="Custom1SerializerDeserializer"/>
<bean id="Custom1SerializerDeserializer" class="hello.CustomSerializerDeserializer1" />
<int-ip:tcp-inbound-gateway id="gatewayCrLf"
connection-factory="serverConnectionFactory"
request-channel="incomingServerChannel"
error-channel="errorChannel"/>
<!-- We leave a message listener off of this channel on purpose because we hook
one up before the test actually runs (see the unit test associated with this
context file) -->
<int:channel id="incomingServerChannel" />
</beans>
EDIT:
现在我可以使用自定义序列化器/反序列化器发送消息。但不幸的是,我无法收到回复。这是我的序列化器/反序列化器:
public class CustomSerializerDeserializer implements Serializer<String>, Deserializer<String> {
protected final Log logger = LogFactory.getLog(this.getClass());
public void serialize(String input, OutputStream outputStream) throws IOException {
logger.info("inside serialize");
outputStream.write(buildSampleMsg(input));
outputStream.flush();
}
public String deserialize(InputStream inputStream) throws IOException {
logger.info("inside deserialize");
final int bufferSize = 1024;
final char[] buffer = new char[bufferSize];
final StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(inputStream, "UTF-8");
for (;;) {
int rsz = in.read(buffer, 0, buffer.length);
if (rsz < 0) {
break;
}
out.append(buffer, 0, rsz);
}
logger.info(out.toString());
return out.toString();
}
public byte[] buildSampleMsg(String body){
logger.info("inside buildsamplemsg");
......
return hexStringToByteArray(data);
}
我在序列化器/反序列化器的第一行完成了一些日志记录,但从未打印日志。这反过来意味着我们没有得到任何回应。任何帮助将不胜感激。