经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Redis » 查看文章
使用Redis+SpringBoot实现定时任务测试 - Tom-shushu
来源:cnblogs  作者:Tom-shushu  时间:2021/3/29 9:04:38  对本文有异议

Redis实现定时任务是基于对RedisKey值的监控

具体代码实现:

代码GitHub地址:https://github.com/Tom-shushu/Project
  • 建一个SpringBoot项目
  • 引入依赖
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.4.4</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>redistask</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>redistask</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-redis</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28.  
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-test</artifactId>
  32. <scope>test</scope>
  33. </dependency>
  34. </dependencies>
  35.  
  36. <build>
  37. <plugins>
  38. <plugin>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-maven-plugin</artifactId>
  41. </plugin>
  42. </plugins>
  43. </build>
  44. </project>
  • 配置文件
  1. spring.redis.host=127.0.0.1
  2. spring.redis.port=6379
  3. spring.redis.timeout=10000
  • 新建一个配置类
  1. package com.zhouhong.redistask.redistaskconfig;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.data.redis.connection.RedisConnectionFactory;
  5. import org.springframework.data.redis.listener.RedisMessageListenerContainer;
  6. /**
  7. * description: Redis配置类
  8. * @author: zhouhong
  9. * @version: V1.0.0
  10. * @date: 2021年3月19日 上午10:58:24
  11. */
  12. @Configuration
  13. public class RedisTaskConfig {
  14. @Bean
  15. RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
  16. RedisMessageListenerContainer container = new RedisMessageListenerContainer();
  17. container.setConnectionFactory(connectionFactory);
  18. return container;
  19. }
  20. }
  • 新建Controller,设置不同过期时间的Key值,注意这里key值最好使用当前的业务标识做前缀,不然可能出现key重复的现象。
  1. package com.zhouhong.redistask.redistaskcontroller;
  2. import java.util.Date;
  3. import java.util.concurrent.TimeUnit;
  4. import org.apache.logging.log4j.LogManager;
  5. import org.apache.logging.log4j.Logger;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.RestController;
  11. /**
  12. * description: 测试Redis定时Controller类
  13. * @author: zhouhong
  14. * @version: V1.0.0
  15. * @date: 2021年3月19日 上午10:59:21
  16. */
  17. @RestController
  18. public class RedisTaskController {
  19. @Autowired
  20. private RedisTemplate< String, String> template;
  21. Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
  22. /**
  23. * 设置定时key,这里key最好使用业务前缀,防止名字相同
  24. * @return
  25. */
  26. @RequestMapping(value = "putkeys", method = RequestMethod.POST)
  27. public String putRedisTaskKeys() {
  28. Date date = new Date();
  29. logger.info("业务开始时间:" + date);
  30. String key10S = "business1"+"|"+"key10S"+"|"+"其他业务中需要使用到的参数";
  31. String key20S = "business1"+"|"+"key20S"+"|"+"其他业务中需要使用到的参数";
  32. template.opsForValue().set(key10S, "values", 10, TimeUnit.SECONDS);
  33. template.opsForValue().set(key20S, "values", 20, TimeUnit.SECONDS);
  34. return "RedisKey过期键设置成功";
  35. }
  36. }
  • 新建Service用来监控过期Key,并且针对不同时间做不同的业务
  1. package com.zhouhong.redistask.service;
  2. import java.util.Date;
  3. import org.apache.logging.log4j.LogManager;
  4. import org.apache.logging.log4j.Logger;
  5. import org.springframework.data.redis.connection.Message;
  6. import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
  7. import org.springframework.data.redis.listener.RedisMessageListenerContainer;
  8. import org.springframework.stereotype.Component;
  9. import org.springframework.stereotype.Service;
  10. /**
  11. * description: RedisKey键监听以及业务逻辑处理
  12. * @author: zhouhong
  13. * @version: V1.0.0
  14. * @date: 2021年3月19日 上午10:58:52
  15. */
  16. @Service
  17. @Component
  18. public class RedisTaskService extends KeyExpirationEventMessageListener {
  19. Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
  20. /**
  21. * @param listenerContainer
  22. */
  23. public RedisTaskService(RedisMessageListenerContainer listenerContainer) {
  24. super(listenerContainer);
  25. }
  26. @Override
  27. public void onMessage(Message message, byte[] pattern) {
  28. String expiredKey = message.toString();
  29. // 将拿到的过期键使用之前拼接时的特殊符号分割成字符数组
  30. String[] expiredKeyArr = expiredKey.split("\\|");
  31. String businessSign = expiredKeyArr[0].toString();
  32. String expiredTimeSign = expiredKeyArr[1].toString();
  33. String othersParm = expiredKeyArr[2].toString();
  34. logger.info(businessSign + expiredTimeSign + othersParm);
  35. Date date = new Date();
  36. // 只有本业务才执行以下操作
  37. if (businessSign.equals("business1")) {
  38. if (expiredTimeSign.equals("key10S")) {
  39. // 定时十秒钟后业务处理
  40. logger.info("十秒钟时的时间:"+ date);
  41. logger.info("定时任务10秒钟已到,下面处理相关业务逻辑代码!!!");
  42. logger.info("10秒钟后的业务逻辑代码,其他业务参数" + othersParm);
  43. } else if (expiredTimeSign.equals("key20S")) {
  44. // 定时十秒钟后业务处理
  45. logger.info("二十秒钟时的时间:"+ date);
  46. logger.info("定时任务20秒钟已到,下面处理相关业务逻辑代码!!!");
  47. logger.info("20秒钟后的业务逻辑代码,其他业务参数" + othersParm);
  48. }
  49. } else {
  50. logger.error("非business1业务不做处理");
  51. }
  52. }
  53. }
  • 演示:

定时成功!!

原文链接:http://www.cnblogs.com/Tom-shushu/p/14558413.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号