Reputation: 135
I want to write a perfect client-server application is Spring Integration. I have a part where server receive a message and send response to client.
I would like to send to client a message with some information from time to time also. I set a header with connectionId received in TcpConnectionEvent but there is nothing happend. There is my code bellow. I stack with this problem from few days. Thanks for any halp!
<!-- CLIENT SIDE -->
<int:gateway id="gw"
service-interface="com.app.hos.service.client.Gateway"
default-request-channel="input"/>
<int-ip:tcp-connection-factory id="client"
type="client"
host="localhost"
port="14020"
single-use="false"
so-timeout="10000"
/>
<int:channel id="input" />
<int-ip:tcp-outbound-gateway id="outGateway"
request-channel="transformChannel"
reply-channel="reply"
connection-factory="client"
request-timeout="10000"
reply-timeout="10000"
/>
<int:channel id="transformChannel" />
<int:channel id="reply" datatype="java.lang.String" />
<!-- TRANSFORMERS -->
<int:transformer id="testTransformer" ref="testTransformerBean" input-channel="input"
method="transform" output-channel="transformChannel"/>
<bean id="testTransformerBean" class="com.app.hos.service.integration.Transformer" />
<!-- SERVER SIDE -->
<bean id="connectionSerializeDeserialize" class="com.app.hos.service.integration.ByteArrayToStringConverter"/>
<int-ip:tcp-connection-factory id="hosServer"
type="server"
port="14020"
serializer="connectionSerializeDeserialize"
deserializer="connectionSerializeDeserialize"
using-nio="true"/>
<int-ip:tcp-inbound-gateway id="inputHosGateway"
connection-factory="hosServer"
request-channel="toServerChannel"
error-channel="errorChannel"/>
<int:channel id="toServerChannel"/>
<int:channel id="errorChannel"/>
<int:channel id="inputChannel" />
<int:service-activator input-channel="toServerChannel"
ref="server"
method="serverTest"/>
<bean id="server"
class="com.app.hos.service.server.Server" />
<!-- TCP EVENTS -->
<int:service-activator input-channel="eventChannel"
ref="event"
method="eventTest"/>
<bean id="event"
class="com.app.hos.service.integration.Event" />
<int:channel id="eventChannel"/>
<int-event:inbound-channel-adapter channel="eventChannel"
event-types="org.springframework.integration.ip.tcp.connection.TcpConnectionEvent"/>
Transforemr where I set connectionId:
@Autowired
public Event event;
public Message<String> transform(Message<String> msg) {
Message<String> newMessage = MessageBuilder.withPayload(msg.getPayload())
.setHeader(IpHeaders.CONNECTION_ID, event.getConncetionId())
.copyHeadersIfAbsent(msg.getHeaders())
.build();
return newMessage;
}
MVC Controller where I try send a message by a gateway:
@Autowired
public TestController(Gateway gateway) {
this.gateway = gateway;
}
@RequestMapping(value = "/showTest", method=RequestMethod.GET)
public String showTestPage() {
return "test/sendMessageTest";
}
@RequestMapping(value = "/sendMessage", method=RequestMethod.GET)
public void sendMessage() {
gateway.send("Working!");
}
Upvotes: 1
Views: 2572
Reputation: 135
Thanks for help! That's the solution:
<int:channel id="input" />
<int-ip:tcp-outbound-channel-adapter id="outboundChannel"
channel="transformChannel"
connection-factory="hosServer" />
<int-ip:tcp-inbound-channel-adapter id="inboundChannel"
channel="toServerChannel"
connection-factory="hosServer"
/>
<int:channel id="transformChannel" />
<int:channel id="reply" datatype="java.lang.String" />
<!-- TRANSFORMERS -->
<int:transformer id="testTransformer" ref="testTransformerBean" input-channel="input"
method="transform" output-channel="transformChannel"/>
<bean id="testTransformerBean" class="com.app.hos.service.integration.Transformer" />
<!-- SERVER SIDE -->
<bean id="connectionSerializeDeserialize" class="com.app.hos.service.integration.ByteArrayToStringConverter"/>
<int-ip:tcp-connection-factory id="hosServer"
type="server"
port="14020"
serializer="connectionSerializeDeserialize"
deserializer="connectionSerializeDeserialize"
using-nio="true"/>
<int:channel id="toServerChannel"/>
<int:channel id="errorChannel"/>
<int:channel id="inputChannel" />
<int:service-activator input-channel="toServerChannel"
ref="server"
method="serverTest"/>
<bean id="server"
class="com.app.hos.service.server.Server" />
<!-- TCP EVENTS -->
<int:service-activator input-channel="eventChannel"
ref="event"
method="eventTest"/>
<bean id="event"
class="com.app.hos.service.integration.Event" />
<int:channel id="eventChannel"/>
<int-event:inbound-channel-adapter channel="eventChannel"
event-types="org.springframework.integration.ip.tcp.connection.TcpConnectionEvent"/>
Upvotes: 1
Reputation: 174769
You can't use gateways when sending arbitrary data they are strictly for request/reply messaging.
In any case, you are sending to a completely different connection.
Instead of an inbound gateway, you need an inbound channel adapter and outbound channel adapter (sharing the server connection factory).
When you want to send arbitrary data (not part of a request/reply), send the message to the oubound channel adapter, with the connection id header set appropriately.
Upvotes: 1