java 定时任务之二 Quartz(第二种方法)

本文介绍了一个使用Spring框架和Quartz实现的定时任务案例。包括如何配置和使用Quartz进行定时任务调度,以及如何在Spring中整合Quartz,实现定时执行自定义任务的功能。文章还展示了如何创建和注入依赖项,以及如何在定时任务中调用其他组件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.视频地址:

慕课讲解:

https://www.imooc.com/video/15090


2.寻找jar:

http://mvnrepository.com/search?q=quartz


3.找到包包


4.



5.


6.

package com.ssm.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Component;
@Component("myBean")
public class MyBean {
	public void printMessage(){
		//打印当前时间格式:2017-01-01 00:00:00
				Date date=new Date();
				SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				System.out.println("MyBean当前执行时间时间====="+sf.format(date));
		System.out.println("MyBean开始执行=============打印我的bean============");
	}
}


7.

package com.ssm.controller;
import org.springframework.stereotype.Component;
@Component("anotherBean")
public class AnotherBean {
	public void printAnotherMessage(){
		System.out.println("打印我的printAnotherMessage============AnotherBean");
	}
}


8.

package com.ssm.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class FirstScheduledJob  extends QuartzJobBean{
	private  AnotherBean anotherBean; 
	public void setAnotherBean(AnotherBean anotherBean){
		this.anotherBean=anotherBean;
	}
	//复写
	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		// TODO Auto-generated method stub
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("FirstScheduledJob当前执行时间时间====="+sf.format(date));
		System.out.println("FirstScheduledJob============执行");
		this.anotherBean.printAnotherMessage();
	}
}


运行即可!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值