手机版
你好,游客 登录 注册
背景:
阅读新闻

在Spring 下配置Quartz集群

[日期:2012-06-20] 来源:Linux社区  作者:ismezy [字体:大 中 小]

由于项目将要上线,而项目中的Quartz集群配置一直没时间弄,本以为应该是一件挺简单的事,没想到Gooble了一两天才解决问题,主要是因为Spring到3.0.7这个版本为止都没有原生支持Quartz的集群,原来的Quartz配置在单实例下运行正常,配置集群之后各种异常都来了,今天终于配置成功,在这里做一个笔记,避免以后走弯路。

这里要先感谢一下 http://www.linuxidc.com/Linux/2012-06/63408.htm 的博主,在参考了此博客后才得以成功。

首先是任务Bean,可以是一个普通的Service或者Bean

  1. @Service("helloJob")   
  2. public HelloServiceImpl implements HelloService{   
  3.       @Autowired  
  4.       private HelloDao dao;   
  5.       public void executeJob(){   
  6.            List list = dao.findJob();   
  7.            // TODO:这里实现自己的功能   
  8.       }   
  9. }  

第二步创建一个任务代理类DetailQuartzJobBean

  1. public class DetailQuartzJobBean extends QuartzJobBean implements ApplicationContextAware {   
  2.     protected final Log logger = LogFactory.getLog(getClass());   
  3.   
  4.     private String targetObject;   
  5.     private String targetMethod;   
  6.     private ApplicationContext ctx;   
  7.   
  8.     protected void executeInternal(JobExecutionContext context)   
  9.           throws JobExecutionException {   
  10.        try {   
  11.            logger.info("execute [" + targetObject + "] at once>>>>>>");   
  12.            Object otargetObject = ctx.getBean(targetObject);   
  13.            Method m = null;   
  14.            try {   
  15.                m = otargetObject.getClass().getMethod(targetMethod,new Class[] {});   
  16.                m.invoke(otargetObject, new Object[] {});   
  17.            } catch (SecurityException e) {   
  18.                 logger.error(e);   
  19.            } catch (NoSuchMethodException e) {   
  20.                 logger.error(e);   
  21.            }   
  22.        } catch (Exception e) {   
  23.            throw new JobExecutionException(e);   
  24.        }   
  25.     }   
  26.   
  27.     public void setApplicationContext(ApplicationContext applicationContext){   
  28.         this.ctx=applicationContext;   
  29.     }   
  30.   
  31.     public void setTargetObject(String targetObject) {   
  32.         this.targetObject = targetObject;   
  33.     }   
  34.   
  35.     public void setTargetMethod(String targetMethod) {   
  36.         this.targetMethod = targetMethod;   
  37.     }   
  38. }  

这个类是调用Job service的关键

linux
相关资讯       集群  Quartz  Spring配置 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款