示例https://github.com/spring-projects/spring-integration-samples/tree/master/basic/tcp-client-server非常適合構建TCP服務器應用程序。它很簡單,可以在JVM上運行。它不需要任何應用程序服務器。如何使TCP服務器運行直至停止
示例使用命令行輸入來運行程序。我希望服務器只接受來自Socket端口的數據,而不是通過命令行。如果我刪除命令行輸入,則主線程正在完成,程序不再接受來自端口的輸入。我必須始終保持主線程運行。
我想有些事情是這樣的:
boolean isGatewayStopped = false;
while (!isGatewayStopped) {
try {
Thread.sleep(5000);
isGatewayStopped = getGatewayStatus();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
我有兩個問題:
有使主線程繼續運行的一個乾淨的方式?
如何知道網關已停止?如果用戶以「退出」方式發送TCP數據,則可以停止網關。有什麼方法可以知道網關已停止嗎?
感謝
我不想從命令行輸入數據,因爲它是一臺服務器。春天有什麼實用程序可以啓動守護程序線程並查詢網關的狀態嗎? – kevin
在我的答案中查看編輯。 –
這就是完美的阿爾喬姆。快速問題: 有關使用控制通道,請參閱https://github.com/spring-projects/spring-integration-samples/tree/master/intermediate/monitoring。 消息 operation = MessageBuilder.withPayload(「@ integrationMBeanExporter.stopActiveComponents(false,20000)」)。build();如果(this.exporter!= null){ } this.controlBusChannel.send(operation); } 如果我發送stopActiveComponents()使用控制通道,它會發送ContextClosedEvent? –
kevin