基于maven发送邮件系列(5)----基于quartz定时的另一种方式:配置文件

这也是我在学习的时候找到的,我就是按照人家的步骤,敲了一下,感觉理解了一些,希望对大家有所帮助,只是方便以后查找

1

package com.test.anotation;

public class EBankJobBean {
	public void printAnotherMessage(){
		System.out.println("CronTriggerBean 调用的定时任务..");
	}

}

2

package com.test.anotation;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class EBankJob extends QuartzJobBean {
	
	private EBankJobBean ebankJobBean;

	public void setEbankJobBean(EBankJobBean ebankJobBean) {
		this.ebankJobBean = ebankJobBean;
	}

	@Override
	protected void executeInternal(JobExecutionContext context)
			throws JobExecutionException {
		ebankJobBean.printAnotherMessage();
		
	}

}

3.

package com.test.anotation;

public class MyJobBean {
	public void printMessage(){
		System.out.println("SimpleTriggerFactoryBean 调用定时任务");
	}

}

4.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    	http://www.springframework.org/schema/util 
    	http://www.springframework.org/schema/util/spring-util-4.0.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<context:component-scan base-package="com.test.anotation" />
	<bean id="myJobBean" class="com.test.anotation.MyJobBean"></bean>
	<bean id="eBankJobBean" class="com.test.anotation.EBankJobBean"></bean>
	<bean id="simpleJobDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="myJobBean" />
		<property name="targetMethod" value="printMessage" />
	</bean>
	<bean name="complexJobDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="eBankJobBean" />
		<property name="targetMethod" value="printAnotherMessage" />
	</bean>
	<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
		<property name="jobDetail" ref="simpleJobDetail" />
		<property name="startDelay" value="1000" />
		<property name="repeatInterval" value="3000" />
		<!-- <property name="cronExpression" value="0 07 15 ? * *" /> -->
	</bean>
	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="complexJobDetail" />
		<property name="cronExpression" value="0 08 15 ? * *" />
	</bean>
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="jobDetails">
			<list>
				<ref bean="simpleJobDetail" />
				<ref bean="complexJobDetail" />
			</list>
		</property>

		<property name="triggers">
			<list>
				<ref bean="simpleTrigger" />
				<ref bean="cronTrigger" />
			</list>
		</property>
	</bean>
</beans> 

5

package com.test.anotation;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StratUpQuartzJobs {
	
	static final Log log = LogFactory.getLog(StratUpQuartzJobs.class);
	public static void main(String[] args) {
		log.info("开始启动定时任务....");
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		log.info("定时任务启动成功....");
		
	}
	
	

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值