[Spring] Java spring quartz 定时任务
首先,需要导入quartz 的jar包 ① applicationContext.xml
<!-- 轮询任务 -->
<import resource="classpath:/conf/quartz/ctmanage-schedule.xml" /> ② ctmanage-schedule.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 配置该项目的定时任务 --> <!-- 生成账单-定义调用对象和调用对象的方法 -->
<bean id="generateBillTaskMethod"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="generateBillService" />
<property name="targetMethod" value="generateBill" />
<property name="concurrent" value="false" />
</bean> <!-- 生成账单-定义触发时间 -->
<!-- 0 0 2 1 * ? 每个月1号凌晨2点执行 正式-->
<!-- 1/15 * * * * ? 启动就执行 每隔15秒执行一次-->
<!-- 0 0/3 * * * ? 每三分钟执行一次 启动服务器三分钟后执行第一次-->
<bean id="generateBillTaskMethodTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="generateBillTaskMethod" />
<property name="cronExpression">
<value>0 0 2 1 * ?</value>
</property>
</bean> <!-- 轮询任务列表 -->
<bean id="timerFactory"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="generateBillTaskMethodTrigger" />
</list>
</property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
<prop key="org.quartz.threadPool.threadCount">10</prop>
<prop key="org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread">true</prop>
</props>
</property>
<property name="startupDelay" value="5"></property>
</bean>
</beans> ③ 配置实现类
<!-- 定时任务 配置 -->
<bean id="generateBillService" class="com.bontai.ct.manager.helper.timed.GenerateBillService">
<property name="generateBillBiz" ref="generateBillBiz" />
</bean>
<bean id="generateBillBiz" class="com.bontai.ct.manager.helper.timed.impl.GenerateBillBizImpl">
<property name="ctUserDAO" ref="ctUserDAO"/>
<property name="ctGroupDAO" ref="ctGroupDAO"/>
<property name="fmOrderDAO" ref="fmOrderDAO"/>
<property name="acBillDAO" ref="acBillDAO"/>
<property name="acBillOrderDAO" ref="acBillOrderDAO"/>
<property name="billDAO" ref="billDAO"/>
</bean> ④ 代码
//service
package com.bontai.ct.manager.helper.timed;
public class GenerateBillService { private GenerateBillBiz generateBillBiz;
public GenerateBillBiz getGenerateBillBiz() {
return generateBillBiz;
}
public void setGenerateBillBiz(GenerateBillBiz generateBillBiz) {
this.generateBillBiz = generateBillBiz;
} //定时任务-生成账单(每月1日为所有机构自动生成账单)
public synchronized void generateBill(){
System.out.println("=================开始执行轮询任务==================");
this.generateBillBiz.generateBill();
} } //biz
package com.bontai.ct.manager.helper.timed;
public interface GenerateBillBiz {
public void generateBill();
} //bizImpl
package com.bontai.ct.manager.helper.timed.impl; import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.springframework.transaction.annotation.Transactional; import com.bontai.ct.manager.dao.custom.mybatis.BillDAO;
import com.bontai.ct.manager.dao.db.AcBillDAO;
import com.bontai.ct.manager.dao.db.AcBillOrderDAO;
import com.bontai.ct.manager.dao.db.CtGroupDAO;
import com.bontai.ct.manager.dao.db.CtUserDAO;
import com.bontai.ct.manager.dao.db.FmOrderDAO;
import com.bontai.ct.manager.entity.mapping.AcBill;
import com.bontai.ct.manager.entity.mapping.AcBillOrder;
import com.bontai.ct.manager.entity.mapping.CtUser;
import com.bontai.ct.manager.entity.mapping.FmOrder;
import com.bontai.ct.manager.helper.timed.DatePoint;
import com.bontai.ct.manager.helper.timed.GenerateBillBiz; public class GenerateBillBizImpl implements GenerateBillBiz{ private SimpleDateFormat timeStampFormater = new SimpleDateFormat("yyyyMMddHHmmss");
private SimpleDateFormat batchNOFormat = new SimpleDateFormat("yyyyMM");
private SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
private SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); private CtUserDAO ctUserDAO;
private CtGroupDAO ctGroupDAO;
private FmOrderDAO fmOrderDAO;
private AcBillDAO acBillDAO;
private AcBillOrderDAO acBillOrderDAO;
private BillDAO billDAO; //定时任务 - 生成每月机构账单
@Transactional
public void generateBill() { System.out.println("==================== 定时任务 - 生成每月机构账单 start ======================"); //定义账单时间段(上月1日到最后一日)
Date startTime = DatePoint.getLastMonthBeginDate();
Date endTime = DatePoint.getLastMonthEndDate();
System.out.println("====================startTime:"+startTime+"======================");
System.out.println("====================endTime:"+endTime+"======================"); //获得年月信息
int year = Integer.parseInt(yearFormat.format(startTime));
int month = Integer.parseInt(monthFormat.format(startTime)); //账单批次号
String batchNo = batchNOFormat.format(startTime); //① 查询所有机构用户
List<CtUser> userList = new ArrayList<CtUser>();
CtUser user = new CtUser();
user.setUserType("1");
userList = this.ctUserDAO.queryCtUser(user); //根据userId循环插入 账单 和 账单订单关系
for(int i = 0;i<userList.size();i++){ Long userId = userList.get(i).getId();//机构用户id
Long billId = null; //① 查询用户上月的有效订单个数、账单金额、充值金额
List<AcBill> billInfo = new ArrayList<AcBill>();
Map<String,Object> params = new HashMap<String,Object>();
params.put("userId",userId);
params.put("year", year);
params.put("month", month);
billInfo= this.billDAO.queryOrderCountAndInOutMoneyByMonth(params); //② 开始插入数据 ac_bill 插入账单
if(billInfo.size()>0){ //获得统计值
int orderCount = billInfo.get(0).getOrderCount();
BigDecimal billMoney = billInfo.get(0).getBillMoney();
BigDecimal depositMoney = billInfo.get(0).getDepositMoney(); //插入
AcBill bill = new AcBill();
bill.setUserId(userId);
bill.setInvoiceStat("0");
bill.setUserType("1");
bill.setStartTime(startTime);
bill.setEndTime(endTime);
bill.setBillSeq(timeStampFormater.format(new Date())+userId);
bill.setBatchNo(batchNo);
bill.setOrderCount(orderCount);
bill.setBillMoney(billMoney);
bill.setDepositMoney(depositMoney);
bill.setBadDebtMoney(BigDecimal.ZERO);
bill.setBadDebtId(null);
bill.setCrtTime(new Date()); this.acBillDAO.insertNotNull(bill);
billId = bill.getId();
} //③ 查询某机构某月的订单
List<FmOrder> orderList = new ArrayList<FmOrder>();
orderList= this.billDAO.queryBillOrderByMonth(params); //④ 插入账单-订单关系
for(int j = 0;j<orderList.size();j++){
Long orderId = orderList.get(j).getId();
AcBillOrder bo = new AcBillOrder();
bo.setOrderId(orderId);
bo.setBillId(billId);
this.acBillOrderDAO.insertNotNull(bo);
}
}
System.out.println("==================== 定时任务 - 生成每月机构账单 end ======================");
} //getters and setters
public CtUserDAO getCtUserDAO() {
return ctUserDAO;
}
public CtGroupDAO getCtGroupDAO() {
return ctGroupDAO;
}
public FmOrderDAO getFmOrderDAO() {
return fmOrderDAO;
}
public AcBillDAO getAcBillDAO() {
return acBillDAO;
}
public AcBillOrderDAO getAcBillOrderDAO() {
return acBillOrderDAO;
}
public void setCtUserDAO(CtUserDAO ctUserDAO) {
this.ctUserDAO = ctUserDAO;
}
public void setCtGroupDAO(CtGroupDAO ctGroupDAO) {
this.ctGroupDAO = ctGroupDAO;
}
public void setFmOrderDAO(FmOrderDAO fmOrderDAO) {
this.fmOrderDAO = fmOrderDAO;
}
public void setAcBillDAO(AcBillDAO acBillDAO) {
this.acBillDAO = acBillDAO;
}
public void setAcBillOrderDAO(AcBillOrderDAO acBillOrderDAO) {
this.acBillOrderDAO = acBillOrderDAO;
}
public BillDAO getBillDAO() {
return billDAO;
}
public void setBillDAO(BillDAO billDAO) {
this.billDAO = billDAO;
}
} //DatePoint
//获得上月1号的日期
public static Date getLastMonthBeginDate(){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);
cal.set(Calendar.DAY_OF_MONTH,1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
return cal.getTime();
} //获得上个月最后一天的日期
public static Date getLastMonthEndDate(){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 0);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
} ⑤ 重要sql <!-- 查询某用户某月的完成订单数/账单金额/充值金额 -->
<select id="queryOrderCountAndInOutMoneyByMonth"
parameterType="java.util.Map"
resultType ="com.bontai.ct.manager.entity.mapping.AcBill" >
SELECT DISTINCT
ifnull(
(
SELECT count(*) FROM fm_order o
WHERE o.buyer_user_id = #{userId}
AND o.deal_stat = '09'
AND YEAR(o.final_time) = #{year}
AND MONTH(o.final_time) = #{month}
),
'0'
) as orderCount,
ifnull(
(
SELECT sum(c.pay_money) FROM fm_order o
LEFT JOIN fm_order_cost c ON o.id = c.order_id
WHERE o.buyer_user_id = #{userId}
AND o.deal_stat = '09'
AND YEAR(o.final_time) = #{year}
AND MONTH(o.final_time) = #{month}
),
'0'
) as billMoney,
ifnull(
(
SELECT sum(d.deposit_money) FROM ac_deposit d
WHERE d.user_id = #{userId}
AND d.deposit_stat = '1'
AND d.audit_stat = '2'
AND YEAR(d.deposit_time) = #{year}
AND MONTH(d.deposit_time) = #{month}
),
'0'
) as depositMoney
</select> <!-- 查询某机构某月的订单 -->
<select id="queryBillOrderByMonth"
parameterType="java.util.Map"
resultType ="com.bontai.ct.manager.entity.mapping.FmOrder" >
SELECT o.id as id
FROM fm_order o
WHERE o.buyer_user_id = #{userId}
AND o.deal_stat = '09'
AND YEAR(o.final_time) = #{year}
AND MONTH(o.final_time) = #{month}
</select>
[Spring] Java spring quartz 定时任务的更多相关文章
- Java学习---Quartz定时任务快速入门
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个, ...
- spring boot 集成 quartz 定时任务
spring boot: @EnableScheduling开启计划任务支持,@Scheduled计划任务声明 1.pom.xml 引入依赖 <dependency> <groupI ...
- spring boot整合quartz定时任务案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...
- [java,2018-02-01] quartz定时任务中时间表达式
格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 秒 是 ...
- Spring quartz定时任务service注入问题
今天想单元测试一下spring中的quartz定时任务,job类的大致结构和下面的SpringQtz1类相似,我的是实现的org.quartz.Job接口,到最后总是发现job类里注入的service ...
- Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)
在Java中,实现定时任务有多种方式,本文介绍4种,Timer和TimerTask.Spring.QuartZ.Linux Cron. 以上4种实现定时任务的方式,Timer是最简单的,不需要任何框架 ...
- Java Spring Quartz 定时任务
公司需要使用JAVA的WebServer完成简单的定时跑任务的工作.其他例如:每隔30分钟执行锁定用户解锁任务. Quartz 在开源任务调度框架中的翘首,它提供了强大任务调度机制,难能可贵的是它同时 ...
- (2)Spring集成Quartz定时任务框架介绍和Cron表达式详解
在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
原文地址:http://www.cnblogs.com/obullxl/archive/2011/07/10/spring-quartz-cron-integration.html 在JavaEE系统 ...
随机推荐
- "Only the original thread that created a view hierarchy can touch its views.” 解决方法
这个主要总是,开启的线程和 UI 线程(主线程)不是同一个线程.可以Runnable方式避免,如下例所示就可以解决这个问题了. public static void updateText(Activi ...
- .NET中那些所谓的新语法
.NET中那些所谓的新语法之四:标准查询运算符与LINQ 摘要: 开篇:在上一篇中,我们了解了预定义委托与Lambda表达式等所谓的新语法,这一篇我们继续征程,看看标准查询运算符和LINQ.标准查询运 ...
- OAF_Oracle Application Framework基本知识点(概念)
2014-02-06 Created By BaoXinjian
- WinForm中变Enter键为Tab键实现焦点转移的方法
if (e.KeyCode == Keys.Enter) { //this.SelectNextControl(this.ActiveControl,true, true, true, true); ...
- 【转】python2与python3的主要区别
摘自:http://www.cnblogs.com/codingmylife/archive/2010/06/06/1752807.html 1.性能 Py3.0运行 pystone benchmar ...
- vue组件值传递之父组件向子组件传递(props)
<template> <div class="hello"> <h1>{{ msg }}</h1> <ul> <l ...
- laravel5的Bcrypt加密方式对系统保存密码
laravel5文档介绍 //对 A 密码使用Bcrypt 加密 $password = Hash::make('mima'); //你也可直接使用 bcrypt 的 function $passwo ...
- unity, 最简单的additive shader
Shader "Custom/myAdditive" { Properties { _MainTex ("Albedo (RGB)&q ...
- 分享六:php脚本守护进程
http://www.baidufe.com/item/9565cec0004cb49d25fd.html
- swfupload详解
前提: Ajax解决了不刷新页面提交表单,但是却没有解决文件上传不刷新页面,当然也有其它技术让不刷新页面而提交文件,该技术主要是利用隐藏的iFrame,较Ajax要麻烦许多,而且其提交方式依然在底层是 ...