当前位置: 首页 > news >正文

深圳网站建设专家域名访问网站怎么下载

深圳网站建设专家,域名访问网站怎么下载,广元网站制作,专业制作网站建设异步任务 创建一个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);}}
http://www.hkea.cn/news/14490723/

相关文章:

  • 珠海网站建设那家好一个商务宣传怎么做网站合适
  • 网站整体建设方案设计seo快速排名软件网址
  • php网站开发技术题目小程序外包公司发展前景
  • 郴州微网站建设做网站代理怎么样
  • 深圳做网站乐云seo598四川建设人力资源网官网
  • 专业版装修用什么网站做导航条门户网站建设
  • seo网站建设时文章频率苏州高端企业网站建设
  • 各大网站免费观看制作网页系统
  • 网站备案查询不到无极在线房屋出租信息
  • 网站文件夹没有权限360推广登录
  • 企业网站建设御彩云源码之家关闭了
  • 东阳网站建设有哪些株洲最新通告
  • 浙江网站建设dyfwzxwap网站开发自适应手机屏幕开源包
  • 备案的网站名称可以改吗线上推广外包公司
  • 如何建设高等数学课程网站培训计划方案
  • 做网站的外包公司申请建设门户网站的申请
  • 做免费网站2345手机浏览器
  • 婚恋网站建设成本html5高端网站建设
  • qq群引流推广网站网络运维面试题
  • 快速生成网页的软件嘉兴网站建设优化
  • 安庆网站建设价格公众号开发者密码是什么意思
  • seo网站平台如何查看网页源代码
  • 欧赛网站建设厦门专业网站排名推广
  • 翔云白云手机网站建设公司网站数据分析
  • 达州建设局网站c 网站开发需要学什么软件有哪些
  • 没有备案的网站怎么访问互动网站
  • 桓台做网站邢台163最新交友
  • 做网站密云seo wordpress主题
  • 成都响应网站建设珠海网站建设公司哪个好
  • 建设网站的优点跟缺点微信下载并登录