Question

I'd like to create an Esper engine long running process but I'm not sure of Esper's threading model nor the model I should implement to do this. Naively I tried the following:

public class EsperTest {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    //EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();
   EPServiceProvider epService = EPServiceProviderManager.getProvider("CoreEngine");
   epService.addServiceStateListener(new EPServiceStateListener() {

       @Override
       public void onEPServiceDestroyRequested(EPServiceProvider epsp) {
           System.out.println("Service destroyed");
       }

       @Override
       public void onEPServiceInitialized(EPServiceProvider epsp) {
           System.out.println("System initialised");
       }
   });

   epService.initialize();
}

}

But the code appears to execute to the end of the main() method and the JVM ends.

Referring to the Esper documentation, section 14.7 p456:

In the default configuration, each engine instance maintains a single timer thread (internal timer) providing for time or schedule-based processing within the engine. The default resolution at which the internal timer operates is 100 milliseconds. The internal timer thread can be disabled and applications can instead send external time events to an engine instance to perform timer or scheduled processing at the resolution required by an application.

Consequently I thought that by creating a an engine instance ("CoreEngine") at least one (timer) thread would be created and assuming this is not a daemon thread the main() method would not complete but this appears not to be the case.

Do I have to implement my own infinite loop in main() or is there a configuration which can be provided to Esper which will allow it to run 'forever.?

Was it helpful?

Solution

The timer threads is a daemon thread. Instead of a loop use a latch like this.

final CountDownLatch shutdownLatch = new CountDownLatch(1);
Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {
        shutdownLatch.countDown();
    }
});
shutdownLatch.await();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top