jobManager.java
package com.job; import java.util.Collection; import java.util.Date; import java.util.Random; import org.quartz.Job; import org.quartz.JobDetail; import org.quartz.JobListener; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.StdSchedulerFactory; public class JobManager { private long delayTime; private String errorCode; public static SchedulerFactory sf = null; public static Scheduler sche = null; private static SimpleTrigger simpleTrigger =null; static { sf = new StdSchedulerFactory(); try { sche = sf.getScheduler(); simpleTrigger = new SimpleTrigger(JobManager.getRandomString(10),Scheduler.DEFAULT_GROUP); } catch (SchedulerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private Job job; public JobManager(){ super(); } public JobManager(Job job, long delayTime) { super(); this.job = job; this.delayTime = delayTime; } public void start() throws Exception { long starttime = System.currentTimeMillis() + delayTime; JobDetail jobdetail = new JobDetail(job.getClass().getName() + getRandomString(4),Scheduler.DEFAULT_GROUP,job.getClass()); System.out.println("JobName:"+job.getClass().getName() + getRandomString(4)); //triggername,triggergroupname,开始时间,结束时间,重复次数;重复时间; SimpleTrigger simpleTrigger = new SimpleTrigger(job.getClass().getName() + getRandomString(4) ,Scheduler.DEFAULT_GROUP , new Date(starttime), null, 0, 0L); sche.scheduleJob(jobdetail, simpleTrigger); JobListener listener = new MyJobListener(); sche.addGlobalJobListener(listener); if (!sche.isShutdown()) { sche.start(); } } public long getDelayTime() { return delayTime; } public void setDelayTime(long delayTime) { this.delayTime = delayTime; } public String getErrorCode() { return errorCode; } public void setErrorCode(String errorCode) { this.errorCode = errorCode; } public Job getJob() { return job; } public void setJob(Job job) { this.job = job; } public static String getRandomString(int length) { String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random random = new Random(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < length; i++) { int number = random.nextInt(base.length()); sb.append(base.charAt(number)); } return (new Date().getTime()) + sb.toString(); } }JobListener.java
import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobListener; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.utils.Key; public class MyJobListener implements JobListener { public static List list=new ArrayList(); @Override public String getName() { // TODO Auto-generated method stub return this.getClass().getName(); } @Override public void jobExecutionVetoed(JobExecutionContext context) { // TODO Auto-generated method stub } @Override public void jobToBeExecuted(JobExecutionContext context) { // TODO Auto-generated method stub JobDetail detail = context.getJobDetail(); System.out.println(detail.getJobClass().getName() + " 任务马上就要执行"); } @Override public void jobWasExecuted(JobExecutionContext context, JobExecutionException exception) { String name=context.getJobDetail().getJobClass().getName(); Key key=context.getJobDetail().getKey(); String fullname =context.getJobDetail().getFullName(); System.out.println("NAME:"+name+",KEY:"+key+",FULLNAME:"+fullname+"任务执行完毕"+key.toString().substring(8)); }TestJob.java
package com.job; import java.util.Collection; import java.util.Iterator; import org.quartz.SchedulerException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestJob //implements Runnable { public TestJob(){ super(); } @SuppressWarnings("unchecked") public static void main(String args[]) throws SchedulerException, InterruptedException{ TestJob job=new TestJob(); for(int i=0;i<1000000;i++){ job.run1(); } } public void run1() { // TODO Auto-generated method stub /** * 从spring 读取,保证JobManager对象为单态 */ //JobManager jobManager= (JobManager)ContextUtil.getContext().getBean("jobManager"); // 控制台测试 ApplicationContext ctx = new ClassPathXmlApplicationContext("config/applicationContext-job.xml"); JobManager jobManager = null; jobManager= (JobManager) ctx.getBean("jobManager"); try { /*** * 任务1 */ SampeJob job = new SampeJob(); jobManager.setJob(job); jobManager.start(); /*** * 任务2 */ EasyJob job2 = new EasyJob(); jobManager.setJob(job2); jobManager.start(); } catch (Exception e) { e.printStackTrace(); } } }easyJob.java
import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; /** * @author user * */ public class EasyJob implements Job { private Object request; public Object getRequest() { return request; } public void setRequest(Object request) { this.request = request; } public void execute(JobExecutionContext context) throws JobExecutionException { // TODO Auto-generated method stub //System.out.println("do easy" + context.getJobDetail().getFullName()); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("do " + context.getJobDetail().getJobClass().getName()+ " at: " + format.format(new Date())); } }quartz.properties
org.quartz.scheduler.instanceName = DefaultQuartzScheduler org.quartz.scheduler.rmi.export = false org.quartz.scheduler.rmi.proxy = false org.quartz.scheduler.wrapJobExecutionInUserTransaction = false org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 10 org.quartz.threadPool.threadPriority = 5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true org.quartz.jobStore.misfireThreshold = 60000 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
applicationContext-job.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans default-lazy-init="true"> <bean id="jobManager" name="/jobManager" class="com.job.JobManager" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default"> <property name="delayTime"> <value>2000</value> </property> </bean> </beans>
需要的jar包