版权声明:(谢厂节的博客)博主文章绝大部分非原创,转载望留链接。 https://blog.csdn.net/xundh/article/details/82419484
SpringBoot可以使用XML配置或注解来定义和使用Quartz。
Quartz
Quartz是由Java编写的开源任务调试框架。
Quartz框架主要核心组件包括调度器、触发器、作业。调度器作为作业的总指挥,触发器作为作业的操作者,作业为应用的功能模块。
使用
maven
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
注解方式使用示例
package com.xundh.demo.quartz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@Configurable
@EnableScheduling
public class TestTask {
private static final Logger logger = LoggerFactory.getLogger(TestTask.class);
@Scheduled(cron="*/5 * * * * * ")
public void reportCurrentByCron(){
logger.info("time up");
}
}
运行:
定时邮件使用示例
项目结构
maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.properties
spring.mail.host=smtp.163.com
spring.mail.username=youraccount@163.com
spring.mail.password=yourpassword
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
SendMailService.java
package com.xundh.demo.mail;
public interface SendMailService {
boolean sendJunkMail();
}
SendMailServiceImpl
package com.xundh.demo.mail;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
@Service
public class SendMailServiceImpl implements SendMailService {
@Autowired
JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
public boolean sendJunkMail() {
MimeMessage mimeMessage = this.mailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
try{
message.setFrom(from);
message.setSubject("hello");
message.setTo("[email protected]");
message.setText("Hello xundh");
this.mailSender.send(mimeMessage);
return true;
}
catch(Exception e){
return false;
}
}
}
定时发送
package com.xundh.demo.quartz;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.xundh.demo.mail.SendMailService;
@Component
@Configurable
@EnableScheduling
public class TestTask {
private static final Logger logger = LoggerFactory.getLogger(TestTask.class);
@Resource
private SendMailService sendJunkMailService;
@Scheduled(cron="*/15 * * * * * ")
public void reportCurrentByCron(){
logger.info("time up");
sendJunkMailService.sendJunkMail();
}
}