Moh*_*mad 3 java spring multithreading redis
Redis 3.0.5
Spring Data Redis 1.3.6
jedis 2.6.3
- 我们的 Web 应用程序通过 pub/sub 从 redis 接收数据。
- 还以键/值对的形式在 redis 上执行读/写数据。
- 读/写发生在侦听器线程、独立监控线程和 http 请求线程上。
- 我们为 Listener 和 redis 模板使用了相同的连接工厂
- 我们的 redis 服务器配置了“timeout=30”
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="nnnn"></property>
<property name="port" value="nnnn"></property>
<property name="password" value="****"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
<bean id="redisContainer"
class="org.springframework.data.redis.listener.RedisMessageListenerContainer">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="messageListeners">
<map>
<entry key-ref="messageListener">
<bean class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="topic_name" />
</bean>
</entry>
</map>
</property>
<property name="taskExecutor" ref="redisTaskExecutor" />
<property name="subscriptionExecutor" ref="redisSubTaskExecutor" />
</bean>
<bean id="redisTaskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="threadNamePrefix" value="RedisListenerThread"></property>
<property name="corePoolSize" value="1" />
<property name="maxPoolSize" value="1" />
</bean>
<bean id="redisSubTaskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="threadNamePrefix" value="RedisSubscribeThread"></property>
<property name="corePoolSize" value="1" />
<property name="maxPoolSize" value="1" />
</bean>
<bean id="messageListener"
class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
<constructor-arg index="0">
<bean class="my.data.Receiver" />
</constructor-arg>
<constructor-arg index="1"><value>receive</value></constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)
有时我们在数据读取过程中会遇到以下生产问题。
org.springframework.data.redis.RedisConnectionFailureException: 意外的流结束。嵌套异常是 redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream。在 org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:47) 在 org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:36) 在 org.springframework .data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:37) 在 org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:37) 在 org.springframework.data.redis.connection.jedis.JedisAccessException.convertJedis (JedisConnection.java:182) 在 org。
我读过其他线程谈论使用单线程进行读/写。但是在我们的例子中很难使用单线程。同样根据 RedisTemplate 文档,它是线程安全的。问题是偶然出现的,我们无法在任何开发/测试/uat 环境中重现。因此无法找到相同的确切原因。我们做错了什么?
我们已经能够重现该问题,原因是 Redis 中的“超时 = 30”设置。