0

I have a very simple java class which basically has some code in it - which runs via the Spring scheduler mechanism.

I am not very familiar with JMX - unfortunately, and I have been given a ticket which relates to turning it on or off via a JMX switch. I have been googling JMX, but the questions I see are somewhat different to what I want to do, so I thought I would ask here if that is doable, and if it is, how might I achieve that?

MickeyThreeSheds
  • 986
  • 4
  • 23
  • 42
  • This is a good opportunity to talk to some of the other developers you work with, and see if they have an approach to this. Surely someone before you has implemented this scheduler and has some way to get you closer to your solution. – Makoto Jun 14 '17 at 16:56
  • Unfortunately, nope - the guys who built the early stages of this are all gone. It is a regular old spring scheduler though - I just want to know if JMX has a path to do this. If so, does anybody have a resource they reccomend I look at? – MickeyThreeSheds Jun 14 '17 at 17:16

1 Answers1

1

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;
        }
    }
}

JMX Management

Bohdan Levchenko
  • 3,411
  • 2
  • 24
  • 28