示例在会睡眠的方法上加**@Async**注解,表示该方法是异步方法
package com.id0304.tesk.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据中");
}
}
controller调用service层的方法,注意要加上**@EnableAsync**开启异步任务,然后我们浏览器访问发现可以直接访问,不必再等待3000ms
package com.id0304.tesk.controller;
import com.id0304.tesk.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@EnableAsync
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@GetMapping("/hello")
public String hello() {
asyncService.hello();
return "success";
}
}
示例定义一个service层的方法,加上**@Scheduled(cron ="* * * * * MON-SAT" )**注解,cron的取值关系到调用时间,如下代码将会在周一到周六每秒会调用一次.
cron表达式可以百度在线生成器生成.
package com.id0304.tesk.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduleService {
@Scheduled(cron ="* * * * * MON-SAT" )
public void hello(){
System.out.println("hello");
}
}
不要忘记在主方法加上**@EnableScheduling**注解,开启定时任务
package com.id0304.tesk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
public class TeskApplication {
public static void main(String[] args) {
SpringApplication.run(TeskApplication.class, args);
}
}
org.springframework.boot
spring-boot-starter-mail
这里我们以qq邮箱为例:
spring:
mail:
username: [email protected]
password: XXXXX
host: smtp.qq.com
# qq邮箱要求ssl安全连接,这里需要开启ssl
properties:
mail.smtp.ssl.enable=true
这里我配置了三个属性:
我们点开上面的设置,可以看到以下界面
然后再点击账户选项卡,找到以下选项,需要先开启以下服务:
开启服务之后,会的到下面的弹窗:
而这个授权码就是密码了
继续上述步骤,我们点击下面这个链接:
点进去找到下面这段:
我们就得到了服务器地址,发送邮件服务器为smtp.qq.com
package com.id0304.tesk;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TeskApplicationTests {
@Autowired
JavaMailSender javaMailSender;
@Test
public void contextLoads() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); //定义邮件信息
simpleMailMessage.setSubject("通知"); //邮件标题
simpleMailMessage.setText("今晚要开会"); //邮件内容
simpleMailMessage.setTo("[email protected]"); //接收方邮件地址
simpleMailMessage.setFrom("[email protected]"); //发送方邮箱地址
javaMailSender.send(simpleMailMessage); //发送邮件
}
}
@Test
public void test02() throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//传入两个参数,一个复杂邮件,一个布尔类型是指定有无附件,true表示有附件
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
mimeMessageHelper.setSubject("复杂通知");
//可以使用html标签css样式,如果是html语句,要记得还要传入一个布尔类型true,不然是以普通文本发送
mimeMessageHelper.setText("今晚要开会",true);
//上传两个附件
mimeMessageHelper.addAttachment("1.jpg", new File("C:\\Users\\xianyu\\Desktop\\1.jpg"));
mimeMessageHelper.addAttachment("2.jpg", new File("C:\\Users\\xianyu\\Desktop\\2.jpg"));
//指定接收方
mimeMessageHelper.setTo("[email protected]");
//指定发送方
mimeMessageHelper.setFrom("[email protected]");
//发送邮件
javaMailSender.send(mimeMessage);
}