0
私はSpring Integration TCPクライアントをラップして、アプリケーション用のAPIを提供しています。これに関する以前の質問はhereとhereです。これの問題は、gateway.send()
がまったく終了せず、API応答が返されないことです。ここでSpring TCPソケット統合実装でgateway.sendメソッドが渡されない
は私ServerConnection.java
ファイルである:ここでは
@RequestMapping(value = "/logon", method = RequestMethod.GET)
@ResponseBody
public String logon() {
// logics go here and the result is stored like below and sent
String message = "0000005401F40000C1E3E304010000000020000000000000000000000000000000000000000000004040404040404040C1E3E300C1C2C3C4C5C6C7C8E8C5E2C8E6C1D540F1F7F24BF0F1F64BF0F0F34BF0F5F200";
if (serverConnections.sendData(message)) {
return "Data sent successfully!";
} else {
return "Data not sent!";
}
}
は私の設定がどのように見えるかです::
<context:property-placeholder />
<int:channel id="input" />
<int:channel id="toSA" />
<int:service-activator input-channel="toSA"
ref="echoService"
method="recieveData"/>
<bean id="echoService" class="com.abc.xyz.serverconnection.ServerConnection" />
<bean id="CustomSerializerDeserializer" class="com.abc.xyz.serverconnection.CustomSerializerDeserializer" />
<int:object-to-string-transformer id="serverBytes2String"
input-channel="serverBytes2StringChannel"
output-channel="toSA"/>
<int:gateway id="gw"
service-interface="com.abc.xyz.serverconnection.SimpleGateway"
default-request-channel="input"/>
<int-ip:tcp-connection-factory id="client"
type="client"
host="<ip>"
serializer="CustomSerializerDeserializer"
deserializer="CustomSerializerDeserializer"
port="6100"
single-use="false" />
<int-ip:tcp-outbound-gateway id="outGateway"
request-channel="input"
reply-channel="serverBytes2StringChannel"
connection-factory="client" />
EDIT私のコントローラで
package com.abc.xyz.serverconnection;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ServerConnections {
private SimpleGateway gateway;
public ServerConnections() {
final GenericXmlApplicationContext context = setupContext();
this.setGateway(context.getBean(SimpleGateway.class));
}
public static GenericXmlApplicationContext setupContext() {
final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.load("classpath:META-INF/spring/integration/tcpClientServerDemo-context.xml");
context.registerShutdownHook();
context.refresh();
return context;
}
public SimpleGateway getGateway() {
return gateway;
}
public void setGateway(SimpleGateway gateway) {
this.gateway = gateway;
}
public boolean sendData(String input) {
this.gateway.send(input);
return true;
}
public void recieveData(String output) {
System.out.println("Data from server:" + output);
}
}
、私はこのような何かを:私はTCPクライアントの実装をMaとして使用しているので、アプリケーションのDEBUGログを取得できませんvenモジュールはMavenプロジェクト内にあります。別のモジュールではこれを依存関係として使用し、REST APIのエンドポイントはそこにあります。
私はあなたの<int:service-activator input-channel="toSA"
ref="echoService"
method="recieveData"/>
はその間あなたgateway.send()
がvoid
方法ではありません、replyChannel
ヘッダに送信するすべての結果を返しません。考える
TCPアウトバウンドゲートウェイは5秒後にタイムアウトし、スレッドは決して来ない返答を待つゲートウェイに座ります。 'gw'の' default-reply-timeout'を5000に設定してください。返信を待つ必要がない場合は、メソッドを 'void'を返すように変更するか、' default-reply-timeout'を0に設定します。@artemは言ったように、実際には送信専用操作。 –
@artem @garyに感謝します。私はゲートウェイを無効に戻すように変更しました。これは動作しますが、@ artemのポイントを理解したいと思います。私の 'EchoService'は' void'を返しますが、 'gateway.send()'は文字列を返します。それで、私は 'gateway.send()'の応答を捕まえる必要があることを意味していますか?私はその点を得ていない。 – JackSlayer94
'EchoService'が' void'を返すのは私の矛盾のようです:)(私はあなたがサンプルから名前を得ていることを認識しています)。非voidを返すゲートウェイメソッドは、応答が期待されるフレームワークに指示します。スレッドがゲートウェイに戻ると、スレッドはその応答を待ちます。タイムアウトがありますが、デフォルトでは無限です。あなたのサービスから空を返すので、フローはその時点で終了し、返信は決して到着しません。 'void 'を返すゲートウェイメソッドは、フレームワークに応答が来ないことを伝えます。リクエスト/返信メッセージングの場合は、通常、返信が到着するまで呼び出しスレッドをブロックする方が良いです。 –