包含组件内容
RedisQueue:消息队列监听标识 RedisQueueInit:Redis队列监听器 RedisQueueListener:Redis消息队列监听实现 RedisQueueService:Redis消息队列服务工具
代码实现
RedisQueue
import java. lang. annotation. ElementType ;
import java. lang. annotation. Retention ;
import java. lang. annotation. RetentionPolicy ;
import java. lang. annotation. Target ;
@Target ( ElementType . TYPE )
@Retention ( RetentionPolicy . RUNTIME )
public @interface RedisQueue {
String value ( ) ;
}
RedisQueueInit
import jakarta. annotation. Resource ;
import java. util. Map ;
import java. util. concurrent. ExecutorService ;
import java. util. concurrent. Executors ;
import java. util. concurrent. ThreadFactory ;
import java. util. concurrent. TimeUnit ;
import java. util. concurrent. atomic. AtomicBoolean ;
import java. util. concurrent. atomic. AtomicInteger ;
import lombok. extern. slf4j. Slf4j ;
import org. jetbrains. annotations. NotNull ;
import org. redisson. api. RBlockingQueue ;
import org. redisson. api. RedissonClient ;
import org. springframework. beans. BeansException ;
import org. springframework. context. ApplicationContext ;
import org. springframework. context. ApplicationContextAware ;
import org. springframework. stereotype. Component ;
@Slf4j
@Component
public class RedisQueueInit implements ApplicationContextAware {
final AtomicBoolean shutdownRequested = new AtomicBoolean ( false ) ;
@Resource
private RedissonClient redissonClient;
private ExecutorService executorService;
@Override
public void setApplicationContext ( ApplicationContext applicationContext) throws BeansException {
Map < String , RedisQueueListener > map = applicationContext. getBeansOfType ( RedisQueueListener . class ) ;
executorService = createThreadPool ( "redis-queue" ) ;
for ( Map. Entry < String , RedisQueueListener > entry : map. entrySet ( ) ) {
RedisQueue redisQueue = entry. getValue ( ) . getClass ( ) . getAnnotation ( RedisQueue . class ) ;
if ( redisQueue != null ) {
String queueName = redisQueue. value ( ) ;
executorService. submit ( ( ) -> listenQueue ( queueName, entry. getValue ( ) ) ) ;
}
}
}
private ExecutorService createThreadPool ( String namePrefix) {
return Executors . newFixedThreadPool (
Runtime . getRuntime ( ) . availableProcessors ( ) ,
new NamedThreadFactory ( namePrefix)
) ;
}
private void listenQueue ( String queueName, RedisQueueListener redisQueueListener) {
RBlockingQueue < ? > blockingQueue = redissonClient. getBlockingQueue ( queueName) ;
log. info ( "Redis队列监听: {}" , queueName) ;
while ( true ) {
if ( shutdownRequested. get ( ) || redissonClient. isShutdown ( ) ) {
log. info ( "Redisson已关闭,停止监听队列: {}" , queueName) ;
break ;
}
try {
Object message = blockingQueue. take ( ) ;
redisQueueListener. invoke ( message) ;
} catch ( InterruptedException e) {
Thread . currentThread ( ) . interrupt ( ) ;
log. error ( "监听队列线程被中断" , e) ;
break ;
} catch ( Exception e) {
log. error ( "监听队列线程错误" , e) ;
}
}
}
public void shutdown ( ) {
if ( executorService != null ) {
executorService. shutdown ( ) ;
try {
if ( ! executorService. awaitTermination ( 60 , TimeUnit . SECONDS ) ) {
executorService. shutdownNow ( ) ;
}
} catch ( InterruptedException ex) {
executorService. shutdownNow ( ) ;
Thread . currentThread ( ) . interrupt ( ) ;
}
}
shutdownRequested. set ( true ) ;
if ( redissonClient != null && ! redissonClient. isShuttingDown ( ) ) {
redissonClient. shutdown ( ) ;
}
}
private static class NamedThreadFactory implements ThreadFactory {
private final AtomicInteger threadNumber = new AtomicInteger ( 1 ) ;
private final String namePrefix;
public NamedThreadFactory ( String prefix) {
this . namePrefix = prefix;
}
@Override
public Thread newThread ( @NotNull Runnable r) {
return new Thread ( r, namePrefix + "-" + threadNumber. getAndIncrement ( ) ) ;
}
}
}
RedisQueueListener
public interface RedisQueueListener < T > {
void invoke ( T content) ;
}
RedisQueueService
import jakarta. annotation. Resource ;
import java. util. concurrent. TimeUnit ;
import org. redisson. api. RBlockingQueue ;
import org. redisson. api. RDelayedQueue ;
import org. redisson. api. RedissonClient ;
import org. springframework. stereotype. Component ;
@Component
public class RedisQueueService {
@Resource
private RedissonClient redissonClient;
public < T > void send ( String queueName, T content) {
RBlockingQueue < T > blockingQueue = redissonClient. getBlockingQueue ( queueName) ;
blockingQueue. add ( content) ;
}
public < T > void sendDelay ( String queueName, T content, long delay, TimeUnit timeUnit) {
RBlockingQueue < T > blockingFairQueue = redissonClient. getBlockingQueue ( queueName) ;
RDelayedQueue < T > delayedQueue = redissonClient. getDelayedQueue ( blockingFairQueue) ;
delayedQueue. offer ( content, delay, timeUnit) ;
}
public < T > void sendDelay ( String queueName, T content, long delay) {
RBlockingQueue < T > blockingFairQueue = redissonClient. getBlockingQueue ( queueName) ;
RDelayedQueue < T > delayedQueue = redissonClient. getDelayedQueue ( blockingFairQueue) ;
delayedQueue. offer ( content, delay, TimeUnit . MILLISECONDS ) ;
}
}
测试
创建监听对象
import cn. yiyanc. infrastructure. redis. annotation. RedisQueue ;
import cn. yiyanc. infrastructure. redis. queue. RedisQueueListener ;
import lombok. extern. slf4j. Slf4j ;
import org. springframework. stereotype. Component ;
@Slf4j
@Component
@RedisQueue ( "test" )
public class TestListener implements RedisQueueListener < String > {
@Override
public void invoke ( String content) {
log. info ( "队列消息接收 >>> {}" , content) ;
}
}
测试用例
import jakarta. annotation. Resource ;
import org. springframework. web. bind. annotation. PostMapping ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ;
@RestController
@RequestMapping ( "queue" )
public class QueueController {
@Resource
private RedisQueueService redisQueueService;
@PostMapping ( "send" )
public void send ( String message) {
redisQueueService. send ( "test" , message) ;
redisQueueService. sendDelay ( "test" , "delay messaege -> " + message, 1000 ) ;
}
}
测试结果