Spring 配置 quartz

本文介绍了一个基于Spring框架的计费系统续订提醒任务实现方案,包括配置定时任务、从数据库获取账户信息及通过队列发送提醒消息的具体流程。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>   
 <bean id="renewalReminderBean" class="com.playphone.billing.renewal.RenewalReminderBean"/>
    <bean id="renewalReminderJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass">
			<value>com.playphone.billing.renewal.RenewalReminderJob</value>
		</property>
		<property name="jobDataAsMap">
			<map>
				<entry key="renewalReminderBean">
					<ref bean="renewalReminderBean" />
				</entry>
			</map>
		</property>
	</bean>
	<bean id="cronRenewalReminderTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="renewalReminderJob" />
		</property>
		<property name="cronExpression">
			<value>0 0/500 * * * ?</value>
		</property>
	</bean>
<bean id="recurrentBillingScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        	<property name="triggers">
            		<list>
   				<ref local="cronRenewalReminderTrigger"/> 
            		</list>
  		</property>
  	</bean>
</beans>

 

renewalReminderBean java code:
public class RenewalReminderBean {
	/**
	 * Get A Instance Of Log
	 */
	private static  Log log = LogFactory.getLog(RenewalReminderBean.class);

	/**
	 * 1.fetch account list belong to accountGroup from database
	 * 2.put message in queue
	 */
	public void sendRenewalReminder() {
		List accountList = null;

		IAcctAccountgroupDAO accAccountgroupDao = (IAcctAccountgroupDAO) SpringBeanLoader
				.loadBean("AcctAccountgroupDAOProxy");
		try {
			accountList = accAccountgroupDao.findAll();
		} catch (Exception e) {
			log.error("fetch all accountGroup from database Exception. Caused by "
					+ e.getMessage(), e);
			return;
		}

		AcctAccountgroup accountGroup = null;
		SubsActivePlanDAO reminderDAO = null;
		List<RenewalReminder> returnUserList = null;
		for (Iterator it = accountList.iterator(); it.hasNext();) {
			accountGroup = (AcctAccountgroup) it.next();
            try {				
				reminderDAO = new SubsActivePlanDAO();
				returnUserList = reminderDAO.findRenewalReminderBy(accountGroup);
			} catch (Exception e) {
				log.error("send renewal message throw exception. Caused by "
						+ e.getMessage(), e);
			}
			sendRenewalMessage(returnUserList);
		}		
	}

	/**
	 * put message to queue.
	 * @param userList the account which receive message
	 */
	private void sendRenewalMessage(List<RenewalReminder> userList) {
		RenewalReminderProducer producer = (RenewalReminderProducer) SpringBeanLoader
				.loadBean("renewalReminderProducer");
		for (RenewalReminder user : userList) {
			producer.send(user);
		}
	}
}

 


public class RenewalReminderJob extends QuartzJobBean {
	/**
	 * Get A Instance Of Log
	 */
	private Log log = LogFactory.getLog(RenewalReminderJob.class);
	/**
	 * JavaBean of RenewalReminder
	 */
    private RenewalReminderBean renewalReminderBean;

    /**
     * set RenewalReminderBean with Spring IOC Container
     * @param renewalReminderBean javabean
     */
    public void setRenewalReminderBean(RenewalReminderBean renewalReminderBean) {
		this.renewalReminderBean = renewalReminderBean;
	}

    /**
     * override the executeInternal
     * @param arg0 JobExecutionContext
     * @throws JobExecutionException JobExecutionException
     */
	@Override
	protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
		if (log.isInfoEnabled()) {
			log.info("RenewalReminder Job is Starting....");
		}
		long startTime = System.currentTimeMillis();

		renewalReminderBean.sendRenewalReminder();

		long endTime = System.currentTimeMillis();
		if (log.isInfoEnabled()) {
			log.info("RenewalReminder Job is Completed. Total spend time : "
					+ (endTime - startTime) + "ms");
		}
	}
}


public class ScheduleJobStart {

	private static boolean started = false;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ScheduleJobStart.start();
	}

	public static void start() {
		if (!started) {
			new ClassPathXmlApplicationContext("/billingSchedulerContext.xml");
			started = true;
		}
	}

	/**
	 * @return Returns the started.
	 */
	public static boolean isStarted() {
		return started;
	}

	/**
	 * @param started The started to set.
	 */
	public static void setStarted(boolean started) {
		ScheduleJobStart.started = started;
	}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值