Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/task scheduler lifecycle #1063

Open
wants to merge 2 commits into
base: 2.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
package org.springframework.statemachine.buildtests;

import org.junit.Test;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineBuilder;
import org.springframework.statemachine.test.StateMachineTestPlan;
Expand All @@ -25,8 +28,10 @@
public class TimerSmokeTests {

private static ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
private static ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
{
taskExecutor.initialize();
taskScheduler.initialize();
}

private StateMachine<String, String> buildMachine() throws Exception {
Expand All @@ -35,7 +40,8 @@ private StateMachine<String, String> buildMachine() throws Exception {

builder.configureConfiguration()
.withConfiguration()
.taskExecutor(taskExecutor);
.taskExecutor(taskExecutor)
.taskScheduler(taskScheduler);

builder.configureStates()
.withStates()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ public abstract class StateMachineSystemConstants {
/** Bean name for task executor */
public static final String TASK_EXECUTOR_BEAN_NAME = "stateMachineTaskExecutor";

/** Task scheduler threads prefix **/
public static final String TASK_SCHEDULER_THREAD_PREFIX = "spring-state-machine-task-scheduler-";

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
package org.springframework.statemachine.config;

import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.StateMachineException;
import org.springframework.statemachine.StateMachineSystemConstants;
import org.springframework.statemachine.config.builders.StateMachineConfigBuilder;
import org.springframework.statemachine.config.builders.StateMachineConfigurationBuilder;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
Expand All @@ -35,6 +36,11 @@
import org.springframework.statemachine.config.model.ConfigurationData;
import org.springframework.statemachine.config.model.StatesData;
import org.springframework.statemachine.config.model.TransitionsData;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;

import java.util.ArrayList;
import java.util.Collection;

/**
* {@code StateMachineBuilder} provides a builder pattern for
Expand Down Expand Up @@ -147,12 +153,35 @@ public StateMachine<S, E> build() {
} else {
stateMachineFactory.setTaskExecutor(new SyncTaskExecutor());
}

final Collection<StateMachineListener<S, E>> resourceManagementListeners = new ArrayList<>();
if (stateMachineConfigurationConfig.getTaskScheduler() != null) {
stateMachineFactory.setTaskScheduler(stateMachineConfigurationConfig.getTaskScheduler());
} else {
stateMachineFactory.setTaskScheduler(new ConcurrentTaskScheduler());
final ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setThreadNamePrefix(StateMachineSystemConstants.TASK_SCHEDULER_THREAD_PREFIX);
taskScheduler.setPoolSize(1);
taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
taskScheduler.setAwaitTerminationSeconds(10);
taskScheduler.afterPropertiesSet();
stateMachineFactory.setTaskScheduler(taskScheduler);
resourceManagementListeners.add(new StateMachineListenerAdapter<S, E>() {
@Override
public void stateMachineStarted(final StateMachine<S, E> stateMachine) {
taskScheduler.afterPropertiesSet();
}

@Override
public void stateMachineStopped(final StateMachine<S, E> stateMachine) {
taskScheduler.destroy();
}
});
}
final StateMachine<S, E> stateMachine = stateMachineFactory.getStateMachine();
for (final StateMachineListener<S, E> listener : resourceManagementListeners) {
stateMachine.addStateListener(listener);
}
return stateMachineFactory.getStateMachine();
return stateMachine;
} catch (Exception e) {
throw new StateMachineException("Error building state machine", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.statemachine.StateMachineSystemConstants;

/**
Expand All @@ -39,7 +39,7 @@ public TaskExecutor taskExecutor() {

@Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler();
return new ThreadPoolTaskScheduler();
}

@Bean(name = StateMachineHandlerApplicationListener.BEAN_NAME)
Expand Down