使用Redis+SpringBoot实现定时任务测试
Redis实现定时任务是基于对RedisKey值的监控
具体代码实现:
代码GitHub地址:https://github.com/Tom-shushu/Project
- 建一个SpringBoot项目
- 引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>redistask</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redistask</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 配置文件
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.timeout=10000
- 新建一个配置类
package com.zhouhong.redistask.redistaskconfig; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer; /**
* description: Redis配置类
* @author: zhouhong
* @version: V1.0.0
* @date: 2021年3月19日 上午10:58:24
*/
@Configuration
public class RedisTaskConfig {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}
- 新建Controller,设置不同过期时间的Key值,注意这里key值最好使用当前的业务标识做前缀,不然可能出现key重复的现象。
package com.zhouhong.redistask.redistaskcontroller; import java.util.Date;
import java.util.concurrent.TimeUnit; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; /**
* description: 测试Redis定时Controller类
* @author: zhouhong
* @version: V1.0.0
* @date: 2021年3月19日 上午10:59:21
*/ @RestController
public class RedisTaskController { @Autowired
private RedisTemplate< String, String> template;
Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
/**
* 设置定时key,这里key最好使用业务前缀,防止名字相同
* @return
*/
@RequestMapping(value = "putkeys", method = RequestMethod.POST)
public String putRedisTaskKeys() {
Date date = new Date();
logger.info("业务开始时间:" + date);
String key10S = "business1"+"|"+"key10S"+"|"+"其他业务中需要使用到的参数";
String key20S = "business1"+"|"+"key20S"+"|"+"其他业务中需要使用到的参数";
template.opsForValue().set(key10S, "values", 10, TimeUnit.SECONDS);
template.opsForValue().set(key20S, "values", 20, TimeUnit.SECONDS);
return "RedisKey过期键设置成功";
} }
- 新建Service用来监控过期Key,并且针对不同时间做不同的业务
package com.zhouhong.redistask.service; import java.util.Date; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
* description: RedisKey键监听以及业务逻辑处理
* @author: zhouhong
* @version: V1.0.0
* @date: 2021年3月19日 上午10:58:52
*/
@Service
@Component
public class RedisTaskService extends KeyExpirationEventMessageListener { Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
/**
* @param listenerContainer
*/
public RedisTaskService(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
@Override
public void onMessage(Message message, byte[] pattern) {
String expiredKey = message.toString();
// 将拿到的过期键使用之前拼接时的特殊符号分割成字符数组
String[] expiredKeyArr = expiredKey.split("\\|");
String businessSign = expiredKeyArr[0].toString();
String expiredTimeSign = expiredKeyArr[1].toString();
String othersParm = expiredKeyArr[2].toString(); logger.info(businessSign + expiredTimeSign + othersParm);
Date date = new Date();
// 只有本业务才执行以下操作
if (businessSign.equals("business1")) {
if (expiredTimeSign.equals("key10S")) {
// 定时十秒钟后业务处理
logger.info("十秒钟时的时间:"+ date);
logger.info("定时任务10秒钟已到,下面处理相关业务逻辑代码!!!");
logger.info("10秒钟后的业务逻辑代码,其他业务参数" + othersParm);
} else if (expiredTimeSign.equals("key20S")) {
// 定时十秒钟后业务处理
logger.info("二十秒钟时的时间:"+ date);
logger.info("定时任务20秒钟已到,下面处理相关业务逻辑代码!!!");
logger.info("20秒钟后的业务逻辑代码,其他业务参数" + othersParm);
}
} else {
logger.error("非business1业务不做处理");
}
}
}
- 演示:
定时成功!!
使用Redis+SpringBoot实现定时任务测试的更多相关文章
- 基于SpringBoot实现定时任务的设置(常用:定时清理数据库)
1.构建SpringBoot工程项目 1)创建一个Springboot工程,在它的程序入口加上@EnableScheduling,开启调度任务. @SpringBootApplication @Ena ...
- 玩转SpringBoot之定时任务详解
序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...
- SpringBoot整合定时任务和异步任务处理 3节课
1.SpringBoot定时任务schedule讲解 定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 ...
- SpringBoot整合定时任务和异步任务处理
SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 timer:配置比较麻烦,时间延后问题, ...
- springboot实现定时任务的方式
springboot实现定时任务的方式 a Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程 ...
- 记一次Redis和NetMQ的测试
Redis是一个高速缓存K-V数据库,而NetMQ是ZeroMQ的C#实现版本,两者是完全不同的东西. 最近做游戏服务器的时候想到,如果选择一个组件来做服务器间通信的话,ZeroMQ绝对是一个不错的选 ...
- redis实现主从复制-单机测试
一.redis实现主从复制-单机测试1.安装redis tar -zxvf redis-2.8.4.tar.gzcd redis-2.8.4make && make install2. ...
- Redis介绍及Jedis测试
1.Redis简介 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes ...
- SpringBoot 配置定时任务
SpringBoot启用定时任务,其内部集成了成熟的框架,因此我们可以很简单的使用它. 开启定时任务 @SpringBootApplication //设置扫描的组件的包 @ComponentScan ...
- SpringBoot - 添加定时任务
SpringBoot 添加定时任务 EXample1: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spri ...
随机推荐
- dotnet 读 WPF 源代码笔记 WriteableBitmap 的渲染和更新是如何实现
在 WPF 框架提供方便进行像素读写的 WriteableBitmap 类,本文来告诉大家在咱写下像素到 WriteableBitmap 渲染,底层的逻辑 之前我使用 WriteableBitmap ...
- 读取 k8s 存储在 etcd 上的数据
读取 k8s 存储在 etcd 上的数据 Etcd Assistant 是一款 Etcd 可视化管理工具,便捷高效地操作您的 etcd 集群:支持多种键的视图:管理租约.用户.角色和权限. etcd是 ...
- 应急响应web1
应急响应的过程 目的:分析攻击时间.攻击操作.攻击结果.安全修复等并给出合理的解决方案. 保护阶段:直接断网,保护现场,看是否能够恢复数据: 分析阶段:对入侵过程进行分析,常见的方法为指纹库搜索.日志 ...
- angular 16 路由守卫更新
在 angular16 中守卫使用方式进行了更新,route 守卫被弃用(取消了CanActivate的使用),新增了功能性守卫(CanActivateFn),支持 inject 注入,官网提供了一个 ...
- vulnhub靶场-->MATRIX-BREAKOUT: 2 MORPHEUS
靶机下载地址 MATRIX-BREAKOUT: 2 MORPHEUS << 点我下载 开始打靶 IP发现 nmap扫描网段发现靶机ip:192.168.111.139 端口发现 对靶机进行 ...
- elementui table tree懒加载只能执行一次的解决办法
绑定 table的:key为随机值,在每次查询更新table时,更改key,就能刷新 table tree 懒加载只能第一次有效的问题, 本来那个懒加载只能执行一次,即使重新绑定了数据列表,再展开,也 ...
- RocketMQ消息过滤机制源码详解
#RocketMQ提供了2种消息过滤的方式: TAG 过滤 SQL92 过滤 SQL过滤默认是没有打开的,如果想要支持,必须在broker的配置文件中设置:enablePropertyFilter = ...
- 论GNU、Linux和GNU/Linux之间的关系
相信很多人看到了这个标题就会产生疑问,这篇文章到底要讲什么东西?在回答这个问题之前,我先提出几个问题? 1. 什么是Linux? 2. 什么是GNU? 3. GNU/Linux是什么玩意儿? 在回答了 ...
- Vue cli之创建组件
一般在开发中,我们会人为把组件分2个目录存放,一个代表的页面组件,另一个代表页面一部分的子组件. src/ |- views/ |- Home.vue |- components/ |- App.vu ...
- c#动态执行脚本的3种方式详解
1.使用Roslyn编译器 2.使用IronPython或IronRuby 3.使用JavaScript引擎 在C#中,可以使用一些第三方库或内置类库实现动态执行脚本的功能.以下是几个常用的方案: 1 ...