Mistre83
Mistre83

Reputation: 2827

Spring Integration - Application always running

I'm build an application that need to be even running (never shutdown).

All the process start from a channel (rootChannel) where right now i start this sending a message.

Looking the example on github repository of Spring Integration i've used the same structure but after sent the message (marked below with // HERE) the application go forward on context.close() (this is correct because i'm not "blocking it")

My question is: how can I do to make sure that application never stop ? (Or stop only when i kill them)

I need this because my app is a monitor and so need to be even running:

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);
    }
}

Upvotes: 0

Views: 558

Answers (1)

Gary Russell
Gary Russell

Reputation: 174809

If your context contains <poller/> s then just exit without closing the context; it will keep running since the poller threads are non-daemon by default.

If you want more control and the ability to shut down cleanly...

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

Upvotes: 1

Related Questions