日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring&Quartz集成自定义注释

發(fā)布時間:2023/12/3 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring&Quartz集成自定义注释 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
我們知道Spring支持與Quartz框架集成。 但是到目前為止,Spring僅支持靜態(tài)XML聲明方法。

如果想了解如何將Spring與Quartz集成,可以參考Spring + Quartz + JavaMail集成教程 。

作為寵物項目要求的一部分,我必須動態(tài)安排工作,并且想到了以下兩個選項:

1.使用注釋提供作業(yè)元數(shù)據(jù)
2.從數(shù)據(jù)庫加載作業(yè)元數(shù)據(jù)

現(xiàn)在,我想到了繼續(xù)使用基于注釋的方法,并且也希望將其與Spring集成。 這是我的方法。

1.創(chuàng)建一個自定義注解QuartzJob

package com.sivalabs.springsamples.jobscheduler;import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;import org.springframework.stereotype.Component;@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component @Scope("prototype") public @interface QuartzJob {String name();String group() default "DEFAULT_GROUP";String cronExp(); }

2.創(chuàng)建一個ApplicationListener來掃描所有Job實施類,并使用Quartz Scheduler調(diào)度它們。

package com.sivalabs.springsamples.jobscheduler;import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set;import org.quartz.Job; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.scheduling.quartz.CronTriggerBean; import org.springframework.scheduling.quartz.JobDetailBean;public class QuartJobSchedulingListener implements ApplicationListener<ContextRefreshedEvent> { @Autowiredprivate Scheduler scheduler;@Overridepublic void onApplicationEvent(ContextRefreshedEvent event){try {ApplicationContext applicationContext = event.getApplicationContext();List<CronTriggerBean> cronTriggerBeans = this.loadCronTriggerBeans(applicationContext);this.scheduleJobs(cronTriggerBeans);} catch (Exception e) {e.printStackTrace();}}private List<CronTriggerBean> loadCronTriggerBeans(ApplicationContext applicationContext){Map<String, Object> quartzJobBeans = applicationContext.getBeansWithAnnotation(QuartzJob.class);Set<String> beanNames = quartzJobBeans.keySet();List<CronTriggerBean> cronTriggerBeans = new ArrayList<CronTriggerBean>();for (String beanName : beanNames) {CronTriggerBean cronTriggerBean = null;Object object = quartzJobBeans.get(beanName);System.out.println(object);try {cronTriggerBean = this.buildCronTriggerBean(object);} catch (Exception e) {e.printStackTrace();}if(cronTriggerBean != null){cronTriggerBeans.add(cronTriggerBean);}}return cronTriggerBeans;}public CronTriggerBean buildCronTriggerBean(Object job) throws Exception{CronTriggerBean cronTriggerBean = null;QuartzJob quartzJobAnnotation = AnnotationUtils.findAnnotation(job.getClass(), QuartzJob.class);if(Job.class.isAssignableFrom(job.getClass())){System.out.println("It is a Quartz Job");cronTriggerBean = new CronTriggerBean();cronTriggerBean.setCronExpression(quartzJobAnnotation.cronExp()); cronTriggerBean.setName(quartzJobAnnotation.name()+"_trigger");//cronTriggerBean.setGroup(quartzJobAnnotation.group());JobDetailBean jobDetail = new JobDetailBean();jobDetail.setName(quartzJobAnnotation.name());//jobDetail.setGroup(quartzJobAnnotation.group());jobDetail.setJobClass(job.getClass());cronTriggerBean.setJobDetail(jobDetail); }else{throw new RuntimeException(job.getClass()+" doesn't implemented "+Job.class);}return cronTriggerBean;}protected void scheduleJobs(List<CronTriggerBean> cronTriggerBeans){for (CronTriggerBean cronTriggerBean : cronTriggerBeans) {JobDetail jobDetail = cronTriggerBean.getJobDetail();try {scheduler.scheduleJob(jobDetail, cronTriggerBean);} catch (SchedulerException e) {e.printStackTrace();} }} }

3.創(chuàng)建一個自定義的JobFactory,以將Spring bean用作Job實現(xiàn)對象。

package com.sivalabs.springsamples.jobscheduler;import org.quartz.Job; import org.quartz.spi.TriggerFiredBundle; import org.springframework.beans.BeanWrapper; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.PropertyAccessorFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.quartz.SpringBeanJobFactory;public class SpringQuartzJobFactory extends SpringBeanJobFactory {@Autowiredprivate ApplicationContext ctx;@Overrideprotected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {@SuppressWarnings("unchecked")Job job = ctx.getBean(bundle.getJobDetail().getJobClass());BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);MutablePropertyValues pvs = new MutablePropertyValues();pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());bw.setPropertyValues(pvs, true);return job;} }

4.創(chuàng)建Job實施類,并使用@QuartzJob對其進行批注

package com.sivalabs.springsamples.jobscheduler;import java.util.Date;import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.quartz.QuartzJobBean;@QuartzJob(name="HelloJob", cronExp="0/5 * * * * ?") public class HelloJob extends QuartzJobBean { @Overrideprotected void executeInternal(JobExecutionContext context)throws JobExecutionException{System.out.println("Hello Job is running @ "+new Date());System.out.println(this.hashCode()); } }

5.在applicationContext.xml中配置SchedulerFactoryBean和QuartJobSchedulingListener

<beans><context:annotation-config></context:annotation-config><context:component-scan base-package="com.sivalabs"></context:component-scan><bean class="com.sivalabs.springsamples.jobscheduler.QuartJobSchedulingListener"></bean><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="jobFactory"><bean class="com.sivalabs.springsamples.jobscheduler.SpringQuartzJobFactory"></bean></property></bean></beans>

6.使用測試客戶端啟動上下文

package com.sivalabs.springsamples;import org.quartz.Job; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.sivalabs.springsamples.jobscheduler.HowAreYouJob; import com.sivalabs.springsamples.jobscheduler.InvalidJob;public class TestClient {public static void main(String[] args){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");System.out.println(context); }}

參考: JCG合作伙伴 Siva在“我的技術實驗”博客上的 使用自定義批注進行Spring和Quartz集成 。

相關文章 :
  • Spring,Quartz和JavaMail集成教程
  • 在運行時交換出Spring Bean配置
  • Spring MVC3 Hibernate CRUD示例應用程序
  • 使用Spring將POJO公開為JMX MBean
  • Java教程和Android教程列表

翻譯自: https://www.javacodegeeks.com/2011/10/spring-quartz-integration-with-custom.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結

以上是生活随笔為你收集整理的Spring&Quartz集成自定义注释的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。