Beh01der / EasyFlow

EasyFlow - Simple and lightweight Finite State Machine for Java
http://datasymphony.com.au/open-source/easyflow
Apache License 2.0
445 stars 86 forks source link

Easyflow never terminates its threads #15

Open cmcconomy opened 10 years ago

cmcconomy commented 10 years ago

I'm using easyflow to manage state change in a file parser and so far it's come in quite handy. However one thing I can't figure out is why after I reach a final state, the context reports itself as terminated, but the flow engine keeps going.

Is there a recommended way to exit the entire FlowEngine when your processing is complete?

EDIT: I found a solution, for anyone looking; create your own Executor class, instantiate your EasyFlow object with it, and call shutdownNow() on the executorservice once your context terminates.

Would be nice to have something like that be a built in option though, or at least the ability to tell EasyFlow to terminate directly.

Avec112 commented 9 years ago

I also miss this. Thanks for the tip cmcconomy.

Avec112 commented 9 years ago

Can't seem to get a hang of it.

Would be pleased if anyone could post an example how to terminate at/after the last state? I do now want to exit the application just terminate the state handling process, like flow.terminate(), stop() or something like that would be nice.

Beh01der commented 9 years ago

You need your own executor:

ExecutorService service = Executors.newSingleThreadExecutor();
flow.executor(service);
flow.start();

and then

        flow.whenFinalState(new StateHandler<StatefulContext>() {
            public void call(StateEnum stateEnum, StatefulContext statefulContext) throws Exception {
                System.out.println("STOP. Nothing more to do.");
                service.shutdown();            
            }
        });
Avec112 commented 9 years ago

Thanks that worked!