Quartz threadpool重新配置

时间:2014-05-21 23:03:19

标签: threadpool quartz-scheduler

我使用Quartz并希望通过远程JMX调用更改它的线程池大小,但遗憾的是无法找到任何正确的解决方案。是否可以通过编程方式更改正在运行的作业的配置?

1 个答案:

答案 0 :(得分:0)

我使用Quartz和spring。在我的web.xml中,我创建了一个Spring ContextListener。我的应用程序启动Quartz作业并公开2个JMX方法以按需启动和停止。

<listener>
    <listener-class>za.co.lance.admin.infrastructure.ui.util.MBeanContextListener</listener-class>
</listener>

像这样的MBeanContextListener类。

public class MBeanContextListener extends ContextLoaderListener {

  private ObjectName objectName;  
  private static Logger logger = LoggerFactory.getLogger(MBeanContextListener.class);

  @Override
  public void contextDestroyed(final ServletContextEvent sce) {
    super.contextDestroyed(sce);
    logger.debug("=============> bean context listener destroy");
    final MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
    try {
      mbeanServer.unregisterMBean(objectName);
      logger.info("=============> QuartzJmx unregisterMBean ok");
    } catch (final Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  public void contextInitialized(final ServletContextEvent sce) {
    super.contextInitialized(sce);
    logger.debug("=============> bean context listener started");
    final MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
    try {
      final QuartzJmx processLatestFailedDocumentsMbean = new QuartzJmx();
      Scheduler scheduler = (Scheduler) ContextLoader.getCurrentWebApplicationContext().getBean("runProcessLatestFailedDocumentsScheduler");
      processLatestFailedDocumentsMbean.setScheduler(scheduler);
      objectName = new ObjectName("za.co.lance.admin.infrastructure.jmx.mbeans:type=QuartzJmxMBean");
      mbeanServer.registerMBean(processLatestFailedDocumentsMbean, objectName);
      logger.info("=============> QuartzJmx registerMBean ok");
    } catch (final Exception e) {
      e.printStackTrace();
    }
  }
}

QuartzJmx类。请注意!任何MBean类(QuartzJmx)都必须有一个以MBean结尾的接口(QuartzJmxMBean)。

@Component
public class QuartzJmx implements QuartzJmxMBean {

  private Scheduler scheduler;

  private static Logger LOG = LoggerFactory.getLogger(QuartzJmx.class);

  @Override
  public synchronized void suspendRunProcessLatestFailedDocumentsJob() {
    LOG.info("Suspending RunProcessLatestFailedDocumentsJob");
    if (scheduler != null) {
      try {
        if (scheduler.isStarted()) {
          scheduler.standby();
          LOG.info("RunProcessLatestFailedDocumentsJob suspended");
        } else {
          LOG.info("RunProcessLatestFailedDocumentsJob already suspended");
          throw new SchedulerException("RunProcessLatestFailedDocumentsJob already suspended");
        }
      } catch (SchedulerException e) {
        LOG.error(e.getMessage());
      }
    } else {
      LOG.error("Cannot suspend RunProcessLatestFailedDocumentsJob. Scheduler = null");
      throw new IllegalArgumentException("Cannot suspend RunProcessLatestFailedDocumentsJob. Scheduler = null");
    }
  }

  @Override
  public synchronized void startRunProcessLatestFailedDocumentsJob() {
    LOG.info("Starting RunProcessLatestFailedDocumentsJob");
    if (scheduler != null) {
      try {
        if (scheduler.isInStandbyMode()) {
          scheduler.start();
          LOG.info("RunProcessLatestFailedDocumentsJob started");
        } else {
          LOG.info("RunProcessLatestFailedDocumentsJob already started");
          throw new SchedulerException("scheduler already started");
        }
      } catch (SchedulerException e) {
        LOG.error(e.getMessage());
      }
    } else {
      LOG.error("Cannot start RunProcessLatestFailedDocumentsJob. Scheduler = null");
      throw new IllegalArgumentException("Cannot start RunProcessLatestFailedDocumentsJob. Scheduler = null");
    }
  }

@Override
  public void setScheduler(Scheduler scheduler) {
    this.scheduler = scheduler;
  }

最后,Spring上下文

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="runProcessLatestFailedDocumentsTask"
        class="za.co.lance.admin.infrastructure.service.vbs.process.ProcessDocumentServiceImpl" />

    <!-- Spring Quartz -->
    <bean name="runProcessLatestFailedDocumentsJob" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass"
            value="za.co.lance.admin.infrastructure.service.quartz.RunProcessLatestFailedDocuments" />
        <property name="jobDataAsMap">
            <map>
                <entry key="processDocumentService" value-ref="runProcessLatestFailedDocumentsTask" />
            </map>
        </property>
    </bean>

    <!-- Cron Trigger -->
    <bean id="processLatestFailedDocumentsTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="runProcessLatestFailedDocumentsJob" />
        <!-- Cron-Expressions (seperated with a space) fields are -->
        <!-- Seconds Minutes Hours Day-of-Month Month Day-of-Week Year(optional) -->
        <!-- Run every morning hour from 9am to 6pm from Monday to Saturday -->
        <property name="cronExpression" value="0 0 9-18 ? * MON-SAT" />
    </bean>

    <!-- Scheduler -->
    <bean id="runProcessLatestFailedDocumentsScheduler"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="runProcessLatestFailedDocumentsJob" />
            </list>
        </property>
        <property name="triggers">
            <list>
                <ref bean="processLatestFailedDocumentsTrigger" />
            </list>
        </property>
    </bean>

</beans>