2016-02-10 72 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