Last active
July 27, 2017 13:04
-
-
Save pfmiles/e0107a4279c691119b8da00211e39b82 to your computer and use it in GitHub Desktop.
分布式quartz全局调度配置, 支持spring bean自动注入
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.common.quartz; | |
import org.quartz.spi.TriggerFiredBundle; | |
import org.springframework.beans.BeansException; | |
import org.springframework.beans.factory.config.AutowireCapableBeanFactory; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.ApplicationContextAware; | |
import org.springframework.scheduling.quartz.SpringBeanJobFactory; | |
/** | |
* 给quartz的job加上自动依赖注入功能 | |
* | |
* @author pf-miles Jul 23, 2017 6:07:51 PM | |
*/ | |
public class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory | |
implements ApplicationContextAware { | |
private transient AutowireCapableBeanFactory applicationContext; | |
@Override | |
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { | |
Object job = super.createJobInstance(bundle); | |
this.applicationContext.autowireBeanProperties(job, | |
AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false); | |
return job; | |
} | |
@Override | |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | |
this.applicationContext = applicationContext.getAutowireCapableBeanFactory(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 全局分布式scheduler --> | |
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> | |
<property name="dataSource"> | |
<ref bean="zdalDataSource" /> | |
</property> | |
<property name="transactionManager"> | |
<ref bean="dwadsTransactionManager" /> | |
</property> | |
<property name="jobFactory"> | |
<bean class="test.common.quartz.AutowiringSpringBeanJobFactory" /> | |
</property> | |
<property name="quartzProperties"> | |
<map> | |
<entry key="org.quartz.scheduler.instanceName" value="testGlobalScheduler" /> | |
<entry key="org.quartz.scheduler.instanceId" value="AUTO" /> | |
<entry key="org.quartz.scheduler.skipUpdateCheck" value="true" /> | |
<!-- pool --> | |
<entry key="org.quartz.threadPool.class" value="org.quartz.simpl.SimpleThreadPool" /> | |
<entry key="org.quartz.threadPool.threadCount" value="40" /> | |
<!-- job store --> | |
<entry key="org.quartz.jobStore.tablePrefix" value="BUMQTZ_" /> | |
<entry key="org.quartz.jobStore.isClustered" value="true" /> | |
<entry key="org.quartz.jobStore.driverDelegateClass" value="org.quartz.impl.jdbcjobstore.StdJDBCDelegate" /> | |
</map> | |
</property> | |
<!-- 这里添加trigger --> | |
<property name="triggers"> | |
<list> | |
<!-- 每隔60s调度一次 --> | |
<bean name="ddMsgSendingTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> | |
<property name="jobDetail"> | |
<bean name="ddMsgSendingJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> | |
<property name="jobClass" | |
value="test.common.dingding.msg.DingDingMsgSendingJob" /> | |
</bean> | |
</property> | |
<!-- 首次启动延迟ms --> | |
<property name="startDelay" value="60000" /> | |
<!-- 运行间隔ms --> | |
<property name="repeatInterval" value="60000" /> | |
<!-- 错过触发时间的策略 --> | |
<property name="misfireInstructionName" value="MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT" /> | |
</bean> | |
</list> | |
</property> | |
</bean> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.Collection; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Objects; | |
import java.util.Optional; | |
import java.util.concurrent.ForkJoinPool; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
import org.apache.commons.codec.digest.DigestUtils; | |
import org.apache.commons.lang.StringUtils; | |
import org.quartz.DisallowConcurrentExecution; | |
import org.quartz.Job; | |
import org.quartz.JobExecutionContext; | |
import org.quartz.JobExecutionException; | |
import com.alibaba.fastjson.JSON; | |
import com.google.common.collect.Lists; | |
/** | |
* @author pf-miles Jul 19, 2017 9:33:27 PM | |
*/ | |
@DisallowConcurrentExecution | |
public class XxxJob implements Job { | |
private static final ForkJoinPool paraLamPool = new ForkJoinPool(Constants.CPU_NUM * 10); | |
private XXXBean xxxBean; | |
@Override | |
public void execute(JobExecutionContext context) throws JobExecutionException { | |
// ...... | |
} | |
public void setXxxBean(XXXBean bean){ | |
this.xxxBean = bean; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
根据上述配置(定制了表前缀为'BUMQTZ'), 需按照quartz发布包的docs/dbTables下的sql文件指示建立下面的11张表: