Quartz 定时器,同时运用多个定时器

时间:2022-05-18 03:33:35

效果:每天执行两个定时器,两个定时器不相关联。jar版本Quartz 2.2.3

Java工程结构图  jar 包下载: 链接: https://pan.baidu.com/s/1-7dh620k9PnwAbfKW7Mdiw 提取码: i25t 

Quartz 定时器,同时运用多个定时器

jingPhone.java

 

public class jingPhone {

private String phoneid;
private String biddersPrice;
private String yuqiprice;
private String second_kill;
public jingPhone(){}
public jingPhone(String phoneid,String biddersPrice,String yuqiprice,String second_kill){
this.phoneid = phoneid;
this.biddersPrice = biddersPrice;
this.yuqiprice = yuqiprice;
this.second_kill = second_kill;
}
public String getPhoneid() {
return phoneid;
}
public void setPhoneid(String phoneid) {
this.phoneid = phoneid;
}
public String getBiddersPrice() {
return biddersPrice;
}
public void setBiddersPrice(String biddersPrice) {
this.biddersPrice = biddersPrice;
}
public String getYuqiprice() {
return yuqiprice;
}
public void setYuqiprice(String yuqiprice) {
this.yuqiprice = yuqiprice;
}
public String getSecond_kill() {
return second_kill;
}
public void setSecond_kill(String second_kill) {
this.second_kill = second_kill;
}
}

 

 

Contants.java

 

public class Contants {
public static final String driver="com.mysql.jdbc.Driver";//加载JDBC驱动
public static final String url="jdbc:mysql://192.168.1.3:3306/suyou";//mysql的url
public static final String username="root";//用户名
public static final String password="123456";//密码
}

 

DBConn.java

 


import java.sql.*;

public class DBConn {
private static Connection conn;
private static PreparedStatement ps;
private static ResultSet rs;
/**
* 连接数据库
* @return
*/
public static Connection getConnection() {
try {
Class.forName(Contants.driver); //加载mysql驱动
System.out.println(Contants.driver + "\n加载成功!");
} catch (ClassNotFoundException e) {
System.out.println(Contants.driver + "加载失败(╯﹏╰)b");
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(Contants.url, Contants.username, Contants.password); //连接数据库
System.out.println(Contants.url + "\n连接成功!");
} catch (SQLException e) {
System.out.println(Contants.url + "连接失败(╯﹏╰)b");
e.printStackTrace();
}
return conn;
}

/**
* 关闭数据库连接
* @throws SQLException
*/
public static void closeConnection() {
if(ps != null) {
try {
System.out.println("关闭ps");
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} //关闭数据库
}
if(rs != null) {
try {
System.out.println("关闭rs");
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
System.out.println("关闭conn");
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

 

DBUnit.java

 


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.suyou.bean.jingPhone;

public class DBUnit {
/**
* 竞拍到竞拍完成系列
*/
//得到竞拍成功的商品
private static final String getjingpaibooleanUser = "select phone_id as phoneid,bidders_price as biddersPrice,second_yuqi_price as yuqiprice,second_kill from phone_show where second_boolean = '1' and cunzai = '1' and bidders_price>=second_yuqi_price ";
//修改竞拍人员里面的,竞拍成功
private static final String updatejingpaiUsersuccess = "update phone_jing_pai set jboolean = '1' , agreetime = NOW() where jboolean = '0' and phone_id = ? and price = ? ";
//修改竞拍人员里面的,竞拍失败
private static final String updatejingpaiUserfail = "update phone_jing_pai set jboolean = '2' , agreetime = NOW() where jboolean = '0' ";
//得到竞拍商品1修改为商品展示竞拍结束的商品,未成功下架
private static final String updatejingboolean = "update phone_show set cunzai='3' where second_boolean = '1' and cunzai = '1' ";
/**
* 竞拍完成到竞拍系列
*/
//竞拍完成后展示商品结束
private static final String updatejingbooleanthree = "update phone_show set cunzai='0' where second_boolean = '1' and cunzai = '3' ";
/**
* 得到竞拍成功的商品
* @return
*/
public static List<jingPhone> getjingUser(Connection conn){
PreparedStatement ps = null;
ResultSet rs = null;
List<jingPhone> stu = new ArrayList<jingPhone>();
try{
ps = conn.prepareStatement(getjingpaibooleanUser);
rs = ps.executeQuery();
while(rs.next()) {
String phoneid = rs.getString("phoneid");
String biddersPrice = rs.getString("biddersPrice");
String yuqiprice = rs.getString("yuqiprice");
String second_kill = rs.getString("second_kill");
jingPhone jp =new jingPhone(phoneid,biddersPrice,yuqiprice,second_kill);
stu.add(jp);
}
}catch(Exception e){e.printStackTrace();}finally{
if(rs != null) {
try {
System.out.println("关闭rs");
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null) {
try {
System.out.println("关闭ps");
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return stu;
}
/**
* 修改信息,竞拍成功
*/
public static void updatejingUsersuccess(jingPhone jp,Connection conn) throws SQLException {
PreparedStatement ps = null;
try{
ps = conn.prepareStatement(updatejingpaiUsersuccess);
ps.setString(1, jp.getPhoneid());
ps.setString(2, jp.getBiddersPrice());
int count = ps.executeUpdate();
System.out.println(isSuccess(count));
}catch(Exception e) {e.printStackTrace();}finally{
if(ps != null) {
try {
System.out.println("关闭ps");
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 修改信息,竞拍失败
*/
public static void updatejingUserfail(Connection conn) throws SQLException {
PreparedStatement ps = null;
try{
ps = conn.prepareStatement(updatejingpaiUserfail);
int count = ps.executeUpdate();
System.out.println(isSuccess(count));
}catch(Exception e) {e.printStackTrace();}finally{
if(ps != null) {
try {
System.out.println("关闭ps");
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 修改商品cunzai 属性 竞拍商品都设置为3(表示待展示,商品竞拍完成)
* @throws SQLException
*/
public static void updatejingboolean(Connection conn) throws SQLException{
PreparedStatement ps = null;
try{
ps = conn.prepareStatement(updatejingboolean);
int count = ps.executeUpdate();
System.out.println(isSuccess(count));
}catch(Exception e) {e.printStackTrace();}finally{
if(ps != null) {
try {
System.out.println("关闭ps");
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 修改商品cunzai 属性 竞拍商品都设置为0(表示竞拍成功后,商品展示完成,进入下一轮竞拍)
* @param conn
*/
public static void updatejingbooleanthree(Connection conn){
PreparedStatement ps = null;
try{
ps = conn.prepareStatement(updatejingbooleanthree);
int count = ps.executeUpdate();
System.out.println(isSuccess(count));
}catch(Exception e) {e.printStackTrace();}finally{
if(ps != null) {
try {
System.out.println("关闭ps");
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 判断操作是否成功
* @param count
* @return
*/
public static String isSuccess(int count) {
if(count > 0) {
return "修改操作成功!";
}else {
return "操作失败";
}
}
}

 

QuartzManager.java  //这个我是借用别人的,增删改方法都有在这里:  https://blog.csdn.net/xlxxcc/article/details/52115995

 


import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
* 来源: https://blog.csdn.net/xlxxcc/article/details/52115995
* @author
*
*/
public class QuartzManager {

private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();

/**
* @Description: 添加一个定时任务
* @param jobName 任务名
* @param jobGroupName 任务组名
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param jobClass 任务
* @param cron 时间设置,参考quartz说明文档
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void addJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName, Class jobClass, String cron) {
try {
Scheduler sched = schedulerFactory.getScheduler();
// 任务名,任务组,任务执行类
JobDetail jobDetail= JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
// 触发器
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
// 触发器名,触发器组
triggerBuilder.withIdentity(triggerName, triggerGroupName);
triggerBuilder.startNow();
// 触发器时间设定
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
// 创建Trigger对象
CronTrigger trigger = (CronTrigger) triggerBuilder.build();
// 调度容器设置JobDetail和Trigger
sched.scheduleJob(jobDetail, trigger);
// 启动
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

 

PhoneAM.java

 


import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.suyou.mysql.DBConn;
import com.suyou.mysql.DBUnit;
public class PhoneAM implements Job{//必须继承Job
//全局变量时间
private static Date day = null;
//全局变量时间转化之后的字符串格式
private static String dateNowStr = null;

/**
* 上午执行的函数
*/
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
/**
* 执行任务开始
*/
//获取当前开始时间
day = new Date();
//转换格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//进行转换
dateNowStr = sdf.format(day);
//打印到控制台
System.out.println("当前开始时间:"+dateNowStr);
//初始化数据库链接
Connection conn = null;
//得到数据库链接,在执行SQL语句之后不关闭,执行完了才关闭
conn = DBConn.getConnection();
//SQL语句操作
DBUnit.updatejingbooleanthree(conn);
//关闭数据库所有链接,这里只关闭conn 链接,其他的已经在执行sql语句之后就关闭相应的链接了
DBConn.closeConnection();
//获取当前结束时间
day = new Date();
//转换格式
dateNowStr = sdf.format(day);
//打印到控制台
System.out.println("当前结束时间:"+dateNowStr);
/**
* 执行任务结束
*/
}
}

 

PhonePM.java

 


import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.suyou.bean.jingPhone;
import com.suyou.mysql.DBConn;
import com.suyou.mysql.DBUnit;

public class PhonePM implements Job{

private static Date day = null;
private static String dateNowStr = null;
/**
* 下午执行的函数
*/
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
/**
* 执行任务开始
*/
//获取当前开始时间
day = new Date();
//转换格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//进行转换
dateNowStr = sdf.format(day);
//打印到控制台
System.out.println("当前开始时间:"+dateNowStr);
//初始化数据库链接
Connection conn = null;
//得到数据库链接,在执行SQL语句之后不关闭,执行完了才关闭
conn = DBConn.getConnection();
//获取查询出来的对象集合
List<jingPhone> list = DBUnit.getjingUser(conn);
try {
for( int i = 0 ; i < list.size() ; i++) {//内部不锁定,效率最高,但在多线程要考虑并发操作的问题。
//对每一个对象集合执行相应的操作
DBUnit.updatejingUsersuccess(list.get(i),conn);
}
//SQL语句操作
DBUnit.updatejingUserfail(conn);
//SQL语句操作
DBUnit.updatejingboolean(conn);
//关闭数据库所有链接,这里只关闭conn 链接,其他的已经在执行sql语句之后就关闭相应的链接了
DBConn.closeConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//获取当前结束时间
day = new Date();
//转换格式
dateNowStr = sdf.format(day);
//打印到控制台
System.out.println("当前结束时间:"+dateNowStr);
/**
* 执行任务结束
*/
}
}

 

Console.java 

 


import com.suyou.quartz.QuartzManager;
public class Console {


static int count = 0;

//任务名 不能一样
public static String JOB_NAME_PM = "18点55分30秒启动";
public static String JOB_NAME_AM = "6点55分30秒启动";
//任务组名 可以一样
public static String JOB_GROUP_NAME = "竞拍phone";
//触发器名 不能一样
public static String JOB_GROUP_NAME_PM = "pmStartPhone";
//触发器组名 可以一样
public static String JOB_GROUP_NAME_AM = "amStartPhone";
public static String TRIGGER_GROUP_NAME = "StartPhone";

public static void main(String[] args) {
/**
* 每天定时任务不用关
*/
try {
/**
* 下午执行的定时器任务,每天下午6点55分30秒执行函数一次
*/
QuartzManager.addJob(JOB_NAME_PM, JOB_GROUP_NAME, JOB_GROUP_NAME_PM, TRIGGER_GROUP_NAME, com.suyou.TestPhone.PhonePM.class, "30 55 18 * * ?");
/**
* 上午执行的定时器任务,每天上午7点00分30秒执行函数一次
*/
QuartzManager.addJob(JOB_NAME_AM, JOB_GROUP_NAME, JOB_GROUP_NAME_AM, TRIGGER_GROUP_NAME, com.suyou.TestPhone.PhoneAM.class, "30 00 7 * * ?");

} catch (Exception e) {
e.printStackTrace();
}
}
}

改好了控制台打印的也没了,代码复制过去SQL语句改成自己的,链接数据库也改成自己的就好了,如果只是测试的话可以不用链接数据库,我只是用来定时修改一个表的属性的。

如果两个定时器之间的时间相互间隔相同可以不用Quartz