问题
I have created quartz application using spring 3.1. I have created one xml file Spring-Quartz.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="runMeTask" class="com.grit.quartz.RunMeTask" />
<!-- Spring Quartz -->
<bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.grit.quartz.RunMeJob" />
<property name="jobDataAsMap">
<map>
<entry key="runMeTask" value-ref="runMeTask" />
</map>
</property>
</bean>
<!-- <bean id="runMeJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="runMeTask" /> <property name="targetMethod"
value="printMe" /> </bean> -->
<!-- Simple Trigger, run every 5 seconds -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="runMeJob" />
<property name="repeatInterval" value="5000" />
<property name="startDelay" value="1000" />
</bean>
<!-- Cron Trigger, run every 5 seconds -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="runMeJob" />
<property name="cronExpression" value="0/1 * * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="runMeJob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.threadCount">15</prop>
</props>
</property>
</bean>
When i am executing this file as standalone application using
new ClassPathXmlApplicationContext("Spring-Quartz.xml");
its working fine.
But i need to start this application when i am going to deploy this application in tomcat.
For this i created ServletContextListener and in context initalized i Call
new ClassPathXmlApplicationContext("Spring-Quartz.xml");
Its working fine but after i shutdown my tomcat, Schedular still working.So how can i shutdown the schedular or is there any other way to intialize a schedular ??
回答1:
In the Destroy method , call the scheduler.shutdown() method.
回答2:
Add to <props>
the following:
<prop key="org.quartz.threadPool.makeThreadsDaemons">true</prop>
This should make quartz's threads to terminate upon application shutdown.
回答3:
Quartz Job Scheduler is Open Source for Job Automation by scheduling Time Interval. So that the job can be executed when ever the Trigger is fired. Trigger Listeners fires at a specifid fire time.
QuartzInitializerListener with Spring
When ever records get changed in DBS trigger to find and send sms
Dto class Which Holds DB data
public class EmpDto {
private Integer id;
private String name;
private Integer age;
private Integer salary;
private String address;
private List<EmpDto> empList;.....
To Scedule a job/task to run at specific date & time. component/class has to implement JOB interface and @override execute(). so that the ececute() method code will be executed by scheduler when trigger is raised.
public class Quartz_JOB implements Job{
private EmpDao edao; // configure in applicationContex.xml
public EmpDao getEdao() { return edao; }
public void setEdao(EmpDao edao) { this.edao = edao; }
public static Integer size;
public void execute(JobExecutionContext context) throws JobExecutionException {
Date time = context.getFireTime();
Date next_trigger_time =context.getNextFireTime();
System.out.println("### Current Trigget Time : "+time+"### Next Trigger Time : "+next_trigger_time);
JobKey job_key = context.getJobDetail().getKey();
JobDataMap dataMap = context.getMergedJobDataMap(); // Get all the keys from Listener.
System.out.println("Instance " + job_key + " of DumbJob says: " + jobSays + ", and val is: " + record_Size);
System.out.println("Trigger Data : "+curr_Record_Size);
System.out.println("#######Empdao : "+edao);
}
String jobSays;
int record_Size;
int curr_Record_Size;
public int getCurr_Record_Size() { return curr_Record_Size; }
public void setCurr_Record_Size(int curr_Record_Size) {
this.curr_Record_Size = curr_Record_Size;
}
}
web.xml
<listener>
<listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class>
</listener>
WebListener
@WebListener
public class QuartzListener extends QuartzInitializerListener {
Scheduler scheduler = null;
Integer size = null;
@Override
public void contextInitialized(ServletContextEvent servletContext) {
System.out.println("Context Initialized");
try {
// JobDetails used to create instances of a class which Implement <<job>>
// <<JobBuilder>> used to define/build instances of <<JobDetails>>. Which define istances of job.
JobDetail job1 = JobBuilder
.newJob(Quartz_JOB.class)
.withIdentity("Job_ID", "Group1")
.usingJobData("jobSays", "Hello World!")
.usingJobData("recird_Size", 3)
.build();
// <<Trigger>> a component that defines the scheduled 'Time Interval'. So that the given job to execute.
// << TriggerBuilder>> used to create Trigger Instance.
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("Trigger_ID", "Group1")
.forJob("Job_ID", "Group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.usingJobData("curr_Record_Size", edto.getEmpList().size())
.build();
// Setup the Job and Trigger with Scheduler & schedule jobs
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job1, trigger);
}catch (SchedulerException e) {
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent servletContext) {
System.out.println("Context Destroyed");
try {
scheduler.shutdown();
System.out.println("Quartz stoped");
}catch (SchedulerException e) {
e.printStackTrace();
}
}
}
When ever trigger is raised, execute() method will invoke in that static variable hold record size. get current size if it differs then send an email (Write email logic in another class & call that method from execute() by passing size).
To Generate Corn Expression
来源:https://stackoverflow.com/questions/17673883/spring-with-quartz