Spring +quartz Get ApplicationContext context

Source: Internet
Author: User

Job exists in the database, you can make dynamic additions and deletions, and recently encountered how to obtain the ApplicationContext context, the solution is as follows

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: It is org.springframework.scheduling.quartz.SchedulerFactoryBean that the spring context is stored in the context of the quartz in a key/value way, The corresponding spring context can be obtained with the key defined by the Applicationcontextschedulercontextkey -  

The corresponding job task

public class Quartzjobbean implements JOB {/* (non-javadoc) * @see Org.quartz.job#execute ( Org.quartz.JobExecutionContext) */@Overridepublic 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 Blocke.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";p rivate 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 F or key \ "" + Application_context_key + "\" ");} return appctx;}}
The value of Application_context_key is the value configured in the first XML, which is passed into the context of the quartz through the Getapplicationcontext method to get the ApplicationContext context. And then get the corresponding bean.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.