我使用spring 3.1创建了石英应用程序。 我创建了一个xml文件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>
当我使用
执行此文件作为独立应用程序时new ClassPathXmlApplicationContext("Spring-Quartz.xml");
工作正常。
但是当我要在tomcat中部署这个应用程序时,我需要启动这个应用程序。
为此我创建了ServletContextListener并在上下文中使用了i Call
new ClassPathXmlApplicationContext("Spring-Quartz.xml");
它的工作正常,但在关闭我的tomcat之后,Schedular仍在工作。所以我如何关闭调度程序还是有其他方法来初始化调度程序?
答案 0 :(得分:0)
答案 1 :(得分:0)
添加到<props>
以下内容:
<prop key="org.quartz.threadPool.makeThreadsDaemons">true</prop>
这应该使石英的线程在应用程序关闭时终止。
答案 2 :(得分:0)
Quartz
Job Scheduler
是通过安排时间间隔来实现作业自动化的开源。这样就可以在触发Trigger时执行作业。触发器侦听器在特定的触发时间触发。
QuartzInitializerListener with Spring
何时在DBS触发器中更改记录以查找和发送短信
Dto类,其中包含数据库数据
public class EmpDto {
private Integer id;
private String name;
private Integer age;
private Integer salary;
private String address;
private List<EmpDto> empList;.....
设置一个工作/任务在特定日期运行&amp;时间。 component / class必须实现 JOB 接口和@override execute()。这样ececute() 引发触发器时,调度程序将执行方法代码。
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();
}
}
}
当引发触发器时,execute()方法将在该静态变量保持记录大小中调用。获取当前大小如果不同则发送电子邮件(在另一个类中写入电子邮件逻辑并通过传递大小从execute()调用该方法)。
致Generate玉米表达