2016-02-10 71 views
0

我構建的應用程序甚至需要運行(從不關閉)。彈簧集成 - 應用程序始終運行

所有的過程從一個通道(rootChannel)開始,現在我開始發送一條消息。

尋找Spring Integration的github存儲庫上的例子,我使用了相同的結構,但在發送消息(標記爲// HERE)後,應用程序繼續前進context.close()(這是正確的,因爲我不是「阻止它」 )

我的問題是:我該如何確保應用程序永不停止? (或者只能停在我殺死他們)

我需要這個,因爲我的應用程序是一個顯示器,因此需要加以甚至跑步:

MainClass.java

public class Application { 
    public static void main(String[] args) throws Exception { 
     AbstractApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext.xml", Application.class); 

     MessageChannel messageChannel = (MessageChannel) context.getBean("rootChannel"); 
     GenericMessage<Document> message = createXmlMessageFromResource("/META-INF/spring/source-file-to-parse.xml"); 
     boolean b = message.send(probeMessage); // HERE I START 

     context.close(); 
    } 

    private static GenericMessage<Document> createXmlMessageFromResource(String path) throws Exception { 
     Resource res = new ClassPathResource(path); 

     DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 
     builderFactory.setNamespaceAware(false); 
     DocumentBuilder builder = builderFactory.newDocumentBuilder(); 

     Document doc = builder.parse(res.getInputStream()); 
     return new GenericMessage<Document>(doc); 
    } 
} 

回答

1

如果上下文包含<poller/>小號然後退出而不關閉上下文;它將繼續運行,因爲默認情況下,輪詢器線程是非守護進程。

如果你想要更多的控制並完全關閉的能力...

System.out.println("Press 'Enter' to terminate"); 
System.in.read(); 
context.close(); 
+0

感謝這兩種解決方案。你給我的第二個是我在早上實施的:) – Mistre83