@Named public class SampleJob implements Job { @Inject private AService aService; @Override public void execute(JobExecutionContext context) throws JobExecutionException { //Do injection with spring SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); aService.doIt(); } }
http://stackoverflow.com/questions/4258313/how-to-use-autowired-in-a-quartz-job
ApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext( ContextLoaderListener.getCurrentWebApplicationContext().getServletContext() ); Bean bean = (Bean) springContext.getBean("beanName"); bean.method();
package com.yingcao.springdatajpa.quartz; import java.util.List; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.Trigger; import org.quartz.TriggerKey; import org.quartz.impl.StdSchedulerFactory; public class ScheduleStartListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent sce) { } public void contextInitialized(ServletContextEvent sce) { try { recovery(); } catch (Exception e) { e.printStackTrace(); } } public void recovery() { Scheduler scheduler = null; try { SchedulerFactory schedulerFactory = new StdSchedulerFactory(); scheduler = schedulerFactory.getScheduler();// 可以通过SchedulerFactory创建一个Scheduler实例 List<String> triggerGroups = scheduler.getTriggerGroupNames();// 获取调度器中所有的触发器组 System.out.println("调度器中所有的触发器组 size():" + triggerGroups.size()); if (triggerGroups != null && triggerGroups.size() != 0)// 重新恢复在triggerGroups组中所有的触发器 { for(int i = 0; i <triggerGroups.size();i++) { TriggerKey triggerKey = TriggerKey.triggerKey( triggerGroups.get(i), triggerGroups.get(i)); System.out.println("triggerKey:" + triggerKey); Trigger tg = scheduler.getTrigger(triggerKey);// 获取trigger System.out.println(triggerKey + " -> 执行时间 :" + tg.getNextFireTime()); scheduler.rescheduleJob(triggerKey, tg);// 按新的trigger重新设置job执行 } } scheduler.start(); } } catch (Exception e) { e.printStackTrace(); } } }
package com.yingcao.springdatajpa.quartz;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzTest {
public void run(String date, int id) throws Exception {
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();// 可以通过SchedulerFactory创建一个Scheduler实例
// 设置工作详情
JobDetail job = newJob(JobTest.class).withIdentity("job_" + id,
"test" + id) // (String name, String
// group)把触发器在集群节点实例命名的组只是为了区分(伐木)从什么地方定问调度重新执行此作业,如果它是正在进行时调度下去...
// .requestRecovery()
.build();
job.getJobDataMap().put("testId", id); // 设置存储参数(不需要可删除)
SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity(
"overdue" + id, "overdue" + id)// withIdentity("trigger",
// "group") .startAt(startDate)
.build();
scheduler.scheduleJob(job, trigger);
scheduler.start();
System.out.println("------- Start Scheduler ----------------");
}
public static void main(String[] args) throws Exception {
QuartzTest quartzOverdue = new QuartzTest();
quartzOverdue.run("2014-07-02 00:30:00", 666);// 666,随便的吉祥数字
}
}
http://blog.csdn.net/Uhzgnaw/article/details/46358333
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.class = org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.useProperties = false org.quartz.jobStore.tablePrefix = QRTZ_ org.quartz.jobStore.isClustered = false # Configure Datasources #============================================================================ org.quartz.jobStore.dataSource = myDS org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3306/quartz1x?useUnicode=true&characterEncoding=utf8 org.quartz.dataSource.myDS.user = root org.quartz.dataSource.myDS.password =111111 org.quartz.dataSource.myDS.maxConnections = 10
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false"> <property name="configLocation" value="classpath:quartz.properties" /> </bean>
<bean id="logout" class="org.apache.shiro.web.filter.authc.LogoutFilter"> <property name="redirectUrl" value="/loginform" /> </bean>
/logout = logout