I would recommend you to start with a simplest possible solution. Make a private boolean field enabled
in task class and in scheduled
methods check if that field set to true
then proceed, otherwise stop execution.
After that all you have to do is publish your task beans to JMX. It is fairly simple, just try to follow conventions. Here is a simple example:
@EnableScheduling
@SpringBootApplication
public class So44550534Application {
public static void main(String[] args) {
SpringApplication.run(So44550534Application.class, args);
}
public interface TaskMBean {
void setEnabled(boolean enabled);
}
@Component
public static class Task implements TaskMBean {
private static final Logger log = LoggerFactory.getLogger(Task.class);
private boolean enabled = true;
@PostConstruct
private void init() throws Exception {
final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
mBeanServer.registerMBean(this, new ObjectName(this.getClass().getSimpleName(), "name", "control"));
}
@Scheduled(fixedRate = 1000L)
public void run() {
if (enabled) {
log.info("Task::Running");
}
}
@Override
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
}
