SpringBoot异步操作Async如何改写为长尾?

2026-04-19 06:411阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计636个文字,预计阅读时间需要3分钟。

SpringBoot异步操作Async如何改写为长尾?

前言:最近看了Spring Boot的异步操作,学到了使用@Async注解来实现异步功能。这样一来,项目中原本的发送邮件通知就都换成了异步操作,不再是去新建一个线程来发送通知。

前言

最近看了下springboot的异步操作,学到了使用async注解来实现异步操作的功能,这不就立马把项目中的发送邮件通知就都换成了异步的操作,而不是去新建一个线程来发送通知,虽然async注解也是通过新线程的方式来实现,但就很美观。下面就来看看async的简单示例

1. 启动类添加注解@EnableAsync

@SpringBootApplication @MapperScan(basePackages = {"com.fyg.mapper"}) @ServletComponentScan @EnableAsync public class BlogApplication { public static void main(String[] args) throws UnknownHostException { ConfigurableApplicationContext application = SpringApplication.run(BlogApplication.class, args); Environment env = application.getEnvironment(); String ip = InetAddress.getLocalHost().getHostAddress(); String port = env.getProperty("server.port"); String path = env.getProperty("server.servlet.context-path"); System.out.println("\n----------------------------------------------------------\n\t" + "blog is running! Access URLs:\n\t" + "Local: \t\tlocalhost:" + port + path + "/\n\t" + "External: \t" + ip + ":" + port + path + "/\n\t" + "Knife4j-ui: \t" + ip + ":" + port + path + "/doc.html\n\t" + "----------------------------------------------------------"); } }

2. 在使用的方法上添加注解@Async

@Async("threadPoolTaskExecutor") public void emailNoticeMe(String subject,String content) { // 构建一个邮件对象 SimpleMailMessage message = new SimpleMailMessage(); // 设置邮件主题 message.setSubject(subject); // 设置邮件发送者 message.setFrom(Objects.requireNonNull(javaMailSender.getUsername())); // 设置邮件接收者,可以有多个接收者,中间用逗号隔开 message.setTo("1248954763@qq.com"); // 设置邮件发送日期 message.setSentDate(new Date()); // 设置邮件的正文 message.setText(content); // 发送邮件 javaMailSender.send(message); }

可以指定线程池的名称,以上代码我是已经指定了,不指定名称就会使用默认的线程池

3.调用

@Transactional(rollbackFor = Exception.class) public ResponseResult applyFriendLink(FriendLink friendLink) { Assert.isTrue(StringUtils.isNotBlank(friendLink.getUrl()),"输入正确的网址!"); friendLink.setStatus(APPLY.getCode()); Assert.isTrue(!friendLink.getUrl().contains("fyg.com") && !friendLink.getUrl().contains("baidu.com"),"不合法的网址!"); //如果已经申请过友链 再次接入则会进行下架处理 需重新审核 FriendLink entity = baseMapper.selectOne(new QueryWrapper<FriendLink>() .eq(SqlConf.URL,friendLink.getUrl())); if (entity != null) { friendLink.setId(entity.getId()); baseMapper.updateById(friendLink); }else { baseMapper.insert(friendLink); } //异步操作邮箱发送 emailService.emailNoticeMe("友链接入通知","网站有新的友链接入啦("+friendLink.getUrl()+"),快去审核吧!!!"); return ResponseResult.success(); }

需要注意的是调用方法和被调用方法不能在同类中,否则就会失效

SpringBoot异步操作Async如何改写为长尾?

本文共计636个文字,预计阅读时间需要3分钟。

SpringBoot异步操作Async如何改写为长尾?

前言:最近看了Spring Boot的异步操作,学到了使用@Async注解来实现异步功能。这样一来,项目中原本的发送邮件通知就都换成了异步操作,不再是去新建一个线程来发送通知。

前言

最近看了下springboot的异步操作,学到了使用async注解来实现异步操作的功能,这不就立马把项目中的发送邮件通知就都换成了异步的操作,而不是去新建一个线程来发送通知,虽然async注解也是通过新线程的方式来实现,但就很美观。下面就来看看async的简单示例

1. 启动类添加注解@EnableAsync

@SpringBootApplication @MapperScan(basePackages = {"com.fyg.mapper"}) @ServletComponentScan @EnableAsync public class BlogApplication { public static void main(String[] args) throws UnknownHostException { ConfigurableApplicationContext application = SpringApplication.run(BlogApplication.class, args); Environment env = application.getEnvironment(); String ip = InetAddress.getLocalHost().getHostAddress(); String port = env.getProperty("server.port"); String path = env.getProperty("server.servlet.context-path"); System.out.println("\n----------------------------------------------------------\n\t" + "blog is running! Access URLs:\n\t" + "Local: \t\tlocalhost:" + port + path + "/\n\t" + "External: \t" + ip + ":" + port + path + "/\n\t" + "Knife4j-ui: \t" + ip + ":" + port + path + "/doc.html\n\t" + "----------------------------------------------------------"); } }

2. 在使用的方法上添加注解@Async

@Async("threadPoolTaskExecutor") public void emailNoticeMe(String subject,String content) { // 构建一个邮件对象 SimpleMailMessage message = new SimpleMailMessage(); // 设置邮件主题 message.setSubject(subject); // 设置邮件发送者 message.setFrom(Objects.requireNonNull(javaMailSender.getUsername())); // 设置邮件接收者,可以有多个接收者,中间用逗号隔开 message.setTo("1248954763@qq.com"); // 设置邮件发送日期 message.setSentDate(new Date()); // 设置邮件的正文 message.setText(content); // 发送邮件 javaMailSender.send(message); }

可以指定线程池的名称,以上代码我是已经指定了,不指定名称就会使用默认的线程池

3.调用

@Transactional(rollbackFor = Exception.class) public ResponseResult applyFriendLink(FriendLink friendLink) { Assert.isTrue(StringUtils.isNotBlank(friendLink.getUrl()),"输入正确的网址!"); friendLink.setStatus(APPLY.getCode()); Assert.isTrue(!friendLink.getUrl().contains("fyg.com") && !friendLink.getUrl().contains("baidu.com"),"不合法的网址!"); //如果已经申请过友链 再次接入则会进行下架处理 需重新审核 FriendLink entity = baseMapper.selectOne(new QueryWrapper<FriendLink>() .eq(SqlConf.URL,friendLink.getUrl())); if (entity != null) { friendLink.setId(entity.getId()); baseMapper.updateById(friendLink); }else { baseMapper.insert(friendLink); } //异步操作邮箱发送 emailService.emailNoticeMe("友链接入通知","网站有新的友链接入啦("+friendLink.getUrl()+"),快去审核吧!!!"); return ResponseResult.success(); }

需要注意的是调用方法和被调用方法不能在同类中,否则就会失效

SpringBoot异步操作Async如何改写为长尾?