Polish tasks example

This commit is contained in:
Janne Valkealahti
2015-05-29 15:07:44 +01:00
parent b73e22864f
commit 7fa8c12c30
5 changed files with 147 additions and 67 deletions

View File

@@ -34,6 +34,7 @@ import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.guard.Guard;
import org.springframework.util.ObjectUtils;
@Configuration
public class Application {
@@ -122,7 +123,6 @@ public class Application {
.withInternal()
.source(States.MANUAL)
.action(fixAction())
.state(States.ERROR)
.event(Events.FIX);
}
//end::snippetAB[]
@@ -135,8 +135,9 @@ public class Application {
@Override
public boolean evaluate(StateContext<States, Events> context) {
Map<Object, Object> variables = context.getExtendedState().getVariables();
return !(variables.get("T1").equals(true) && variables.get("T2").equals(true) && variables
.get("T3").equals(true));
return !(ObjectUtils.nullSafeEquals(variables.get("T1"), true)
&& ObjectUtils.nullSafeEquals(variables.get("T2"), true)
&& ObjectUtils.nullSafeEquals(variables.get("T3"), true));
}
};
}
@@ -150,7 +151,9 @@ public class Application {
@Override
public void execute(StateContext<States, Events> context) {
Map<Object, Object> variables = context.getExtendedState().getVariables();
if (variables.get("T1").equals(true)) {
if (ObjectUtils.nullSafeEquals(variables.get("T1"), true)
&& ObjectUtils.nullSafeEquals(variables.get("T2"), true)
&& ObjectUtils.nullSafeEquals(variables.get("T3"), true)) {
context.getStateMachine().sendEvent(Events.CONTINUE);
} else {
context.getStateMachine().sendEvent(Events.FALLBACK);
@@ -166,30 +169,28 @@ public class Application {
@Override
public void execute(StateContext<States, Events> context) {
Map<Object, Object> variables = context.getExtendedState().getVariables();
if (variables.get("T1").equals(true) && variables.get("T2").equals(true)
&& variables.get("T3").equals(true)) {
context.getStateMachine().sendEvent(Events.CONTINUE);
}
variables.put("T1", true);
variables.put("T2", true);
variables.put("T3", true);
context.getStateMachine().sendEvent(Events.CONTINUE);
}
};
}
//end::snippetAD[]
//tag::snippetAE[]
@Bean
public Tasks tasks() {
return new Tasks();
}
//end::snippetAE[]
//tag::snippetAF[]
//tag::snippetAE[]
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
return taskExecutor;
}
//end::snippetAF[]
//end::snippetAE[]
}

View File

@@ -49,14 +49,10 @@ public class Tasks {
stateMachine.sendEvent(Events.RUN);
}
public void cont() {
stateMachine.sendEvent(Events.CONTINUE);
}
public void fix(String task) {
if (tasks.containsKey(task)) {
tasks.put(task, true);
}
public void fix() {
tasks.put("T1", true);
tasks.put("T2", true);
tasks.put("T3", true);
stateMachine.sendEvent(Events.FIX);
}
@@ -68,44 +64,33 @@ public class Tasks {
@StatesOnTransition(target = States.T1)
public void taskT1(ExtendedState extendedState) {
log.info("run task on T1");
sleep(2000);
extendedState.getVariables().put("T1", tasks.get("T1"));
log.info("run task on T1 done");
runTask("T1", extendedState);
}
@StatesOnTransition(target = States.T2)
public void taskT2(ExtendedState extendedState) {
log.info("run task on T2");
sleep(2000);
extendedState.getVariables().put("T2", tasks.get("T2"));
log.info("run task on T2 done");
runTask("T2", extendedState);
}
@StatesOnTransition(target = States.T3)
public void taskT3(ExtendedState extendedState) {
log.info("run task on T3");
sleep(2000);
extendedState.getVariables().put("T3", tasks.get("T3"));
log.info("run task on T3 done");
runTask("T3", extendedState);
}
//tag::snippetA[]
@StatesOnTransition(target = States.AUTOMATIC)
public void automaticFix(ExtendedState extendedState) {
Map<Object, Object> variables = extendedState.getVariables();
if (variables.get("T1").equals(false)) {
variables.put("T1", true);
tasks.put("T1", true);
}
variables.put("T1", true);
tasks.put("T1", true);
}
//end::snippetA[]
@StatesOnTransition(target = States.MANUAL)
public void manualFix(ExtendedState extendedState) {
Map<Object, Object> variables = extendedState.getVariables();
if (variables.get("T2").equals(false)) {
variables.put("T2", true);
tasks.put("T2", true);
}
private void runTask(String task, ExtendedState extendedState) {
log.info("run task on " + task);
sleep(2000);
extendedState.getVariables().put(task, tasks.get(task));
log.info("run task on " + task + " done");
}
private static void sleep(long millis) {

View File

@@ -37,9 +37,9 @@ public class TasksCommands implements CommandMarker {
return tasks.toString();
}
@CliCommand(value = "tasks fix", help = "Fix task")
public void fix(@CliOption(key = {"", "task"}, help = "Task id") String task) {
tasks.fix(task);
@CliCommand(value = "tasks fix", help = "Fix tasks")
public void fix() {
tasks.fix();
}
@CliCommand(value = "tasks fail", help = "Fail task")