石英线程池重新配置



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

我使用石英与弹簧。在我的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>

相关内容

  • 没有找到相关文章

最新更新