异步任务
package com.zhou.springboot09task.service;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class AsyncService {
public void hello(){
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("正在处理数据中");
}
}
在service层中,有一个休息3s然后在处理数据的任务
package com.zhou.springboot09task.controller;
import com.zhou.springboot09task.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//restful风格,可以返回字符串给页面
@RestController
public class AsyncController {
// 交给spring托管,返回bean对象,不用new
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello(){
asyncService.hello();
return "处理完成";
}
}
这样的效果是游览器会白屏转圈3s后才有页面,影响用户体验,所以加入springboot自带的异步任务,这样几乎可以是实时的了
使用方法:首先告诉spring这是一个异步方法
package com.zhou.springboot09task.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class AsyncService {
// 告诉spring这个是异步方法
@Async
public void hello(){
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("正在处理数据中");
}
}
然后在主程序中开启异步注解功能
package com.zhou.springboot09task;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
//开启异步注解功能
@EnableAsync
@SpringBootApplication
public class Springboot09TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TaskApplication.class, args);
}
}
邮件任务
#配置用户名密码
spring.mail.username=# 邮箱账号
spring.mail.password=#QQ邮箱的动态秘钥
#邮件服务器
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true
properties配置
package com.zhou.springboot09task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@SpringBootTest
class Springboot09TaskApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
//一个简单的邮件
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("这是一封测试邮件");
mailMessage.setText("机会是留给有准备的人的");
mailMessage.setTo("zhoudage666@gmail.com");
mailMessage.setFrom("1373983975@qq.com");
mailSender.send(mailMessage);
}
@Test
void contextLoads1() throws MessagingException {
//一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
// 组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
helper.setSubject("测试");
helper.setText("<p style='color:green'> 妙啊!!</p>",true);
// 附件
helper.addAttachment("1.jpg",new File("C:\\Users\\ZhouZhongChen\\Desktop\\孙一文.jpg"));
helper.addAttachment("dage.jpg",new File("C:\\Users\\ZhouZhongChen\\Desktop\\孙一文.jpg"));
helper.setTo("zhoudage666@gmail.com");
helper.setFrom("1373983975@qq.com");
mailSender.send(mimeMessage);
}
}
具体调用
TaskSchedule 任务调度者
TaskExecutor 任务执行者
@EnableScheduling 开启定时任务的注解
@Scheduled 定时任务的方法 里面有Cron表达式
分布式
rpc
两个核心模块:通信、序列化
定义: 远程过程调用,两台服务器A、B ,服务器A上的应用想调用服务器B上的方法或者函数,由于不在一个内存空间,不能直接调用,需要通过网络来表达调用的语义和传达调用的数据。
序列化:数据传输需要转换(网络上只能传输01二进制代码,不认识对象)