JAVA Quartz任务调度(超实用)基础使用

首先编写你的任务调度内容..建议另外建一个调度内容的工具包

跟住就写具体内容

@Transactional(rollbackFor=RuntimeException.class)
public class JobDetail{
    
	
	@Autowired
	private TBaseMemberService TBaseMemberService;

	/**
	 * 任务调度每天检查数据库的中文有否转换拼音
	 * @throws JobExecutionException
	 */
	public void executeJob() throws Exception {
		try {  
			//查出所有LoginName为空的User
			List<TBaseMember> tBaseMemberList = TBaseMemberService.findAllByNullLoginName();
			//把中文转换成拼音
			TBaseMemberService.changePinYin(tBaseMemberList);
			System.out.println("保存成功");
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
		
		System.out.println("hihihihi");
	}
}

跟住就即刻写配置文件  名称: applicationContext-job.xml

<?xml version="1.0" encoding="UTF-8"?>   
  <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:aop="http://www.springframework.org/schema/aop"      
       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:context="http://www.springframework.org/schema/context"   
        xmlns:tx="http://www.springframework.org/schema/tx"   
       xmlns:task="http://www.springframework.org/schema/task"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd   
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task-3.0.xsd"
       default-autowire="byName" >
	    <!-- allen 任务调度配置文件 -->
		<!-- 1定义执行任务类 -->
		<bean id="scheduleWorker" class="com.zc.jc.task.job.JobDetail"/>
		
		<!-- 2创建任务工厂 -->
		<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
			<!-- 注入任务类 -->
			<property name="targetObject" ref="scheduleWorker"></property>
			<!-- 注入任务类的方法,该方法就是定时执行方法体 -->
			<property name="targetMethod" value="executeJob"></property>
		</bean>
		
		<!-- 3.创建任务触发器的工厂,注入任务工厂+ 任务执行表达式 -->
		<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
			<!-- 注入 -->
			<property name="jobDetail" ref="jobDetail"/>
			<!-- 指定任务执行表达式,按照指定的周期执行任务 (秒  分  小时   日  月  周  )-->
			<property name="cronExpression" value="0/5 * * * * ?"></property>
			<!-- 0/5 * * * * ? -->
		</bean>
		
		<!-- 4.创建任务执行计划 -->
		<bean  lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
			<!-- 注入任务执行触发器 -->
			<property name="triggers">	
				<list>
					<!-- 引用触发器对象 -->
					<ref bean="cronTrigger"/>
				</list>
			</property>
		</bean>
		 
</beans>

最后!! 肯撚定每个人都为时间表达式烦恼, 这里就供给各位一个非常好的时间转换网站:

http://cron.qqe2.com/

这里某些时间点的表达式可能有误,要先测试再使用哦~ 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值