springboot简单使用定时(corn)和邮箱发送功能

在配置文件application.properties

spring.mail.username=7573548@qq.com
spring.mail.password=koapxsjyssdsdcbdjj(在自己邮箱设置拿到密码)
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

test类测试发送

package com.chen.springboot08assignment;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.MailMessage;
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;

@SpringBootTest
class Springboot08AssignmentApplicationTests {


    @Autowired
    JavaMailSenderImpl mailSender;
    @Test
    void contextLoads() {

        //一个简单的邮件
       SimpleMailMessage mailMessage= new SimpleMailMessage();
       mailMessage.setSubject("hello");
       mailMessage.setText("好好学习哈");
       mailMessage.setTo("1292331759046@qq.com");
       mailMessage.setFrom("7573548@qq.com");

        mailSender.send(mailMessage);
    }


    @Test
    void contextLoads2() throws MessagingException {

        //一个复杂的邮件
        MimeMessage mailMessage=mailSender.createMimeMessage();

        //组装
        MimeMessageHelper  helper=new MimeMessageHelper( mailMessage,true);
       //正文
        helper.setSubject("哈哈哈,下午好");
        helper.setText("<p style='color:red'>goodgoodstudy,daydayup</p>",true);

        //附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\86151\\Pictures\\Saved Pictures\\1.jpg"));
        helper.addAttachment("2.jpg",new File("C:\\Users\\86151\\Pictures\\Saved Pictures\\1.jpg"));
        helper.setTo("1291759046@qq.com");
        helper.setFrom("7573548@qq.com");
        mailSender.send(mailMessage);
    }

}

使用定时功能

在springboot启动类添加注解

@EnableScheduling//开启定时功能的注解


@SpringBootApplication
public class Springboot08AssignmentApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot08AssignmentApplication.class, args);
    }

}

测试使用

package com.chen.springboot08assignment.service;


import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class ScheduledService {

    //在一个特定的时间执行这个方法

    //cron表达式
    //秒 分 时 日 月 周
    /*
    30 15 10 * * ?      代表每天10点15分执行一次
    30 0/5 10,18 * * ?   代表每天10点和18点,每隔五分钟执行一次
    0 15 10 ? * 1-6       每个月的周一到周六10.15分钟执行一次
    0/2 * * * * ?       每隔两秒执行一次
    */
    @Scheduled(cron="20 16 15 * * ?")
    public void hello(){
        System.out.println("hello,你被执行了~");
    }

}

使用异步延时功能

在springboot启动类添加注解


@EnableAsync//开启异步注解功能


@SpringBootApplication
public class Springboot08AssignmentApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot08AssignmentApplication.class, args);
    }

}

创建一个AsyncService类


@Service
public class AsyncService {

    //告诉spring这是一个异步的方法
    @Async
    public void hello(){
        try{

            Thread.sleep(3000);

        }catch (InterruptedException e){
            e.printStackTrace();
        }
    System.out.println("数据正在处理。。。");

    }

}

创建一个控制类AsyncController


@RestController
public class AsyncController {


    @Autowired
    AsyncService asyncService;

    @RequestMapping("hello")
    public String hello(){
        asyncService.hello();//停止三秒,转圈
        return "ok";
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中,定时任务通常通过 `java.util.Timer` 或者 `java.util.concurrent.Executors` 的 ScheduledExecutorService 进行设置。如果你需要每小时整点执行任务,可以使用 cron 表达式(也称为 Quartz 配置)。cron 表达式是一种时间调度语言,用于精确控制任务何时运行。 对于每小时整点执行,对应的 cron 表达式会是这样: ```text 0 0 * * ? * ``` 这里各部分解释如下: - 第一个字段 `0` 表示分钟(0表示整点) - 第二个字段 `0` 表示小时(0表示开始的时间点) - 第三个字段 `*` 表示日期中的月份范围(*表示每个月都执行) - 第四个字段 `*` 表示星期几范围(*表示每周都执行) - 第五个字段 `?` 表示年份范围(由于不是每年都需要,所以用问号代表每天) 使用 Quartz 框架,你可以创建一个 `CronScheduleBuilder` 和 `Trigger` 对象,然后将它们结合在一起: ```java import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; public void scheduleJob() { // 创建作业 Trigger trigger = Trigger.newTrigger() .withIdentity("hourlyTask", "group1") .forJob(new MyJob()) .withSchedule(CronScheduleBuilder.cronSchedule("0 0 * * ? *")); // 创建并启动调度器 Scheduler scheduler = new StdSchedulerFactory().getScheduler(); try { scheduler.start(); scheduler.scheduleJob(trigger); System.out.println("Scheduled job for hourly execution."); } catch (SchedulerException e) { e.printStackTrace(); } } ``` 在这个例子中,`MyJob` 是你需要执行的实际任务类。记得处理 `SchedulerException` 并确保程序结束时正确关闭调度器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值