• Spring集成quartz集群配置总结


    1.spring-quartz.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
        <!-- expiration notice -->
        <bean id="expirationNoticeService" class="org.guyezhai.demo.service.scheduler.ExpirationNoticeService"/>
        <bean id="expirationNoticeDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
            <property name="jobClass">
                <value>org.guyezhai.modules.quartz.DXQuartzJobBean</value>
            </property>
            <property name="jobDataMap">
                <map>
                    <entry key="targetObject" value="expirationNoticeService" />
                    <entry key="targetMethod" value="execute" />
                    <entry key="concurrent" value="false" />
                </map>
            </property>
        </bean>
        <bean id="expirationNoticeTriggers" class="org.guyezhai.modules.quartz.InitCronTrigger">
            <constructor-arg value="expirationNoticeTriggers"/>
            <property name="jobDetail" ref="expirationNoticeDetail" />
        </bean>
        
        <bean id="scheduler" lazy-init="true" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="configLocation" value="classpath:quartz.properties"/>
            <property name="triggers">
                <list>
                    <ref local="expirationNoticeTriggers"/>
                </list>
            </property>
            <property name="applicationContextSchedulerContextKey" value="applicationContext" />
        </bean>
    
    </beans>

    2.quartz.properties

    #============================================================================  
    # Configure Main Scheduler Properties
    #============================================================================
    org.quartz.scheduler.instanceName = DEMO
    org.quartz.scheduler.instanceId = AUTO
    
    #============================================================================  
    # Configure ThreadPool
    #============================================================================
    org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
    org.quartz.threadPool.threadCount = 100
    org.quartz.threadPool.threadPriority = 5
    
    #============================================================================
    # Configure JobStore
    #============================================================================
    org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
    org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
    org.quartz.jobStore.useProperties = false
    org.quartz.jobStore.dataSource = demo
    org.quartz.jobStore.tablePrefix = qrtz_
    org.quartz.jobStore.misfireThreshold = 60000
    org.quartz.jobStore.dontSetAutoCommitFalse = false
    org.quartz.jobStore.isClustered = true
    org.quartz.jobStore.clusterCheckinInterval = 10000
    
    #============================================================================
    # Configure Datasources
    #============================================================================
    org.quartz.dataSource.deptusercert.driver = com.mysql.jdbc.Driver
    org.quartz.dataSource.deptusercert.URL = jdbc:mysql://127.0.0.1:3306/demo
    org.quartz.dataSource.deptusercert.user = root
    org.quartz.dataSource.deptusercert.password = sa
    org.quartz.dataSource.deptusercert.maxConnections = 100
    org.quartz.dataSource.deptusercert.validationQuery = select 0 from dual

    3.InitCronTrigger.java

    package org.guyezhai.modules.quartz;
    
    import org.guyezhai.demo.dao.scheduler.SchedulerInfoDao.SchedulerInfoDao;
    import org.guyezhai.demo.entity.po.scheduler.SchedulerInfo.SchedulerInfo;
    import org.guyezhai.modules.spring.context.SpringContextHolder;
    import org.guyezhai.modules.utils.StringUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
    
    /**
     * 初始化CronTrigger
     */
    public class InitCronTrigger extends CronTriggerFactoryBean {
        private static Logger logger = LoggerFactory.getLogger(InitCronTrigger.class);
        
        private String triggerid;
        private SchedulerInfoDao schedulerInfoDao;
        
        public InitCronTrigger(String triggerid) {
            super();
            logger.info("------InitCronTrigger.InitCronTrigger():" + triggerid);
            try {
                schedulerInfoDao = SpringContextHolder.getBean(SchedulerInfoDao.class);
                
                String cronExpression = getCronExpressionFromDB(triggerid);
                if (StringUtils.isNotBlank(cronExpression)) {
                    this.setCronExpression(cronExpression);
                    // logger.info("------setCronExpression:" + this.getCronExpression());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }
        
        /**
         * 从数据库中获取cronExpression
         * 
         * @param triggerid
         * @return
         */
        private String getCronExpressionFromDB(String triggerid) {
            SchedulerInfo schedulerInfo = schedulerInfoDao.get(triggerid);
            return null == schedulerInfo ? "" : schedulerInfo.getCronExpression();
        }
        
        public String getTriggerid() {
            return triggerid;
        }
        
        public void setTriggerid(String triggerid) {
            this.triggerid = triggerid;
        }
    }

    4.DXQuartzJobBean.java

    package org.guyezhai.modules.quartz;
    
    import java.lang.reflect.Method;
    
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.scheduling.quartz.QuartzJobBean;
    
    /**
     * 解决quartz集群class序列化的问题
     */
    public class DXQuartzJobBean extends QuartzJobBean {
        private static Logger logger = LoggerFactory.getLogger(DXQuartzJobBean.class);
        
        private String targetMethod;
        private String targetObject;
        private ApplicationContext applicationContext;
        
        @Override
        protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
            try {
                logger.warn("INFO: execute [" + targetObject + "] at once...");
                Object otargetObject = applicationContext.getBean(targetObject);
                Method m = null;
                try {
                    m = otargetObject.getClass().getMethod(targetMethod, new Class[] {});
                    m.invoke(otargetObject, new Object[] {});
                } catch (SecurityException e) {
                    logger.error("DXQuartzJobBean.executeInternal()", e);
                } catch (NoSuchMethodException e) {
                    logger.error("DXQuartzJobBean.executeInternal()", e);
                }
            } catch (Exception e) {
                // throw new JobExecutionException(e);
                logger.error("DXQuartzJobBean.executeInternal()", e);
            }
        }
        
        public void setApplicationContext(ApplicationContext applicationContext) {
            this.applicationContext = applicationContext;
        }
        
        public void setTargetObject(String targetObject) {
            this.targetObject = targetObject;
        }
        
        public void setTargetMethod(String targetMethod) {
            this.targetMethod = targetMethod;
        }
    }
  • 相关阅读:
    垃圾回收器
    垃圾回收相关概念
    Spring Cloud 框架 -- Spring Cloud Gateway
    Spring Cloud 框架 -- Zuul
    报错:Failed to read artifact descriptor for org.springframework.cloud:spring-cloud-starter-netflix-zuul:jar:2.2.2.RELEASE
    Spring Cloud 框架 -- Resilience4j
    Spring Cloud 框架 -- OpenFeign
    Spring Cloud 框架 -- Eureka 服务的注册与消费
    Spring Cloud 框架 -- Hystrix 的基本介绍与使用
    打包 Spring Boot 项目报错:Failed to execute goal on project provider: Could not resolve dependencies for project com.example:provider:jar:0.0.1-SNAPSHOT
  • 原文地址:https://www.cnblogs.com/guyezhai/p/5431107.html
Copyright © 2020-2023  润新知