In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to achieve instant Quartz Job injection Bean in java". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve instant Quartz Job injection Bean in java".
Nowadays, Spring has become the de facto framework of enterprise application development. Because real-time Quartz Job is not Spring Bean in general, it is necessary to find a way to obtain Bean in Job.
Real-time Job is different from periodic / timed Job. It is a Quartz Job that is submitted at any time according to business needs. It is often used in time-consuming tasks (such as import / export) in the background of interface submission.
Next, let's take a brief look at several ways to obtain Spring Bean:
The first: define the AppCtxUtils utility class, which is used to get Bean.
@ Componentpublic class AppCtxUtils implements ApplicationContextAware {private static ApplicationContext appCtx; @ Override public void setApplicationContext (ApplicationContext appCtx) throws BeansException {this.appCtx = appCtx;} public static ApplicationContext getAppCtx () {return appCtx;} public static T getBean (Class clazz) {return appCtx.getBean (clazz);} public static T getBean (String beanName) {return (T) appCtx.getBean (beanName);} public class AppCtxUtilsExampleJob implements Job {private ExampleService exampleService = AppCtxUtils.getBean (ExampleService.class) / / omit. @ Override public void execute (JobExecutionContext context) throws JobExecutionException {/ / omitted here.}}
Comments: this is one of the most widely used methods.
The second is to pass ApplicationContext or Bean through the JobDataMap parameter.
Public class JobDataMapExampleJob implements Job {/ / define KEY public static final String KEY_APP_CTX = "appCtx"; / / omit here. @ Override public void execute (JobExecutionContext context) throws JobExecutionException {/ / take ApplicationContext ApplicationContext appCtx = (ApplicationContext) getJobDataMap (context) .get (KEY_APP_CTX) from JobDataMap; / / fetch various Bean ExampleService exampleService = appCtx.getBean (ExampleService.class) through ApplicationContext / / omit here.} private JobDataMap getJobDataMap (JobExecutionContext context) {return context.getJobDetail (). GetJobDataMap ();}}
Third: JobDetail is configured as prototype Bean.
Define Job and use annotations to inject Bean.
/ / defined as Bean@Component ("prototypeJobDetailExampleJob") public class PrototypeJobDetailExampleJob implements Job {/ / using annotation injection @ Autowired private ExampleService exampleService; @ Override public void execute (JobExecutionContext context) throws JobExecutionException {/ / omitted here.}
Configure prototype JobDetail Bean.
Dispatch immediately
/ / get JobDetail from Spring ApplicationContext. JobDetail jobDetail = AppCtxUtils.getBean ("prototypeJobDetailExampleJob.jobDetail"); / / the code scheduler.addJob (jobDetail, false) is omitted here; String myTrigger = UUID.randomUUID (). ToString (); Trigger trigger = TriggerBuilder.newTrigger () / / .withidentity (myTrigger, "groupName") / / .forJob (jobDetail) / / .startNow () / / .build (); scheduler.scheduleJob (trigger)
Comments: PrototypeJobDetailExampleJob is written in Spring Bean way is very simple, but the configuration and use is more complex.
Fourth: a way for Spring to provide support for Quartz.
Define Job
Public class SchedulerContextExampleJob implements Job {/ / define KEY public static final String KEY_APP_CTX = "appCtx"; @ Override public void execute (JobExecutionContext context) throws JobExecutionException {try {ApplicationContext appCtx = getApplicationContext (context); / / fetch various Bean ExampleService exampleService = appCtx.getBean (ExampleService.class) via ApplicationContext; / / the code here is omitted} catch (Exception e) {throw new JobExecutionException (e) }} private ApplicationContext getApplicationContext (JobExecutionContext context) throws SchedulerException {/ / take out ApplicationContext return (ApplicationContext) context.getScheduler (). GetContext (). Get (KEY_APP_CTX);}}
Configure JobDetail and Scheduler
The above four writing methods are either lengthy or complex in configuration. Elegant enough for programmers with obsessive-compulsive disorder, let's introduce a more elegant approach.
Elegant public class ExampleJob implements Job {@ Autowired private ExampleService exampleService; @ Override public void execute (JobExecutionContext context) throws JobExecutionException {/ / omitted here.}}
The biggest difference between ExampleJob and the third PrototypeJobDetailExampleJob is that ExampleJob is not Spring Bean, but you can still use Spring annotations to inject Bean.
Then how to achieve it?
Basic principle: instantiate the JOB object instance of Quartz and ask the Spring container to help inject it before using it.
Don't talk much nonsense and go straight to the code.
Extend SpringBeanJobFactory.
Public class SpringAutowireCapableJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {private ApplicationContext applicationContext; @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;} @ Override protected Object createJobInstance (TriggerFiredBundle bundle) throws Exception {Object jobInstance = super.createJobInstance (bundle); / / the object applicationContext.getAutowireCapableBeanFactory (). AutowireBean (jobInstance); return jobInstance;} is processed with Spring here.
Specify jobFactory as SpringAutowireCapableJobFactory when configuring scheduler.
Thank you for your reading, the above is the content of "how to achieve instant Quartz Job injection Bean in java". After the study of this article, I believe you have a deeper understanding of how to achieve instant Quartz Job injection Bean in java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.