String commandType = message.getCommandType();
String taskName = getTaskName(commandType);
if (StartMatcher.matcher(commandType).matches()) {
AsyncTask task = ctx.getAttribute(AsyncTask.class, taskName);
// there's no task running in this context.
if (task == null) {
ResourceProvider<Double> randomNumberProvider = new ResourceProvider<Double>() {
public Double get() {
return Math.random();
}
};
task = MessageBuilder.createConversation(message)
.subjectProvided()
.withProvided("Data", randomNumberProvider)
.noErrorHandling()
.replyRepeating(TimeUnit.MILLISECONDS, 50);
System.out.println("New task started: " + taskName);
ctx.setAttribute(taskName, task);
} else {
System.out.println("Task already started: " + taskName);
}
} else if (StopMatcher.matcher(commandType).matches()) {
AsyncTask task = ctx.getAttribute(AsyncTask.class, taskName);
if (task == null) {
System.out.println("Nothing to stop: " + taskName);
} else {
System.out.println("Stopping: " + taskName);
task.cancel(true);
ctx.removeAttribute(taskName);
}
}
}