springasynchronousspring-statemachine

Spring State Machine task execution not firing


I am having issues with getting a runnable to run in the manner described in the following reference:

http://docs.spring.io/autorepo/docs/spring-statemachine/1.0.0.M3/reference/htmlsingle/#statemachine-examples-tasks

TasksHandler handler = TasksHandler.builder()
    .task("1", sleepRunnable())
    .task("2", sleepRunnable())
    .task("3", sleepRunnable())
    .build();
handler.runTasks();

My implementation looks like this:

private Action<States, Events> getUnlockedAction() {
    return new Action() {
        @Override
        public void execute(StateContext sc) {
            System.out.println("in action..");
            handler = new TasksHandler.Builder().taskExecutor(taskExecutor()).task("1", dp.runProcess(1)).build();
            handler.addTasksListener(new MyTasksListener());

            handler.runTasks();

            System.out.println("after action..");
        }

    };
}

The initialization for the TaskExecutor looks like this:

    @Bean
public TaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor te = new ThreadPoolTaskExecutor();
    te.setMaxPoolSize(50);
    te.setThreadNamePrefix("LULExecutor-");
    te.setCorePoolSize(25);
    te.initialize();
    return te;
}

My code for dp (DataProcessor) looks like this:

@Component
@Qualifier("dataProcessor")
public class ADataProcessor {

    public Runnable runProcess(final int i) {
    return new Runnable() {
        @Async
        @Override
        public void run() {
            long delay = (long) ((Math.random() * 10) + 1) * 1000;
            System.out.println("In thread " + i + "... sleep for " + delay);
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ex) {
                Logger.getLogger(FSMFactoryConfig.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("After thread " + i + "...");
        }

    };
    }
}

When i execute my code, I see the messages for 'in action..' and 'after action..' with no delay..

When I use the following:

                taskExecutor().execute(dp.runProcess(1));
            taskExecutor().execute(dp.runProcess(2));
            taskExecutor().execute(dp.runProcess(3));
            taskExecutor().execute(dp.runProcess(4));
            taskExecutor().execute(dp.runProcess(5));
            taskExecutor().execute(dp.runProcess(6));

I get what I would expect from using the TasksHandler..

None of the messages before or after the delay in the sleep are displayed when using the TasksHandler. So my question, how do I actually execute my runnable?? If I'm doing it correctly, what should I check?


Solution

  • I think you've slightly misunderstood few things. First you're linking to tasks sample having the original idea which were turned into a tasks recipe. It's also worth to look unit tests for tasks.

    You register runnables with taskhandler get a state machine from it to start it and then tell handler to run tasks.

    I now realize that in docs I probably should be bit more clear of its usage.