1

I am trying to use org.springframework.data.redis.listener.RedisMessageListenerContainer to listen on the redis pubsub channel.

my MessageListener class looks like this

@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());
    }
}

and in application context

<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 publisher looks like below

@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");
    }
}

with this configuration, for some strange reasons I am getting a message twice (some times even 3-4 times) even if it is sent by my publisher only once

in my logs I can see that it is message is being listened by 2 different rediscontainer threads

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
4

1 回答 1

4

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

于 2015-12-07T20:21:23.860 回答