首页 > 解决方案 > Spring boot with lettuce:无法连接,事件执行器组被终止

问题描述

我用生菜创建了 Spring Boot 多模块项目。我的配置:

@Bean
    public LettuceConnectionFactory lettuceConnectionFactory(){
        return new LettuceConnectionFactory(host, port);
    }

    @Bean
    public RedisTemplate<String, FullMessage> redisTemplate() {
        RedisTemplate<String, FullMessage> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory());
        return template;
    }

我异步使用 RedisTemplate 其他模块:

@Async
@EventListener(ApplicationReadyEvent.class)
public void check() {

    while (true){
        try{

                List<ObjectRecord<String, FullMessage>> records = redisTemplate
                        .opsForStream()
                        .read(FullMessage.class, StreamOffset.fromStart(key));
           ...
}

此代码在多模块项目中不起作用,但在另一个项目(不是多模块)中工作我得到以下错误(无穷大):

java.lang.IllegalStateException: Cannot connect, Event executor group is terminated.
    at io.lettuce.core.AbstractRedisClient.initializeChannelAsync(AbstractRedisClient.java:283)
    at io.lettuce.core.RedisClient.connectStatefulAsync(RedisClient.java:314)
    at io.lettuce.core.RedisClient.connectStandaloneAsync(RedisClient.java:271)
    at io.lettuce.core.RedisClient.connect(RedisClient.java:204)
    at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.lambda$getConnection$1(StandaloneConnectionProvider.java:115)
    at java.base/java.util.Optional.orElseGet(Optional.java:369)
    at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:115)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1197)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1178)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedConnection(LettuceConnectionFactory.java:942)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getConnection(LettuceConnectionFactory.java:353)
    at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:132)
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:95)
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:82)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:215)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:188)
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:96)
    at org.springframework.data.redis.core.DefaultStreamOperations.read(DefaultStreamOperations.java:217)
    at org.springframework.data.redis.core.StreamOperations.read(StreamOperations.java:319)
    at org.springframework.data.redis.core.StreamOperations.read(StreamOperations.java:290)
    at uz.test.websocket.config.MainFIFOListener.check(MainFIFOListener.java:49)
    at uz.test.websocket.config.MainFIFOListener$$FastClassBySpringCGLIB$$a659b30d.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

标签: spring-bootspring-data-redislettuce

解决方案


推荐阅读