quartz之servlet启动方式

1.复制org.quartz.quartz.properties或新建quartz_priority.properties到src目录下;

org.quartz.scheduler.instanceName = PushDBScheduler
org.quartz.scheduler.instanceId = one
orgorg.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 4
org.quartz.threadPool.threadPriority = 4
orgorg.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileName = quartz_job.xml

 

2.新建quarzt_config.xml到src目录下;

 

<quartz>
	<job>
		<job-detail>
			<name>ScanItemsInDB</name>
			<group>Scanning</group>
			<job-class>com.testquartz.ScanDB</job-class>
			<job-data-map allows-transient-data="true">
				<entry>
					<key>testmode</key>
					<value>true</value>
				</entry>
			</job-data-map>
		</job-detail>
		<trigger>
			<cron>
				<name>t1</name>
				<group> Scanning </group>
				<job-name> ScanItemsInDB </job-name>
				<job-group> Scanning </job-group>
				<cron-expression>0 0/5 * * * ?</cron-expression>
			</cron>
		</trigger>
	</job>
</quartz>

 

3.新建log4j.xml文件到src目录下;

4.引入quartz的依赖包:

  • log4j-1.2.16.jar
  • quartz-2.1.7.jar
  • slf4j-api-1.6.1.jar
  • slf4j-log4j12-1.6.1.jar

5.新建Servlet:QuartzInit;

web.xml中配置:

 

<servlet>
   <servlet-name>QuartzInit</servlet-name>
   <servlet-class>com.xxx.util.QuartzInit</servlet-class>
   <load-on-startup>1</load-on-startup>
 </servlet>

6.servlet代码:

 

import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.examples.example1.HelloJob;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QuartzInit extends HttpServlet {
	private static Logger log = LoggerFactory.getLogger(QuartzInit.class);

	@Override
	public void init(ServletConfig cfg) throws ServletException {
		super.init();
		initScheduler(cfg);
	}
	
	protected void initScheduler(ServletConfig cfg) {
		try {
			SchedulerFactory sf = new StdSchedulerFactory();
			Scheduler sched = sf.getScheduler();
			Date runTime = evenMinuteDate(new Date());
			JobDetail job = newJob(HelloJob.class).withIdentity("job1",
					"group1").build();
			Trigger trigger = newTrigger().withIdentity("trigger1", "group1")
					.startAt(runTime).build();
			sched.scheduleJob(job, trigger);
			sched.start();
			try {
				Thread.sleep(65L * 1000L);
			} catch (Exception e) {
			}
			log.info("------- Shutting Down ---------------------");
			sched.shutdown(true);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

}

 7.定义我们的job类:HelloJob

 

package org.quartz.examples.example1;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class HelloJob implements Job {
	private static Logger _log = LoggerFactory.getLogger(HelloJob.class);

	public HelloJob() {
	}

	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		_log.info("Hello World! - " + new Date());
	}

}

 

8.部署项目到服务器,启动服务器,后台就可以看到调度的执行情况。

你可能感兴趣的:(servlet)