Reactive changes for samples

- Relates #750
This commit is contained in:
Janne Valkealahti
2019-05-11 09:26:13 +01:00
parent 7932545ea9
commit 64779f4039
22 changed files with 208 additions and 82 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,8 @@ import org.springframework.statemachine.config.builders.StateMachineStateConfigu
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.guard.Guard;
import reactor.core.publisher.Mono;
@Configuration
public class Application {
@@ -214,7 +216,10 @@ public class Application {
&& context.getEvent() == Events.PLAY
&& context.getTransition().getTarget().getId() == States.CLOSED
&& context.getExtendedState().getVariables().get(Variables.CD) != null) {
context.getStateMachine().sendEvent(Events.PLAY);
context.getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.PLAY).build()))
.subscribe();
}
}
}
@@ -265,9 +270,11 @@ public class Application {
if (elapsed instanceof Long) {
long e = ((Long)elapsed) + 1000l;
if (e > ((Cd) cd).getTracks()[((Integer) track)].getLength()*1000) {
context.getStateMachine().sendEvent(MessageBuilder
context.getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.FORWARD)
.setHeader(Headers.TRACKSHIFT.toString(), 1).build());
.setHeader(Headers.TRACKSHIFT.toString(), 1).build()))
.subscribe();
} else {
variables.put(Variables.ELAPSEDTIME, e);
}
@@ -291,7 +298,10 @@ public class Application {
variables.put(Variables.ELAPSEDTIME, 0l);
variables.put(Variables.TRACK, next);
} else if (((Cd)cd).getTracks().length <= next) {
context.getStateMachine().sendEvent(Events.STOP);
context.getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.STOP).build()))
.subscribe();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import demo.cdplayer.Application.Headers;
import demo.cdplayer.Application.States;
import demo.cdplayer.Application.StatesOnTransition;
import demo.cdplayer.Application.Variables;
import reactor.core.publisher.Mono;
@WithStateMachine
public class CdPlayer {
@@ -41,37 +42,54 @@ public class CdPlayer {
private String trackStatus = "";
public void load(Cd cd) {
stateMachine.sendEvent(MessageBuilder.withPayload(Events.LOAD).setHeader(Variables.CD.toString(), cd).build());
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.LOAD).setHeader(Variables.CD.toString(), cd).build()))
.subscribe();
}
public void play() {
stateMachine.sendEvent(Events.PLAY);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.PLAY).build()))
.subscribe();
}
public void stop() {
stateMachine.sendEvent(Events.STOP);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.STOP).build()))
.subscribe();
}
public void pause() {
stateMachine.sendEvent(Events.PAUSE);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.PAUSE).build()))
.subscribe();
}
public void eject() {
stateMachine.sendEvent(Events.EJECT);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.EJECT).build()))
.subscribe();
}
public void forward() {
stateMachine
.sendEvent(MessageBuilder
.withPayload(Events.FORWARD)
.setHeader(Headers.TRACKSHIFT.toString(), 1).build());
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.FORWARD)
.setHeader(Headers.TRACKSHIFT.toString(), 1).build()))
.subscribe();
}
public void back() {
stateMachine
.sendEvent(MessageBuilder
.withPayload(Events.BACK)
.setHeader(Headers.TRACKSHIFT.toString(), -1).build());
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.BACK)
.setHeader(Headers.TRACKSHIFT.toString(), -1).build()))
.subscribe();
}
public String getLdcStatus() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
*/
package demo.cdplayer;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;
@@ -22,14 +23,17 @@ import org.springframework.stereotype.Component;
import demo.AbstractStateMachineCommands;
import demo.cdplayer.Application.Events;
import demo.cdplayer.Application.States;
import reactor.core.publisher.Mono;
@Component
public class StateMachineCommands extends AbstractStateMachineCommands<States, Events> {
@CliCommand(value = "sm event", help = "Sends an event to a state machine")
public String event(@CliOption(key = { "", "event" }, mandatory = true, help = "The event") final Events event) {
getStateMachine().sendEvent(event);
getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.subscribe();
return "Event " + event + " send";
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.statemachine.data.RepositoryTransition;
@@ -28,6 +29,8 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import reactor.core.publisher.Mono;
@Controller
public class StateMachineController {
@@ -48,13 +51,16 @@ public class StateMachineController {
StateMachine<String, String> stateMachine = stateMachineFactory.getStateMachine();
StateMachineLogListener listener = new StateMachineLogListener();
stateMachine.addStateListener(listener);
stateMachine.start();
stateMachine.startReactively().block();
if (events != null) {
for (String event : events) {
stateMachine.sendEvent(event);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.blockLast();
}
}
stateMachine.stop();
stateMachine.stopReactively().block();
model.addAttribute("allEvents", getEvents());
model.addAttribute("messages", createMessages(listener.getMessages()));
return "states";

View File

@@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;
@@ -31,6 +32,8 @@ import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import reactor.core.publisher.Mono;
@Controller
public class StateMachineController {
@@ -67,7 +70,10 @@ public class StateMachineController {
StateMachine<String, String> stateMachine = getStateMachine(machine);
if (events != null) {
for (String event : events) {
stateMachine.sendEvent(event);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.blockLast();
}
}
@@ -104,13 +110,13 @@ public class StateMachineController {
if (currentStateMachine == null) {
currentStateMachine = stateMachineService.acquireStateMachine(machineId, false);
currentStateMachine.addStateListener(listener);
currentStateMachine.start();
currentStateMachine.startReactively().block();
} else if (!ObjectUtils.nullSafeEquals(currentStateMachine.getId(), machineId)) {
stateMachineService.releaseStateMachine(currentStateMachine.getId());
currentStateMachine.stop();
currentStateMachine.stopReactively().block();
currentStateMachine = stateMachineService.acquireStateMachine(machineId, false);
currentStateMachine.addStateListener(listener);
currentStateMachine.start();
currentStateMachine.startReactively().block();
}
return currentStateMachine;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 the original author or authors.
* Copyright 2017-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import java.util.EnumSet;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;
@@ -31,6 +32,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import demo.datapersist.StateMachineConfig.Events;
import demo.datapersist.StateMachineConfig.States;
import reactor.core.publisher.Mono;
@Controller
public class StateMachineController {
@@ -61,7 +63,10 @@ public class StateMachineController {
StateMachine<States, Events> stateMachine = getStateMachine(machine);
if (events != null) {
for (Events event : events) {
stateMachine.sendEvent(event);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.blockLast();
}
}
StateMachineContext<States, Events> stateMachineContext = stateMachinePersist.read(machine);
@@ -79,13 +84,13 @@ public class StateMachineController {
if (currentStateMachine == null) {
currentStateMachine = stateMachineService.acquireStateMachine(machineId);
currentStateMachine.addStateListener(listener);
currentStateMachine.start();
currentStateMachine.startReactively().block();
} else if (!ObjectUtils.nullSafeEquals(currentStateMachine.getId(), machineId)) {
stateMachineService.releaseStateMachine(currentStateMachine.getId());
currentStateMachine.stop();
currentStateMachine.stopReactively().block();
currentStateMachine = stateMachineService.acquireStateMachine(machineId);
currentStateMachine.addStateListener(listener);
currentStateMachine.start();
currentStateMachine.startReactively().block();
}
return currentStateMachine;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,6 +29,8 @@ import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import reactor.core.publisher.Mono;
@Controller
public class StateMachineController {
@@ -59,7 +61,10 @@ public class StateMachineController {
}
}
listener.resetMessages();
stateMachine.sendEvent(MessageBuilder.createMessage(event, new MessageHeaders(headers)));
stateMachine
.sendEvent(Mono.just(MessageBuilder
.createMessage(event, new MessageHeaders(headers))))
.blockLast();
}
model.addAttribute("states", stateMachine.getState().getIds());
model.addAttribute("messages", listener.getMessages());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package demo.eventservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.persist.StateMachinePersister;
import org.springframework.stereotype.Controller;
@@ -31,6 +32,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
import demo.eventservice.StateMachineConfig.Events;
import demo.eventservice.StateMachineConfig.States;
import reactor.core.publisher.Mono;
@Controller
public class StateMachineController {
@@ -85,7 +87,10 @@ public class StateMachineController {
//tag::snippetD[]
private void feedMachine(String user, Events id) throws Exception {
stateMachine.sendEvent(id);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(id).build()))
.blockLast();
stateMachinePersister.persist(stateMachine, "testprefix:" + user);
}
//end::snippetD[]

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,12 +18,15 @@ package demo.monitoring;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import reactor.core.publisher.Mono;
@Controller
public class StateMachineController {
@@ -39,13 +42,16 @@ public class StateMachineController {
public String feedAndGetStates(@RequestParam(value = "events", required = false) List<String> events, Model model) throws Exception {
StateMachineLogListener listener = new StateMachineLogListener();
stateMachine.addStateListener(listener);
stateMachine.start();
stateMachine.startReactively().block();
if (events != null) {
for (String event : events) {
stateMachine.sendEvent(event);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.blockLast();
}
}
stateMachine.stop();
stateMachine.stopReactively().block();
model.addAttribute("allEvents", new String[]{"E1", "E2"});
model.addAttribute("messages", createMessages(listener.getMessages()));
return "states";

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,6 +31,8 @@ import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import reactor.core.publisher.Mono;
@Controller
public class StateMachineController {
@@ -82,7 +84,10 @@ public class StateMachineController {
headers.put("payment", true);
}
}
stateMachine.sendEvent(MessageBuilder.createMessage(event, new MessageHeaders(headers)));
stateMachine
.sendEvent(Mono.just(MessageBuilder
.createMessage(event, new MessageHeaders(headers))))
.blockLast();
}
model.addAttribute("allIds", machines.keySet());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,19 +15,23 @@
*/
package demo.persist;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;
import demo.AbstractStateMachineCommands;
import reactor.core.publisher.Mono;
@Component
public class StateMachineCommands extends AbstractStateMachineCommands<String, String> {
@CliCommand(value = "sm event", help = "Sends an event to a state machine")
public String event(@CliOption(key = { "", "event" }, mandatory = true, help = "The event") final String event) {
getStateMachine().sendEvent(event);
getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.subscribe();
return "Event " + event + " send";
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package demo.scope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@@ -25,6 +26,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import demo.scope.StateMachineConfig.Events;
import demo.scope.StateMachineConfig.States;
import reactor.core.publisher.Mono;
@Controller
public class StateMachineController {
@@ -44,7 +46,10 @@ public class StateMachineController {
@RequestMapping("/states")
public String getStates(@RequestParam(value = "event", required = false) Events event, Model model) {
if (event != null) {
stateMachine.sendEvent(event);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.blockLast();
}
model.addAttribute("states", stateMachine.getState().getIds());
model.addAttribute("stateChartModel", stateChartModel);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@@ -27,6 +28,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import demo.security.StateMachineConfig.Events;
import demo.security.StateMachineConfig.States;
import reactor.core.publisher.Mono;
@Controller
public class StateMachineController {
@@ -49,7 +51,10 @@ public class StateMachineController {
public String getStates(@RequestParam(value = "event", required = false) Events event, Model model) {
if (event != null) {
try {
stateMachine.sendEvent(event);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.blockLast();
} catch (Exception e) {
log.error("Error sendEvent", e);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
*/
package demo.showcase;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;
@@ -22,14 +23,17 @@ import org.springframework.stereotype.Component;
import demo.AbstractStateMachineCommands;
import demo.showcase.Application.Events;
import demo.showcase.Application.States;
import reactor.core.publisher.Mono;
@Component
public class StateMachineCommands extends AbstractStateMachineCommands<States, Events> {
@CliCommand(value = "sm event", help = "Sends an event to a state machine")
public String event(@CliOption(key = { "", "event" }, mandatory = true, help = "The event") final Events event) {
getStateMachine().sendEvent(event);
getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.subscribe();
return "Event " + event + " send";
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -54,13 +54,13 @@ public class AbstractStateMachineCommands<S, E> implements CommandMarker {
@CliCommand(value = "sm start", help = "Start a state machine")
public String start() {
stateMachine.start();
stateMachine.startReactively().subscribe();
return "State machine started";
}
@CliCommand(value = "sm stop", help = "Stop a state machine")
public String stop() {
stateMachine.stop();
stateMachine.stopReactively().subscribe();
return "State machine stopped";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ import java.util.Map;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.shell.Bootstrap;
import org.springframework.statemachine.StateContext;
@@ -37,6 +38,8 @@ import org.springframework.statemachine.config.builders.StateMachineTransitionCo
import org.springframework.statemachine.guard.Guard;
import org.springframework.util.ObjectUtils;
import reactor.core.publisher.Mono;
@Configuration
public class Application {
@@ -155,9 +158,15 @@ public class Application {
if (ObjectUtils.nullSafeEquals(variables.get("T1"), true)
&& ObjectUtils.nullSafeEquals(variables.get("T2"), true)
&& ObjectUtils.nullSafeEquals(variables.get("T3"), true)) {
context.getStateMachine().sendEvent(Events.CONTINUE);
context.getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.CONTINUE).build()))
.subscribe();
} else {
context.getStateMachine().sendEvent(Events.FALLBACK);
context.getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.FALLBACK).build()))
.subscribe();
}
}
};
@@ -173,7 +182,10 @@ public class Application {
variables.put("T1", true);
variables.put("T2", true);
variables.put("T3", true);
context.getStateMachine().sendEvent(Events.CONTINUE);
context.getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.CONTINUE).build()))
.subscribe();
}
};
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
*/
package demo.tasks;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;
@@ -22,14 +23,17 @@ import org.springframework.stereotype.Component;
import demo.AbstractStateMachineCommands;
import demo.tasks.Application.Events;
import demo.tasks.Application.States;
import reactor.core.publisher.Mono;
@Component
public class StateMachineCommands extends AbstractStateMachineCommands<States, Events> {
@CliCommand(value = "sm event", help = "Sends an event to a state machine")
public String event(@CliOption(key = { "", "event" }, mandatory = true, help = "The event") final Events event) {
getStateMachine().sendEvent(event);
getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.subscribe();
return "Event " + event + " send";
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.ExtendedState;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.annotation.WithStateMachine;
@@ -28,6 +29,7 @@ import org.springframework.statemachine.annotation.WithStateMachine;
import demo.tasks.Application.Events;
import demo.tasks.Application.States;
import demo.tasks.Application.StatesOnTransition;
import reactor.core.publisher.Mono;
@WithStateMachine
public class Tasks {
@@ -46,14 +48,20 @@ public class Tasks {
}
public void run() {
stateMachine.sendEvent(Events.RUN);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.RUN).build()))
.subscribe();
}
public void fix() {
tasks.put("T1", true);
tasks.put("T2", true);
tasks.put("T3", true);
stateMachine.sendEvent(Events.FIX);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.FIX).build()))
.subscribe();
}
public void fail(String task) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
*/
package demo.turnstile;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;
@@ -22,14 +23,17 @@ import org.springframework.stereotype.Component;
import demo.AbstractStateMachineCommands;
import demo.turnstile.Application.Events;
import demo.turnstile.Application.States;
import reactor.core.publisher.Mono;
@Component
public class StateMachineCommands extends AbstractStateMachineCommands<States, Events> {
@CliCommand(value = "sm event", help = "Sends an event to a state machine")
public String event(@CliOption(key = { "", "event" }, mandatory = true, help = "The event") final Events event) {
getStateMachine().sendEvent(event);
getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.subscribe();
return "Event " + event + " send";
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
*/
package demo.washer;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;
@@ -22,13 +23,17 @@ import org.springframework.stereotype.Component;
import demo.AbstractStateMachineCommands;
import demo.washer.Application.Events;
import demo.washer.Application.States;
import reactor.core.publisher.Mono;
@Component
public class StateMachineCommands extends AbstractStateMachineCommands<States, Events> {
@CliCommand(value = "sm event", help = "Sends an event to a state machine")
public String event(@CliOption(key = { "", "event" }, mandatory = true, help = "The event") final Events event) {
getStateMachine().sendEvent(event);
getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.subscribe();
return "Event " + event + " send";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
import demo.web.StateMachineConfig.Events;
import demo.web.StateMachineConfig.States;
import reactor.core.publisher.Mono;
@Controller
public class StateMachineController {
@@ -154,7 +155,7 @@ public class StateMachineController {
.withPayload(id)
.setHeader("testVariable", testVariable)
.build();
stateMachine.sendEvent(message);
stateMachine.sendEvent(Mono.just(message)).subscribe();
}
@RequestMapping("/join")

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,19 +15,23 @@
*/
package demo.zookeeper;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;
import demo.AbstractStateMachineCommands;
import reactor.core.publisher.Mono;
@Component
public class StateMachineCommands extends AbstractStateMachineCommands<String, String> {
@CliCommand(value = "sm event", help = "Sends an event to a state machine")
public String event(@CliOption(key = { "", "event" }, mandatory = true, help = "The event") final String event) {
getStateMachine().sendEvent(event);
getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.subscribe();
return "Event " + event + " send";
}
}
}