In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use Redis+SpringBoot to achieve timed task testing". In daily operation, I believe many people have doubts about how to use Redis+SpringBoot to achieve timed task testing. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Redis+SpringBoot to achieve timed task testing". Next, please follow the editor to study!
The realization of scheduled tasks by Redis is based on the monitoring of the value of Rediskey.
Specific code implementation:
Build a SpringBoot project
Introduce dependency
4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.4 com.example redistask 0.0.1-SNAPSHOT redistask Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-data-redis org.springframework.boot Spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
Configuration file
Spring.redis.host=127.0.0.1spring.redis.port=6379spring.redis.timeout=10000
Create a new configuration class
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 configuration class * @ author: zhouhong * @ version: V1.0.0 * @ date: 10:58:24 * / @ Configurationpublic class RedisTaskConfig {@ Bean RedisMessageListenerContainer container (RedisConnectionFactory connectionFactory) {RedisMessageListenerContainer container = new RedisMessageListenerContainer (); container.setConnectionFactory (connectionFactory); return container;}}
Create a new Controller and set the key value for different expiration times. Note that the key value should be prefixed with the current business ID, otherwise duplicate key may occur.
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: test Redis timed Controller class * @ author: zhouhong * @ version: V1.0.0 * @ date: 10:59:21 * / @ RestControllerpublic class RedisTaskController {@ Autowiredprivate RedisTemplate on March 19th, 2021
< String, String>Template; Logger logger = LogManager.getLogger (LogManager.ROOT_LOGGER_NAME); / * set timing key, where key is best to use business prefixes to prevent the same name * @ return * / @ RequestMapping (value = "putkeys", method = RequestMethod.POST) public String putRedisTaskKeys () {Date date = new Date (); logger.info ("Business start time:" + date) String key10S = "business1" + "|" + "key10S" + "|" + "parameters needed in other businesses"; String key20S = "business1" + "|" + "key20S" + "|" + "parameters needed in other businesses"; template.opsForValue () .set (key10S, "values", 10, TimeUnit.SECONDS); template.opsForValue () .set (key20S, "values", 20, TimeUnit.SECONDS) Return "RedisKey expired key set successfully";}}
Create a new Service to monitor the expired Key and do different business for different times
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 key snooping and business logic processing * @ author: zhouhong * @ version: V1.0.0 * @ date: 10:58:52 on March 19, 2021 * / @ Service@Componentpublic class RedisTaskService extends KeyExpirationEventMessageListener {Logger logger = LogManager.getLogger (LogManager.ROOT_LOGGER_NAME); / * * @ param listenerContainer * / public RedisTaskService (RedisMessageListenerContainer listenerContainer) {super (listenerContainer) } @ Overridepublic void onMessage (Message message, byte [] pattern) {String expiredKey = message.toString (); / / divide the special symbol of the acquired expired key into a character array 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 (); / / only this service performs the following operations: if (businessSign.equals ("business1")) {if (expiredTimeSign.equals ("key10S")) {/ / Service processing logger.info after ten seconds ("time at ten seconds:" + date) Logger.info ("the scheduled task has been up for 10 seconds, let's deal with the relevant business logic code!") ; logger.info ("business logic code after 10 seconds, other business parameters" + othersParm);} else if (expiredTimeSign.equals ("key20S")) {/ / Business processing logger.info after 10 seconds ("time at 20 seconds: + date); logger.info (" scheduled task 20 seconds have arrived, let's deal with the relevant business logic code!! ") ; logger.info ("business logic code after 20 seconds, other business parameters" + othersParm);}} else {logger.error ("non-business1 business is not processed");}
Demo:
Success on time!
At this point, the study on "how to use Redis+SpringBoot to achieve scheduled task testing" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.