spring配置定时器的时间设置

时间大小由小到大排列,从秒开始,顺序为 秒,分,时,天,月,年    *为任意 ?为无限制。
具体如下:
"0/10 * * * * ?" 每10秒触发
"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 spring boot 定时器库存预警的代码示例: 首先,在 pom.xml 中添加依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 然后,在 application.properties 中配置邮件发送相关的属性: ``` spring.mail.host=smtp.example.com spring.mail.port=587 spring.mail.username=yourusername spring.mail.password=yourpassword spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true ``` 接下来,创建一个定时器类 InventoryWarningTask,并添加 @Component 注解,表示这是一个组件类: ```java @Component public class InventoryWarningTask { @Autowired private ProductService productService; @Autowired private JavaMailSender javaMailSender; @Scheduled(cron = "${inventory.warning.cron}") public void execute() { List<Product> products = productService.getProducts(); for (Product product : products) { if (product.getInventory() < product.getWarningInventory()) { String subject = "库存预警:" + product.getName(); String text = "商品名称:" + product.getName() + "\n" + "当前库存:" + product.getInventory() + "\n" + "警戒库存:" + product.getWarningInventory(); sendEmail(subject, text); } } } private void sendEmail(String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("sender@example.com"); message.setTo("recipient@example.com"); message.setSubject(subject); message.setText(text); javaMailSender.send(message); } } ``` 在定时器类中,我们使用 @Autowired 注解将 ProductService 和 JavaMailSender 注入进来。在 execute() 方法中,我们获取所有商品,检查库存是否低于警戒库存。如果是,则发送邮件给管理员。 定时器的执行时间通过 @Scheduled 注解的 cron 属性指定,这里使用了 ${inventory.warning.cron} 属性占位符,需要在 application.properties 中定义该属性的值,例如: ``` inventory.warning.cron=0 0 12 * * ? ``` 表示每天中午 12 点执行一次。 以上就是一个简单的 spring boot 定时器库存预警的代码示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值