spring data redis消息监听器监听消息两次

时间:2015-12-03 04:01:58

标签: spring-data-redis

我正在尝试使用org.springframework.data.redis.listener.RedisMessageListenerContainer来监听redis pubsub频道。

我的MessageListener类看起来像这样

@Component
public class RedisMessageListener {

    @Autowired
    private ProcessAdapterImpl processAdapter;

    private static Logger logger = LoggerFactory.getLogger(RedisMessageListener.class);

    public void handleMessage(Serializable message, String channel) {
        logger.debug("handleMessage >>>>> Message received: " + message.toString() + " channel:" + channel);
        processAdapter.onEventReceived(message.toString());
    }
}

并在应用程序上下文中

<bean id="tbProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:conf/notifier-local.properties
        </value>
    </property>
</bean>
<bean id="jedisConnectionFactory"
    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="${redis.server}" />
    <property name="port" value="${redis.port}" />
    <property name="usePool" value="true" />
</bean>

<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
    p:connection-factory-ref="jedisConnectionFactory" />

<bean id="redisPublisher"
    class="com.vgroup.apps.notifier.event.publisher.RedisPublisherImpl" />

<bean id="messageListener"
    class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
    <constructor-arg>
        <bean class="com.vgroup.apps.notifier.event.listener.RedisMessageListener" />
    </constructor-arg>
</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="${redis.queue}" />
                </bean>
            </entry>
        </map>
    </property>
</bean>

redis发布商如下所示

@Component
public class RedisPublisherImpl implements IRedisPublisher {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ObjectMapper mapper;

    private static Logger logger = LoggerFactory.getLogger(RedisPublisherImpl.class);

    public void afterPropertiesSet() throws Exception {
    }

    @Override
    public void publish(Object event) {
        logger.info("Start executing publish object");
        try {
            String jsonString = mapper.writeValueAsString(event);
            redisTemplate.convertAndSend("notifications", jsonString);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        logger.info("End executing publish object");
    }
}

使用这种配置,出于一些奇怪的原因,我收到两次消息(有时甚至是3-4次),即使我的发布者只发送一次消息

在我的日志中我可以看到它是由2个不同的rediscontainer线程监听的消息

2015-12-03 09:13:12.152 [redisContainer-2] DEBUG c.v.a.n.e.l.RedisMessageListener - handleMessage >>>>> Message received: abc channel:notifications
2015-12-03 09:13:12.152 [redisContainer-2] DEBUG c.v.a.n.e.l.RedisMessageListener - handleMessage >>>>> Message received: def channel:notifications
2015-12-03 09:13:12.156 [redisContainer-3] DEBUG c.v.a.n.e.l.RedisMessageListener - handleMessage >>>>> Message received: abc channel:notifications
2015-12-03 09:13:12.157 [redisContainer-3] DEBUG c.v.a.n.e.l.RedisMessageListener - handleMessage >>>>> Message received: def channel:notifications

1 个答案:

答案 0 :(得分:3)

检查bean是否正在创建两次(即订阅发生两次)。我在web.xml中遇到过类似的问题。我有contextListener和DispatchServlet,它通过订阅两次初始化bean两次。