Add more state events
- Now app context and listener are notified when state is exited or entered.
This commit is contained in:
@@ -57,6 +57,20 @@ public class DefaultStateMachineEventPublisher implements StateMachineEventPubli
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publishStateEntered(Object source, State<?, ?> state) {
|
||||
if (applicationEventPublisher != null) {
|
||||
applicationEventPublisher.publishEvent(new OnStateEntryEvent(source, state));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publishStateExited(Object source, State<?, ?> state) {
|
||||
if (applicationEventPublisher != null) {
|
||||
applicationEventPublisher.publishEvent(new OnStateExitEvent(source, state));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publishTransitionStart(Object source, Transition<?, ?> transition) {
|
||||
if (applicationEventPublisher != null) {
|
||||
|
||||
@@ -30,7 +30,7 @@ public class OnStateChangedEvent extends StateMachineEvent {
|
||||
private final State<?, ?> targetState;
|
||||
|
||||
/**
|
||||
* Instantiates a new granted event.
|
||||
* Instantiates a new on state changed event.
|
||||
*
|
||||
* @param source the component that published the event (never {@code null})
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2015 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.statemachine.event;
|
||||
|
||||
import org.springframework.statemachine.state.State;
|
||||
|
||||
/**
|
||||
* Generic event representing that state has been entered.
|
||||
*
|
||||
* @author Janne Valkealahti
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class OnStateEntryEvent extends StateMachineEvent {
|
||||
|
||||
private final State<?, ?> state;
|
||||
|
||||
/**
|
||||
* Instantiates a new on state entry event.
|
||||
*
|
||||
* @param source the source
|
||||
* @param state the state
|
||||
*/
|
||||
public OnStateEntryEvent(Object source, State<?, ?> state) {
|
||||
super(source);
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the state.
|
||||
*
|
||||
* @return the state
|
||||
*/
|
||||
public State<?, ?> getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OnStateEntryEvent [state=" + state + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2015 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.statemachine.event;
|
||||
|
||||
import org.springframework.statemachine.state.State;
|
||||
|
||||
/**
|
||||
* Generic event representing that state has been exited.
|
||||
*
|
||||
* @author Janne Valkealahti
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class OnStateExitEvent extends StateMachineEvent {
|
||||
|
||||
private final State<?, ?> state;
|
||||
|
||||
/**
|
||||
* Instantiates a new on state exit event.
|
||||
*
|
||||
* @param source the source
|
||||
* @param state the state
|
||||
*/
|
||||
public OnStateExitEvent(Object source, State<?, ?> state) {
|
||||
super(source);
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the state.
|
||||
*
|
||||
* @return the state
|
||||
*/
|
||||
public State<?, ?> getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OnStateExitEvent [state=" + state + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,6 +35,22 @@ public interface StateMachineEventPublisher {
|
||||
*/
|
||||
void publishStateChanged(Object source, State<?, ?> sourceState, State<?, ?> targetState);
|
||||
|
||||
/**
|
||||
* Publish a state entered event.
|
||||
*
|
||||
* @param source the source
|
||||
* @param state the state
|
||||
*/
|
||||
void publishStateEntered(Object source, State<?, ?> state);
|
||||
|
||||
/**
|
||||
* Publish a state exited event.
|
||||
*
|
||||
* @param source the source
|
||||
* @param state the state
|
||||
*/
|
||||
void publishStateExited(Object source, State<?, ?> state);
|
||||
|
||||
/**
|
||||
* Publish a transition start event.
|
||||
*
|
||||
|
||||
@@ -39,6 +39,22 @@ public class CompositeStateMachineListener<S,E> extends AbstractCompositeListene
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateEntered(State<S, E> state) {
|
||||
for (Iterator<StateMachineListener<S, E>> iterator = getListeners().reverse(); iterator.hasNext();) {
|
||||
StateMachineListener<S, E> listener = iterator.next();
|
||||
listener.stateEntered(state);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateExited(State<S, E> state) {
|
||||
for (Iterator<StateMachineListener<S, E>> iterator = getListeners().reverse(); iterator.hasNext();) {
|
||||
StateMachineListener<S, E> listener = iterator.next();
|
||||
listener.stateExited(state);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transition(Transition<S, E> transition) {
|
||||
for (Iterator<StateMachineListener<S, E>> iterator = getListeners().reverse(); iterator.hasNext();) {
|
||||
|
||||
@@ -36,6 +36,20 @@ public interface StateMachineListener<S,E> {
|
||||
*/
|
||||
void stateChanged(State<S,E> from, State<S,E> to);
|
||||
|
||||
/**
|
||||
* Notified when state is entered.
|
||||
*
|
||||
* @param state the state
|
||||
*/
|
||||
void stateEntered(State<S,E> state);
|
||||
|
||||
/**
|
||||
* Notified when state is exited.
|
||||
*
|
||||
* @param state the state
|
||||
*/
|
||||
void stateExited(State<S,E> state);
|
||||
|
||||
/**
|
||||
* Notified when transition happened.
|
||||
*
|
||||
|
||||
@@ -316,6 +316,7 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
|
||||
new HashMap<String, Object>());
|
||||
StateContext<S, E> stateContext = new DefaultStateContext<S, E>(messageHeaders, extendedState, transition, this);
|
||||
state.exit(event != null ? event.getPayload() : null, stateContext);
|
||||
notifyStateExited(state);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,6 +327,7 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
|
||||
new HashMap<String, Object>());
|
||||
StateContext<S, E> stateContext = new DefaultStateContext<S, E>(messageHeaders, extendedState, transition, this);
|
||||
state.entry(event != null ? event.getPayload() : null, stateContext);
|
||||
notifyStateEntered(state);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -544,6 +546,22 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyStateEntered(State<S,E> state) {
|
||||
stateListener.stateEntered(state);
|
||||
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
|
||||
if (eventPublisher != null) {
|
||||
eventPublisher.publishStateEntered(this, state);
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyStateExited(State<S,E> state) {
|
||||
stateListener.stateExited(state);
|
||||
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
|
||||
if (eventPublisher != null) {
|
||||
eventPublisher.publishStateExited(this, state);
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyTransitionStart(Transition<S,E> transition) {
|
||||
stateListener.transitionStarted(transition);
|
||||
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
|
||||
|
||||
@@ -105,6 +105,14 @@ public class ListenerTests extends AbstractStateMachineTests {
|
||||
states.add(new Holder(from, to));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateEntered(State<TestStates, TestEvents> state) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateExited(State<TestStates, TestEvents> state) {
|
||||
}
|
||||
|
||||
static class Holder {
|
||||
State<TestStates, TestEvents> from;
|
||||
State<TestStates, TestEvents> to;
|
||||
|
||||
Reference in New Issue
Block a user