We need to listen to the port and receive the message and then we need to write it back in the same output stream. we have written custom deserializer which is working fine. but custom serilaizer is not getting called. We have used Spring integration flow.
@Bean
public IntegrationFlow integrationFlow() {
return IntegrationFlows.from(Tcp.inboundGateway(Tcp.nioServer(port)
.deserializer(new TestDeserializer())
.serializer(new TestSerializer())
))
.transform(Transformers.objectToString())
.handle("outboundService", "processAndSendMessage")
.get();
}
Find below Custom Deserializer:
public class TestDeserializer implements Deserializer<String>{
private static final char END_OF_BLOCK = '\u001c';
@Override
public String deserialize(InputStream inputStream) throws IOException {
boolean end_of_message = false;
int characterReceived = 0;
StringBuffer parsedMessage = new StringBuffer();
characterReceived = inputStream.read();
while (!end_of_message) {
characterReceived = inputStream.read();
if (characterReceived == END_OF_BLOCK) {
characterReceived = inputStream.read();
end_of_message = true;
}else {
parsedMessage.append((char) characterReceived);
}
}
String message = parsedMessage.toString();
inputStream.close();
parsedMessage = null;
return message;
}
}
Find below Custom Serializer
public class TestSerializer implements Serializer<String>{
@Override
public void serialize(String object, OutputStream outputStream) throws IOException {
System.out.println("inside Serializer -- "+object);
outputStream.write(object.getBytes());
System.out.println("inside after Serializer -- "+object);
}
}
Custom Serializer is not getting called in the Spring Integration flow.
The
Serializer
is called from aTcp.inboundGateway()
when downstream flow returns a reply.Please, be sure that your
.handle("outboundService", "processAndSendMessage")
returns something. Otherwise there is no point in theTcp.inboundGateway()
.