[java]spring集成quartz定时任务

版权声明:本文为博主原创文章,未经博主允许不得转载。有任何问题请邮件联系[email protected] https://blog.csdn.net/drdongshiye/article/details/83117420

quartz适合单系统定时任务,分布式不适合,废话少说开始 使用
1.

	<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>2.2.3</version>
		</dependency>
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz-jobs</artifactId>
			<version>2.2.3</version>
		</dependency>

2.spring-quartz.xml 文件配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context/spring-context.xsd
        "
       default-autowire="byType">
    <bean id="biCronJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="biTaskCronService" />
        <property name="targetMethod" value="run" />
        <property name="concurrent" value="false" />
        <property name="arguments">
            <list></list>
        </property>
    </bean>

    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="biCronJob" />
        <property name="cronExpression" value="0 */5 * * * ?" />
    </bean>
    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
          autowire="no">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
            </list>
        </property>
    </bean>

</beans>

3代码展示

/**
 * @author :dongshuo
 * @date : 2018/10/13 16:00
 * @desc : 定时执行任务
 */
@Service("biTaskCronService")
public class BiTaskCronService implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(BiTaskCronService.class);

    @Autowired
    TBiTaskAction tBiTaskAction;

    @Override
    public void run() {

        try {
            logger.info("BiTaskCronService run");
            tBiTaskAction.startTask();
        } catch (Exception e) {
            logger.error("BiTaskCronService exception",e);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/drdongshiye/article/details/83117420