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上下文的更多相关文章

  1. spring中获取applicationContext

    常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXm ...

  2. 在web项目中获取ApplicationContext上下文的3种主要方式及适用情况

    最近在做web项目,需要写一些工具方法,涉及到通过Java代码来获取spring中配置的bean,并对该bean进行操作的情形.而最关键的一步就是获取ApplicationContext,过程中纠结和 ...

  3. Spring Boot 获取ApplicationContext

    package com.demo; import org.springframework.beans.BeansException; import org.springframework.contex ...

  4. spring中获取applicationContext(2)

    前几天写web项目的时候,用到了spring mvc. 但是又写bean.我要在代码里面生成,而这个bean里面,又有一些属性是通过spring注入的. 所以,只能通过ApplicationConte ...

  5. spring中获取ApplicationContext对象的技巧,含源码说明

    第一步,实现接口ApplicationContextAware,重写setApplicationContext方法,下方代码标红的地方,绿色部分 可以通过声明来进行存储到本类中. @Component ...

  6. spring项目获取ApplicationContext(能手动从Spring获取所需要的bean)

    最流行的方法就是  实现ApplicationContextAware接口 @Component public class SpringContextUtil implements Applicati ...

  7. Hibernate数据连接不能正常释放的原因,以及在监听中获取apolicationContext上下文

    Hibernate数据库连接不能正常释放: https://blog.csdn.net/u011644423/article/details/44267301 监听中获取applicationCont ...

  8. Spring获取ApplicationContext

    在Spring+Struts+Hibernate中,有时需要使用到Spring上下文.项目启动时,会自动根据applicationContext配置文件初始化上下文,可以使用ApplicationCo ...

  9. spring 代码中获取ApplicationContext(@AutoWired,ApplicationListener)

    2017年度全网原创IT博主评选活动投票:http://www.itbang.me/goVote/234    学习spring框架时间不长,一点一滴都得亲力亲为.今天忽然觉得老是通过@Autowir ...

随机推荐

  1. 一个执行Dos命令的窗口程序,与各位分享。

    一个执行Dos命令的窗口程序,与各位分享.   效果图:     具体实现在代码中有详细的注释,请看代码.   实现执行CMD命令的核心代码(Cmd.cs):   [csharp]   using S ...

  2. android 上传图片

    public static String uploadPicture(String url, String uploadFile) {     String resultcode = "1& ...

  3. Kettle资源库采用SQLserver数据库需要注意的点

    Kettle开源ETL工具有着自己的元数据存储方式,可以分为两种 1:File 2:DB 文件存储我这里就不多说了,下面说一下在用SQLserver2008 R2作为资源库在创建的过程中遇到的问题 K ...

  4. java web过滤器实际应用(解决中文乱码 html标签转义功能 敏感字符过滤功能)

    转载地址:http://www.cnblogs.com/xdp-gacl/p/3952405.html 在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可 ...

  5. Jmeter-Maven-Plugin高级应用:Test Results File Format-Test Results

    Test Results File Format Test Results Disabling The <testResultsTimestamp> Enabling <append ...

  6. java基础-类变量,类方法

  7. HttpContext.Current.Cache 和 HttpRuntime.Cache

    HttpRuntime.Cache:用于winfrom 和 web HttpContext.Current.Cache 用于web .NET中Cache有两种调用方式:HttpContext.Curr ...

  8. from VC的IDE使用技巧大全

    .cpp是c++源文件 .opt 工程关于开发环境的参数文件.如工具条位置等信息: .aps (AppStudio File),资源辅助文件,二进制格式,一般不用去管他. .clw ClassWiza ...

  9. 修复eclipse build-helper-maven-plugin 异常

    安装 help --> install new http://repo1.maven.org/maven2/.m2e/connectors/m2eclipse-buildhelper/0.15. ...

  10. 安装 启动sshd服务:

    .先确认是否已安装ssh服务: [root@localhost ~]# rpm -qa | grep openssh-server openssh-server-.3p1-.fc12.i686 (这行 ...