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
Spring +quartz获取ApplicationContext上下文的更多相关文章
- spring中获取applicationContext
常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXm ...
- 在web项目中获取ApplicationContext上下文的3种主要方式及适用情况
最近在做web项目,需要写一些工具方法,涉及到通过Java代码来获取spring中配置的bean,并对该bean进行操作的情形.而最关键的一步就是获取ApplicationContext,过程中纠结和 ...
- Spring Boot 获取ApplicationContext
package com.demo; import org.springframework.beans.BeansException; import org.springframework.contex ...
- spring中获取applicationContext(2)
前几天写web项目的时候,用到了spring mvc. 但是又写bean.我要在代码里面生成,而这个bean里面,又有一些属性是通过spring注入的. 所以,只能通过ApplicationConte ...
- spring中获取ApplicationContext对象的技巧,含源码说明
第一步,实现接口ApplicationContextAware,重写setApplicationContext方法,下方代码标红的地方,绿色部分 可以通过声明来进行存储到本类中. @Component ...
- spring项目获取ApplicationContext(能手动从Spring获取所需要的bean)
最流行的方法就是 实现ApplicationContextAware接口 @Component public class SpringContextUtil implements Applicati ...
- Hibernate数据连接不能正常释放的原因,以及在监听中获取apolicationContext上下文
Hibernate数据库连接不能正常释放: https://blog.csdn.net/u011644423/article/details/44267301 监听中获取applicationCont ...
- Spring获取ApplicationContext
在Spring+Struts+Hibernate中,有时需要使用到Spring上下文.项目启动时,会自动根据applicationContext配置文件初始化上下文,可以使用ApplicationCo ...
- spring 代码中获取ApplicationContext(@AutoWired,ApplicationListener)
2017年度全网原创IT博主评选活动投票:http://www.itbang.me/goVote/234 学习spring框架时间不长,一点一滴都得亲力亲为.今天忽然觉得老是通过@Autowir ...
随机推荐
- LA 4728 Square ,旋转卡壳法求多边形的直径
给出一些正方形.让你求这些正方形顶点之间的最大距离的平方. //返回点集直径的平方 int diameter2(vector<Point> & points) { vector&l ...
- C# GDI+技术
C# GDI+技术 GDI+概述 GDI+是GDI(即Windows早期版本号中附带的Graphics Device Interface)的后继者.它是一种构成Windows XP操作 ...
- Highstock生成股票K线图
在线演示 本地下载 使用HightStock生成股票K线图例子.
- 【OpenCV新手教程之十七】OpenCV重映射 & SURF特征点检測合辑
本系列文章由@浅墨_毛星云 出品.转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/30974513 作者:毛星云(浅墨) ...
- ArcGIS Pro体验04——菜单栏
对菜单栏进行熟悉一下: 1.地图菜单 剪切板(Clipboard):剪切(Cut).复制(Copy).粘贴(Paste),这些不用说了,在ArcMap中是放在"编辑"菜单下面的.当 ...
- Eclipse开发Android的配置(包括ADT安装,SDK配置)
1. 下载Android SDK http://code.google.com/android/download.html下载后直接解压就可以使用了. 为了在DOS控制台中直接使用SDK的工具,可 ...
- JavaScript 之 变量
一:作用域 说起变量第一个要说到的肯定就是作用域,正是因为不熟悉JS的 作用域,往往就会把面向对象的作用域张冠李戴,毕竟有些东西总是习惯性的这样,但是并不是每次照搬都是可以的,那么下一个问题就来了,j ...
- UNIX网络编程读书笔记:辅助数据
辅助数据(ancillary data)可通过调用sendmsg和recvmsg这两个函数,使用msghdr结构中的msg_control和msg_controllen这两个成员发送和接收. 辅助数据 ...
- 怎么将手动设定的IP变成固定的自动IP.
怎么将手动设定的IP变成固定的自动IP. 基本原理是 是用的MAC 地址来绑定你的IP地址 方法1左下角 开始→运行→输入 cmd 回车→输入 ipconfig /all 用来查看你的MXC地址 ...
- oracl查询锁表语句
技能源于生活的不断实践,实践是对知识的不断扩展和总结.汇总.进而形成思想体系! --查询锁表语句 select sess.sid, sess.serial#, lo.oracle_username, ...