Spring +quartz获取ApplicationContext上下文

job存在数据库中,能够进行动态的增增删改查,近期遇到了怎样获取ApplicationContext上下文的问题。解决的方法例如以下

applicationContext-quartz.xml

<?xml version="1.0" encoding="UTF-8"?

> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="applicationContextSchedulerContextKey" value="applicationContextKey"/> <property name="configLocation" value="classpath:quartz.properties"/> </bean> </beans>


<!-- applicationContextSchedulerContextKey: 是org.springframework.scheduling.quartz.SchedulerFactoryBean这个类中把spring上下 文以key/value的方式存放在了quartz的上下文中了。能够用applicationContextSchedulerContextKey所定义的key得到相应的spring上下文 -->   

相应的job任务

public class QuartzJobBean implements Job {


	/* (non-Javadoc)
	 * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
	 */
	@Override
	public void execute(JobExecutionContext jobContext) throws JobExecutionException {
		
		
		String jobId = jobContext.getJobDetail().getDescription();
		String serviceId = jobContext.getTrigger().getDescription();
		

		ApplicationContext applicationContext=null;
		try {
			applicationContext=getApplicationContext(jobContext);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		WebServiceService webServiceService = (WebServiceService) applicationContext.getBean("webServiceService");
		WebServicePOJO  webService = webServiceService.getWebService(serviceId);
		ScheduleService.schedule(webService.getNameSpace(), webService.getServiceName(), webService.getWsdlURL(), webService.getMethod());
	}
	
	private static final String APPLICATION_CONTEXT_KEY = "applicationContextKey";
	private ApplicationContext getApplicationContext(JobExecutionContext context) throws Exception {
			ApplicationContext appCtx = null;
			appCtx = (ApplicationContext) context.getScheduler().getContext().get(APPLICATION_CONTEXT_KEY);
			if (appCtx == null) {
				throw new JobExecutionException("No application context available in scheduler context for key \"" + APPLICATION_CONTEXT_KEY + "\"");
			}
			return appCtx;
	}


}
APPLICATION_CONTEXT_KEY的值是第一个XML中配置的值,通过getApplicationContext方法传入quartz的context就可以获取ApplicationContext上下文,进而获取相应的bean


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Spring Quartz实现定时任务时,如果需要修改定时时间而不重启服务,可以考虑以下几种方式: 1. 使用Quartz的动态定时任务更新功能: Quartz提供了动态修改定时任务的功能,可以通过更新Cron表达式来实现修改定时时间。首先,在配置文件中设置定时任务的Cron表达式为一个变量,然后在代码中通过调用Quartz的API读取配置文件的变量,并更新定时任务的Cron表达式。通过这种方式,不需要重启服务即可修改定时时间。 2. 使用数据库表保存定时任务配置参数: 可以将定时任务的配置参数,如定时时间、执行类等保存在数据库表中。然后在代码中通过查询数据库表的方式动态获取定时任务的配置参数。当需要修改定时时间时,只需更新数据库表中的相应字段,代码会动态读取到新的定时时间并生效,而不需要重启服务。 3. 使用Spring的动态Bean注册功能: 在项目启动时,通过编程的方式动态注册定时任务的Bean。将定时任务的配置属性作为参数传入Bean,当需要修改定时时间时,通过修改Bean的相关属性值来实现。Spring会自动重新加载Bean的定义,并根据新的配置参数重新创建定时任务对象,从而实现修改定时时间而不重启服务。 综上所述,使用Quartz的动态定时任务更新功能、数据库表保存定时任务配置参数或者Spring的动态Bean注册功能,都可以实现在不重启服务的情况下修改定时时间。根据实际项目需求和技术架构选择合适的方式即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值