SpringMvc Quartz 定时任务集成

文章积累知识,如有存在问题,请大家不啬赐教

1、引入jar

  <!-- quartz -->
  <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz</artifactId>
      <version>2.3.0</version>
  </dependency>

2、spring-task.xml 配置文件

<beans
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">

	<bean class="com.fruits.controller.TimerTask" id="firstTask" />

	<!--定时器1 订单大于7点后,待收货自动签收  begin -->
	<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
			id="timingStatistics">
		<property ref="firstTask" name="targetObject" />
		<property name="targetMethod" value="orderAutoSignIn" />
		<!--指定定时器任务类要执行的方法名称 这里是midownloadItemTask -->
	</bean>
	<bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"
		  id="midDownTrigger">
		<!--配置定时器任务的调度器 -->
		<property ref="timingStatistics" name="jobDetail" />
		<property name="cronExpression" value="0 0 0 * * ?" />
	</bean>

	<!--注册监听器 -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
		  id="registerQuartz">
		<!--注册定时器实体 集合 -->
		<property name="triggers">
			<list>
				<ref local="midDownTrigger" />
			</list>
		</property>
	</bean>
</beans>

3、time

package com.fruits.controller;

import com.fruits.dao.TblOrderMapper;
import com.web.generate.gen.DatabaseUtil;
import com.web.utils.AliyunSendSms;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Component
public class TimerTask {

    @Resource
    private TblOrderMapper orderMapper;

    private static Logger logger = Logger.getLogger(TimerTask.class);

    public void printTimeStamp() {
        Calendar ca = Calendar.getInstance();
        ca.setTimeInMillis(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS ", Locale.CHINA);
        //显示当前时间 精确到毫秒
        System.out.print(sdf.format(ca.getTime()));
    }

    public TimerTask() {
        this.printTimeStamp();
        System.out.println("计划任务被初始化了");
    }

    public void doTask() {
        this.printTimeStamp();
        System.out.print("计划任务被执行,线程id:");
        System.out.println(Thread.currentThread().getId());
    }

    /**
     * 订单已完成大于7天后,待收货自动签收
     */
    public void orderAutoSignIn() {
        CallableStatement cs = null;
        Connection conn = null;
        try {
            //doTask();
            System.out.println("进入定时任务了:orderAutoSignIn");
            conn = DatabaseUtil.getConnection();
            cs = conn.prepareCall("{call automatic_signIn()}");
            cs.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("orderAutoSignIn");
        } finally {
            if (cs != null) {
                try {
                    System.out.println("进入关闭流");
                    cs.close();
                    DatabaseUtil.closeConnection(conn);
                } catch (SQLException e) {
                    logger.error("getColumnTypes close pstem and connection failure", e);
                }
            }
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值