67,549
社区成员
发帖
与我相关
我的任务
分享
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 我的时间任务测试类
* 类描述:
* @author 何祖文
* 创建时间:2014-5-26 下午4:30:27
*
*/
@Component("myTimeTestJob")
public class MyTimeTestService {
@Scheduled(cron = "0/3 * * * * ?")
public void job() {
System.out.println("任务进行中。。。");
}
}
public static void main(String args[]) {
try {
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schedFact.getScheduler();
sched.start();
JobDetail jobDetail = new JobDetail("CronTirgger",Scheduler.DEFAULT_GROUP, CronTirgger.class);
jobDetail.getJobDataMap().put(" type ", " FULL ");
CronTrigger trigger = new CronTrigger("CronTirgger",Scheduler.DEFAULT_GROUP);
/**这里你可以写一个专门获取表达式的工具类 */
trigger.setCronExpression("0 40 8 * * ?");
sched.scheduleJob(jobDetail, trigger);
// sched.start();
} catch (Exception e) {
e.printStackTrace();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 被执行类 -->
<bean id="testQuarzt" class="cmcc.gz.pmp.timetest.service.ScheduleInfoService">
<property name="scheduler" ref="schedulerFactory" />
</bean>
<!-- 将testQuarzt注入到job中 -->
<bean id="testQuartzJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="testQuarzt" />
<property name="targetMethod" value="resetJob" />
<property name="concurrent" value="false" />
</bean>
<!-- 将job注入到定时触发器 -->
<bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="testQuartzJob" />
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
</bean>
<!-- 将触发器注入任务工程 -->
<bean id="schedulerFactory"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="testTrigger" />
</list>
</property>
</bean>
</beans>
我的任务类:
import java.text.ParseException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerBean;
import cmcc.gz.pmp.timetest.db.domain.Job;
import cmcc.gz.pmp.timetest.db.mapper.JobMapper;
/**
*
* 类描述:时间任务调度测试方法
* @author 胡汉三
* 创建时间:2014-5-26 下午6:11:55
*
*/
public class ScheduleInfoService {
@Autowired
private JobMapper mapper;
private Scheduler scheduler;
// 设值注入,通过setter方法传入被调用者的实例scheduler
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
public void resetJob() throws SchedulerException, ParseException{
Job j = mapper.findJob();
j.setFlag(1);
mapper.updateTimeTask(j);
String cronExpression = j.getJob();
System.out.println(cronExpression);
Job job = new Job();
if(j.getType()!=3){
job.setId(j.getId()+1);
}else{
job.setId(1);
}
job.setFlag(0);
mapper.updateTimeTask(job);
// 运行时可通过动态注入的scheduler得到trigger,注意采用这种注入方式在有的项目中会有问题,如果遇到注入问题,可以采 取在运行方法时候,获得bean来避免错误发生。
CronTriggerBean trigger = (CronTriggerBean)scheduler.getTrigger("testTrigger",Scheduler.DEFAULT_GROUP);
String originConExpression = trigger.getCronExpression();
// 如果相等,则表示用户并没有重新设定数据库中的任务时间,这种情况不需要重新rescheduleJob
if(!originConExpression.equalsIgnoreCase(cronExpression)){
trigger.setCronExpression(cronExpression);
scheduler.rescheduleJob("testTrigger", Scheduler.DEFAULT_GROUP, trigger);
}
/** 执行任务 **/
test();
}
/**
* 执行任务
*/
public void test(){
System.out.println("XML任务进行中...");
}
}
DROP TABLE IF EXISTS `test_time_task`;
CREATE TABLE `test_time_task` (
`status` int(11) DEFAULT NULL COMMENT '状态:0为正常,1为禁用',
`job` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT '执行时间',
`id` int(11) DEFAULT NULL COMMENT '编号'
) ENGINE=InnoDB DEFAULT CHARSET=utf-8;
-- ----------------------------
-- Records of test_time_task
-- ----------------------------
INSERT INTO `test_time_task` VALUES ('0', '0 30 9 * * ?', '1');
INSERT INTO `test_time_task` VALUES ('0', '0 30 11 * * ?', '2');
INSERT INTO `test_time_task` VALUES ('0', '0 30 16 * * ?', '3');
查询语句其中的#{hourMinute}为查询条件,格式——'12:11':
select case when (select job
from (select job,RTRIM(substr(substr(job,6,7),1,2)) as hour, LTRIM(substr(job,2,3)) as minute from test_time_task where status = 0 ) as mytable
where
str_to_date( CONCAT(hour,':',minute),'%k:%i')> str_to_date( #{hourMinute} ,'%k:%i') order by str_to_date(hour,'%k') LIMIT 1) is null then
(select job
from (select job,RTRIM(substr(substr(job,6,7),1,2)) as hour, LTRIM(substr(job,2,3)) as minute from test_time_task where status = 0 ) as mytable
order by str_to_date( CONCAT(hour,':',minute),'%k:%i') LIMIT 1) else
(select job
from (select job,RTRIM(substr(substr(job,6,7),1,2)) as hour, LTRIM(substr(job,2,3)) as minute from test_time_task where status = 0 ) as mytable
where
str_to_date( CONCAT(hour,':',minute),'%k:%i')> str_to_date( #{hourMinute} ,'%k:%i') order by str_to_date(hour,'%k') LIMIT 1) end
Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 被执行类 -->
<bean id="testQuarzt" class="cmcc.gz.pmp.timetest.service.ScheduleInfoService">
<property name="scheduler" ref="schedulerFactory" />
</bean>
<!-- 将testQuarzt注入到job中 -->
<bean id="testQuartzJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="testQuarzt" />
<property name="targetMethod" value="loadJob" />
<property name="concurrent" value="false" />
</bean>
<!-- 将job注入到定时触发器 -->
<bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="testQuartzJob" />
<property name="cronExpression">
<value>0/1 * * * * ?</value>
</property>
</bean>
<!-- 将触发器注入任务工程 -->
<bean id="schedulerFactory"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="testTrigger" />
</list>
</property>
</bean>
</beans>
java程序代码:
import java.text.ParseException;
import java.util.Date;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerBean;
import cmcc.gz.pmp.timetest.db.mapper.JobMapper;
import cmcc.gz.pmp.util.CommonDate;
/**
*
* 类描述:时间任务调度测试方法
* @author 胡汉三
* 创建时间:2014-5-26 下午6:11:55
*
*/
public class ScheduleInfoService {
@Autowired
private JobMapper mapper;
private Scheduler scheduler;
// 设值注入,通过setter方法传入被调用者的实例scheduler
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
public void loadJob() throws SchedulerException, ParseException{
Object j = mapper.findJob(CommonDate.getTimeNow(new Date()));
// 运行时可通过动态注入的scheduler得到trigger,注意采用这种注入方式在有的项目中会有问题,如果遇到注入问题,可以采 取在运行方法时候,获得bean来避免错误发生。
CronTriggerBean trigger = (CronTriggerBean)scheduler.getTrigger("testTrigger",Scheduler.DEFAULT_GROUP);
String originConExpression = trigger.getCronExpression();
// 如果相等,则表示用户并没有重新设定数据库中的任务时间,这种情况不需要重新rescheduleJob 也不需要去执行任务
if(!originConExpression.equalsIgnoreCase(j.toString())){
//如果不是系统启动时的任务,就去执行它
if(!originConExpression.equalsIgnoreCase("0/1 * * * * ?")){
/** 执行任务 **/
test();
}
System.out.println(j.toString());
trigger.setCronExpression(j.toString());
scheduler.rescheduleJob("testTrigger", Scheduler.DEFAULT_GROUP, trigger);
}
}
/**
* 执行任务
*/
public void test(){
System.out.println("XML任务进行中...");
}
}
查看:show global variables like 'wait_timeout';
修改:set global wait_timeout=86999;
OK......告一段落!!!
05-29 09:30:00 ERROR com.jolbox.bonecp.ConnectionHandle.markPossiblyBroken(ConnectionHandle.java:297): Database access problem. Killing off all remaining connections in the connection pool. SQL State = 08S01
05-29 09:30:00 ERROR org.quartz.core.JobRunShell.run(JobRunShell.java:232): Job DEFAULT.testQuartzJob threw an unhandled Exception:
org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'resetJob' on target class [class cmcc.gz.pmp.timetest.service.ScheduleInfoService$$EnhancerByCGLIB$$11b080f3] failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 70,495,036 milliseconds ago. The last packet sent successfully to the server was 1 milliseconds ago.
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:308)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:111)
at org.quartz.core.JobRunShell.run(JobRunShell.java:223)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
Caused by: org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 70,495,036 milliseconds ago. The last packet sent successfully to the server was 1 milliseconds ago.
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:240)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80)
at cmcc.gz.platform.log.aop.CmccLogMonitorAspect.doServiceAround(SourceFile:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
at cmcc.gz.pmp.timetest.service.ScheduleInfoService$$EnhancerByCGLIB$$11b080f3.resetJob(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:299)
... 3 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 70,495,036 milliseconds ago. The last packet sent successfully to the server was 1 milliseconds ago.]
at org.quartz.core.JobRunShell.run(JobRunShell.java:234)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
Caused by: org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'resetJob' on target class [class cmcc.gz.pmp.timetest.service.ScheduleInfoService$$EnhancerByCGLIB$$11b080f3] failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
at cmcc.gz.pmp.timetest.service.ScheduleInfoService$$EnhancerByCGLIB$$11b080f3.resetJob(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:299)
... 3 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 70,495,036 milliseconds ago. The last packet sent successfully to the server was 1 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3102)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2991)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3532)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618)
at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:5000)
at com.jolbox.bonecp.ConnectionHandle.setAutoCommit(ConnectionHandle.java:1063)
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:223)
... 29 more
Caused by: java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:114)
at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:161)
at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:189)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2549)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3002)
... 37 more
import java.text.ParseException;
import java.util.Date;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerBean;
import cmcc.gz.pmp.timetest.db.mapper.JobMapper;
import cmcc.gz.pmp.util.CommonDate;
/**
*
* 类描述:时间任务调度测试方法
* @author 胡汉三
* 创建时间:2014-5-26 下午6:11:55
*
*/
public class ScheduleInfoService {
@Autowired
private JobMapper mapper;
private Scheduler scheduler;
// 设值注入,通过setter方法传入被调用者的实例scheduler
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
public void resetJob() throws SchedulerException, ParseException{
Object j = mapper.findJob(CommonDate.getTimeNow(new Date()));
// 运行时可通过动态注入的scheduler得到trigger,注意采用这种注入方式在有的项目中会有问题,如果遇到注入问题,可以采 取在运行方法时候,获得bean来避免错误发生。
CronTriggerBean trigger = (CronTriggerBean)scheduler.getTrigger("testTrigger",Scheduler.DEFAULT_GROUP);
String originConExpression = trigger.getCronExpression();
// 如果相等,则表示用户并没有重新设定数据库中的任务时间,这种情况不需要重新rescheduleJob 也不需要去执行任务
if(!originConExpression.equalsIgnoreCase(j.toString())){
//如果不是系统启动时的任务,就去执行它
if(!originConExpression.equalsIgnoreCase("0/1 * * * * ?")){
/** 执行任务 **/
test();
}
System.out.println(j.toString());
trigger.setCronExpression(j.toString());
scheduler.rescheduleJob("testTrigger", Scheduler.DEFAULT_GROUP, trigger);
}
}
/**
* 执行任务
*/
public void test(){
System.out.println("XML任务进行中...");
}
}
数据库值:
0 30 9 * * ?
0 30 11 * * ?
0 30 16 * * ?
测试时,当我把我的系统时间改为九点29的时候等到30就会执行,结果跟过程都正常,
更改到十一点半正常,更改到十六点半正常,当我更改到第二天九29的时候,执行就出错了:
05-29 09:30:00 ERROR com.jolbox.bonecp.ConnectionHandle.markPossiblyBroken(ConnectionHandle.java:297): Database access problem. Killing off all remaining connections in the connection pool. SQL State = 08S01
05-29 09:30:00 ERROR org.quartz.core.JobRunShell.run(JobRunShell.java:232): Job DEFAULT.testQuartzJob threw an unhandled Exception:
org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'resetJob' on target class [class cmcc.gz.pmp.timetest.service.ScheduleInfoService$$EnhancerByCGLIB$$11b080f3] failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure