springboot之异步任务
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>springboot-task</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-task</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>
package com.example.task;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;@SpringBootApplication
@EnableAsync // 开启异步注解功能
public class SpringbootTaskApplication {public static void main(String[] args) {SpringApplication.run(SpringbootTaskApplication.class, args);}}
package com.example.task.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class AsyncController {@AutowiredAsyncService asyncService;@GetMapping("hello")public String hello(){asyncService.hello(); // 停止3sreturn "ok";}
}package com.example.task.service;import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;@Service
public class AsyncService {// 告诉spring这是一个异步的方法// 如果不是异步任务,controller会等待3s,才返回ok// 异步任务的话,controller会直接返回ok,service会正常执行@Asyncpublic void hello(){System.out.println("========数据开始处理============");try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("===========数据处理完成=========");}
}
springboot之邮件发送
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>springboot-task</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-task</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency></dependencies></project>
spring:mail:host: smtp.qq.comusername: 你的qq号password: qq邮箱的授权码,当然这些东西用网易、移动那些也可以# 开启加密验证properties.mail.smtl.ssl.enable: true
package com.example.task;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.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;@SpringBootTest
class SpringbootTaskApplicationTests {@AutowiredJavaMailSenderImpl javaMailSender;// 发送一个简单的邮件@Testvoid contextLoads() {SimpleMailMessage simpleMailMessage = new SimpleMailMessage();simpleMailMessage.setSubject("这是邮件的主题");simpleMailMessage.setText("这是邮件的内容");// 接收者simpleMailMessage.setTo("1393519068@qq.com");// 发送者simpleMailMessage.setFrom("1393519068@qq.com");javaMailSender.send(simpleMailMessage);}// 发送一个复杂的邮件@Testvoid contextLoads2() throws MessagingException {MimeMessage mimeMessage = javaMailSender.createMimeMessage();// 组装// 第二个参数为 多文件MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);mimeMessageHelper.setSubject("这是邮件的主题");// true 会解析html,否则是纯文本mimeMessageHelper.setText("<h1 style='color:red'>这是邮件的内容</h1>",true);// 附件mimeMessageHelper.addAttachment("1.txt",new File("C:\\Users\\EDY\\Desktop\\新文件 1.txt"));// 接收者mimeMessageHelper.setTo("1393519068@qq.com");// 发送者mimeMessageHelper.setFrom("1393519068@qq.com");javaMailSender.send(mimeMessage);}}
springboot之定时任务
@SpringBootApplication
// 开启使用定时任务,使用spring自带的定时任务
@EnableScheduling
public class CrawlerApplication {public static void main(String[] args) {SpringApplication.run(CrawlerApplication.class,args);}
}// 这两个不是在同一个类中,用spring定时任务的这个类需要交给spring管理// 添加定时任务配置,这里一般是用corn表达式// initialDelay,项目启动成功后,多久执行任务,单位毫秒// fixedDelay,任务执行完成后,间隔多久下一次任务执行,单位毫秒@Scheduled(initialDelay = 1000, fixedDelay = 10000)public void run(){}/**
cron:cron表达式,指定任务在特定时间执行;
fixedDelay:上一次任务执行完后多久再执行,参数类型为long,单位ms
fixedDelayString:与fixedDelay含义一样,只是参数类型变为String
fixedRate:按一定的频率执行任务,参数类型为long,单位ms
fixedRateString: 与fixedRate的含义一样,只是将参数类型变为String
initialDelay:延迟多久再第一次执行任务,参数类型为long,单位ms
initialDelayString:与initialDelay的含义一样,只是将参数类型变为String
zone:时区,默认为当前时区,一般没有用到
*/
// 使用cron表达式
package com.example.task.service;import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;@Component
public class TaskScheduling {@Scheduled(cron = "0/10 * * * * ?")public void testScheduling(){System.out.println("testScheduling");}}
部分内容转载自:
https://bilibili.com/video/BV1PE411i7CV/?p=52&spm_id_from=pageDriver&vd_source=64c73c596c59837e620fed47fa27ada7
世界是复杂的,但又不是随机的,知识也应当如此。求知的道路,意味着永恒的疲倦以及偶尔的惊喜。
可能性的艺术:比较政治学30讲
刘瑜