zoukankan      html  css  js  c++  java
  • spring3.1的BeanFactory与Quartz1.8整合

    spring的applicationContext.xml配置文件:

    加入

    <bean id="myJob" class="org.springframework.scheduling.quartz.JobDetailBean">
      <property name="jobClass">
       <value>com.job.Service</value>
      </property>
     </bean> 
     <bean id="myCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
      <property name="jobDetail">
       <ref bean="myJob"/>
      </property>
      <property name="cronExpression"> 
       <value>0 0 1 * * ?</value>
      </property>
     </bean>
     <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
      <property name="jobFactory"> 
       <bean class="com.JobFactory"/> 
      </property> 
      <property name="triggers"> 
       <list> 
       <ref bean="myCronTrigger"/> 
       </list> 
      </property> 
     </bean>
    

    quartz与spring的BeanFactory整合的类: 

    /**
     * 为定时器提供自动注入功能
     */
    public class JobFactory 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);
    		applicationContext.getAutowireCapableBeanFactory().autowireBean(	jobInstance);
    		return jobInstance;
    	}
    	
    }
    

     定时任务类:

    public class MyService extends QuartzJobBean {
    
    	private static final Log log = LogFactory.getLog(MyService.class);
    	
    	@Resource
    	private UserService userService;
    	
    	@Override
    	protected void executeInternal(JobExecutionContext jobExecutionContext)
    			throws JobExecutionException {
    		log.info("执行定时任务");
    		userService.freshUserAmount();
    	}
    	
    }
    

    Quartz中时间表达式的设置-----corn表达式 

    时间格式: <!-- s m h d m w(?) y(?) -->,   分别对应: 秒>分>小时>日>月>周>年, 

  • 相关阅读:
    6大开源SIEM工具,安全信息和事件管理的“利器”
    数据库为何需要安全审计系统
    WEB漏洞扫描的开源工具
    12种开源Web安全扫描程序
    开源框架openresty+nginx 实现web应用防火墙(WAF)
    锦衣盾:开源WEB应用防火墙介绍
    20步打造最安全的Nginx Web服务器
    MySQL数据库审计系统
    数据库(分库分表)中间件对比
    Mysql调优基础、Mysql问题排查、Mysql优化、与hikari数据库连接池配合
  • 原文地址:https://www.cnblogs.com/zhaofeng555/p/3544070.html
Copyright © 2011-2022 走看看