石英弹簧 [英] Spring with quartz
问题描述
我使用 spring 3.1 创建了石英应用程序.我创建了一个 xml 文件 Spring-Quartz.xml
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");
它工作正常.
但是当我要在 tomcat 中部署这个应用程序时,我需要启动这个应用程序.
But i need to start this application when i am going to deploy this application in tomcat.
为此我创建了 ServletContextListener 并在上下文中初始化我调用new ClassPathXmlApplicationContext("Spring-Quartz.xml");
For this i created ServletContextListener and in context initalized i Call
new ClassPathXmlApplicationContext("Spring-Quartz.xml");
它工作正常,但在我关闭我的 tomcat 后,Schedular 仍在工作.那么我如何关闭 schedular 或者有其他方法来初始化一个 schedular 吗??
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 ??
推荐答案
Quartz代码>
作业调度程序
是通过调度时间间隔实现作业自动化的开源软件.这样就可以在触发触发器时执行作业.触发器侦听器在特定触发时间触发.
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.
带有 Spring 的 QuartzInitializerListener
当 DBS 中的记录发生更改时,会触发查找和发送短信保存数据库数据的Dto类
public class EmpDto {
private Integer id;
private String name;
private Integer age;
private Integer salary;
private String address;
private List<EmpDto> empList;.....
安排作业/任务在特定日期运行时间.组件/类必须实现JOB 接口和@override execute().以便 ececute()触发触发时,方法代码将由调度程序执行.
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();
}
}
}
每当触发触发时,execute() 方法将调用该静态变量保持记录大小.如果当前大小不同,则获取当前大小然后发送电子邮件(在另一个类中编写电子邮件逻辑并通过传递大小从 execute() 调用该方法).
生成玉米表达式
这篇关于石英弹簧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!