java

SpringBoot03:同步、异步、定时、复杂邮件任务

2019-04-07  本文已影响0人  赶路人_3864

1.同步任务:

      新建一个service的类:

    import org.springframework.scheduling.annotation.Async;

    import org.springframework.stereotype.Service;

    @Service

    public class Asyncservice {

      //定义方法3s后执行某个事件

        public void hello(){

            try {

                Thread.sleep(3000);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            System.out.println("同步任务处理中");

        }

    }

写一个类用于测试:

    import com.yzx.renwu.service.Asyncservice;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.web.bind.annotation.GetMapping;

    import org.springframework.web.bind.annotation.RestController;

    @RestController

    public class Asynscon {

        @Autowired

        Asyncservice asyncservice;

        @GetMapping("hello")

        public String hello(){

            asyncservice.hello();

            return "success";

        }

    }

  可以发现打开http://localhost:xxx/hello后三秒才加载出success并在控制台打印同步任务处理中

  2.异步任务:

          在之前基础上我们加一些注解便可实现异步任务

    @Service

    public class Asyncservice {

      //此处新加异步注解

        @Async

        public void hello(){

            try {

                Thread.sleep(3000);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            System.out.println("异步任务处理中");

        }

    }

同时我们要加开启异步的注解:

        //开启异步

        @EnableAsync

        @SpringBootApplication

        public class RenwuApplication {

            public static void main(String[] args) {

                SpringApplication.run(RenwuApplication.class, args);

            }

        }

此时在发送/hello请求我们发现,success是直接出来的,而控制台是三秒之后在打印的。

3.定时任务

    import org.springframework.scheduling.annotation.Scheduled;

    import org.springframework.stereotype.Service;

    @Service

    public class Schedule {

        /**

        * second,minute,hour,day of month,month,day of week,

        * 0 * * * * MON-SAT

        * 0,1,2,3,4 * * * * MON-SAT枚举方式代表每到1,2,3,4秒执行一次

        * 0-4 * * * *代表0到4的区间

        * 0/4每4s

        */

        //每整分执行一次

        @Scheduled(cron="0 * * * * MON-SAT")

        public  void date(){

            System.out.println("time执行");

        }

    }

  在appliction类开启定时任务功能注解

    @EnableScheduling

    @SpringBootApplication

    public class RenwuApplication {

        public static void main(String[] args) {

            SpringApplication.run(RenwuApplication.class, args);

        }

4.复杂邮件任务

      加入mail相关依赖:

      <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-mail</artifactId>

        </dependency>

在邮箱中开启:相关服务最好全部开启,并生成授权码如qq邮箱

在appliction.properties中写入相关配置,注意qq和网易的host是不一样的。而且此处的密码是之前的授权码

    spring.mail.username=59623xxxx@qq.com

    spring.mail.password=yhxmaoomaxdvbxxc

    spring.mail.host=smtp.qq.com

测试类:

    @RunWith(SpringRunner.class)

    @SpringBootTest

    public class RenwuApplicationTests {

        @Autowired

        JavaMailSenderImpl mailSender;

        @Test

        public void contextLoads() throws Exception {

            MimeMessage message=mailSender.createMimeMessage();

            MimeMessageHelper helper=new MimeMessageHelper(message,true);

            //上传的标题

            helper.setSubject("上传图片啊");

            //此处可以写html代码

            helper.setText("<b style='color:red'>樱花雨</b>",true);

            //收件人

            helper.setTo("596238xxx@qq.com");

            //发件人

            helper.setFrom("596238xxx@qq.com");

            //你需要发送的附件

            helper.addAttachment("1.png",new File("C:/Users/Administrator/Desktop/1.png"));

            mailSender.send(message);

        }

    }

如果发送失败。仔细对照代码查看是否邮箱配置没开、授权码是否正确、mail依赖是否已经加载进来、是否还有一些配置没配。如果都正确建议重启idea。有时候会有网络延迟的问题,我之前就发生过这种问题。

                                                                            ------------------新手上路,请多指教

上一篇 下一篇

猜你喜欢

热点阅读