苏州地区,哪些视频评测网站提供优秀的内容?如何优化这些网站的以吸引用户?
摘要:做评测好的视频网站有哪些,苏州关键词优化怎样,长春模板自助建站,网站制作多少钱异步任务 创建一个Hello项目创建一个类AsyncService 异步处理还是非常常用的,比如我们在网站上发送邮件&a
做评测好的视频网站有哪些,苏州关键词优化怎样,长春模板自助建站,网站制作多少钱异步任务 创建一个Hello项目 创建一个类AsyncService 异步处理还是非常常用的#xff0c;比如我们在网站上发送邮件#xff0c;后台会去发送邮件#xff0c;此时前台会造成响应不动#xff0c;直到邮件发送完毕#xff0c;响应才会成功#xff0c;所以我们一般会采用多线…异步任务 创建一个Hello项目 创建一个类AsyncService 异步处理还是非常常用的比如我们在网站上发送邮件后台会去发送邮件此时前台会造成响应不动直到邮件发送完毕响应才会成功所以我们一般会采用多线程的方式去处理这些任务。 编写方法假装正在处理数据使用线程设置一些延时模拟同步等待的情况 Service
public class AsyncService {public void hello(){try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(业务进行中....);}
}编写AsyncController类 我们去写一个Controller测试一下 RestController
public class AsyncController {Autowiredprivate AsyncService asyncService;GetMapping(/hello)public String hello(){asyncService.hello();return OK;}}访问http://localhost:8080/hello进行测试3秒后出现OK这是同步等待的情况。 问题我们如果想让用户直接得到消息就在后台使用多线程的方式进行处理即可但是每次都需要自己手动去编写多线程的实现的话太麻烦了我们只需要用一个简单的办法在我们的方法上加一个简单的注解即可如下 给hello方法添加Async注解 //告诉Spring这是一个异步方法
Async
public void hello(){try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(业务进行中....);
}SpringBoot就会自己开一个线程池进行调用但是要让这个注解生效我们还需要在主程序上添加一个注解EnableAsync 开启异步注解功能 EnableAsync //开启异步注解功能
SpringBootApplication
public class SpringbootTaskApplication {public static void main(String[] args) {SpringApplication.run(SpringbootTaskApplication.class, args);}}7、重启测试网页瞬间响应后台代码依旧执行
邮件任务
邮件发送在我们的日常开发中也非常的多Springboot也帮我们做了支持
邮件发送需要引入spring-boot-start-mailSpringBoot 自动配置MailSenderAutoConfiguration定义MailProperties内容配置在application.yml中自动装配JavaMailSender测试邮件发送
测试 引入pom依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-mail/artifactId
/dependency看它引入的依赖可以看到 jakarta.mail dependencygroupIdcom.sun.mail/groupIdartifactIdjakarta.mail/artifactIdscopecompile/scope
/dependency查看自动配置类MailSenderAutoConfiguration 这个类中存在beanJavaMailSenderImpl 然后我们去看下配置文件 ConfigurationProperties(prefix spring.mail)
public class MailProperties {private static final Charset DEFAULT_CHARSET StandardCharsets.UTF_8;private String host;private Integer port;private String username;private String password;private String protocol smtp;private Charset defaultEncoding DEFAULT_CHARSET;private MapString, String properties new HashMap();private String jndiName;//set、get方法省略。。。
} 配置文件 spring:mail:username: 2356731504qq.compassword: 你的qq授权码host: smtp.qq.comproperties:mail:smtp:ssl:enable: true # qq需要配置ssl获取授权码在QQ邮箱中的设置-账户-开启pop3和smtp服务 Spring单元测试 package com.liming;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.MimeMessageHelper;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import java.io.File;SpringBootTestclass HelloApplicationTests {Autowiredprivate JavaMailSenderImpl javaMailSender;//邮件设置1一个简单的邮件Testvoid contextLoads() {SimpleMailMessage mailMessage new SimpleMailMessage();mailMessage.setSubject(黎明你好); // 主题mailMessage.setText(这是邮件发送测试。。。); // 正文mailMessage.setTo(2356731504qq.com); // 发送给谁mailMessage.setFrom(2356731504qq.com); // 谁发javaMailSender.send(mailMessage);}// 一个复杂的邮件Testvoid contextLoads2() throws MessagingException {MimeMessage mimeMessage javaMailSender.createMimeMessage();//组装MimeMessageHelper helper new MimeMessageHelper(mimeMessage, true);//正文helper.setSubject(黎明你好~plus);helper.setText(p stylecolor:red这是邮件发送测试/p, true);//附件helper.addAttachment(1.jpg, new File(D:\\Users\\Pictures\\Saved Pictures\\SAP-ABAP.jpg));helper.addAttachment(2.jpg, new File(D:\\Users\\Pictures\\Saved Pictures\\SAP-ABAP.jpg));helper.setTo(2356731504qq.com);helper.setFrom(2356731504qq.com);javaMailSender.send(mimeMessage);}}
