从Redis收到重复的消息

时间:2015-12-03 03:55:56

标签: java spring redis

我正在使用Spring + Redis,并遵循此文档 - > http://docs.spring.io/spring-data/data-redis/docs/1.2.0.RELEASE/reference/html/redis.html

我的网络可以获取redis-cli发送的消息,但它会收到两次相同的消息,这是意外的。

我的pom.xml

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.6.0</version>
</dependency>

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.5.0.RELEASE</version>
</dependency>

bean的配置

<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="localhost" p:port="6379"/>
    <!-- the default ConnectionFactory -->
    <redis:listener-container>
    <redis:listener ref="listener" method="handleMessage" topic="chat" />
    </redis:listener-container>

<bean id="listener" class="com.wisdom.common.queue.DefaultMessageDelegate"/>

DefaultMessageDelegate.java

@Service
public class DefaultMessageDelegate implements MessageDelegate {


    @Override
    public void handleMessage(String message) throws JsonParseException, JsonMappingException, IOException {

    System.out.println(message);

    }

}

当Web服务启动时,我可以看到有两个客户端订阅了该频道,然后handleMessage()接收到两个相同的消息,但我只想要一个。

id=4 addr=127.0.0.1:49487 fd=8 name= age=8750 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
id=5 addr=127.0.0.1:49510 fd=9 name= age=8636 idle=222 flags=O db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=56 oll=0 omem=0 events=rw cmd=monitor
id=24 addr=127.0.0.1:51069 fd=6 name= age=228 idle=228 flags=N db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
id=25 addr=127.0.0.1:51074 fd=7 name= age=222 idle=222 flags=N db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe

有什么想法解决这个问题吗?谢谢!

==============

更新了用于发送消息的代码:

    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxIdle(RedisSetting.MAX_IDLE);
    poolConfig.setMinIdle(RedisSetting.MIN_IDLE);
    poolConfig.setTestOnBorrow(RedisSetting.TEST_ON_BORROW);
    poolConfig.setNumTestsPerEvictionRun(RedisSetting.NUM_TESTS_PER_EVICTION_RUN);
    poolConfig.setTimeBetweenEvictionRunsMillis(RedisSetting.TIME_BETWEEN_EVICTION_RUNS_MILLIS);
    poolConfig.setMaxWaitMillis(RedisSetting.MAX_WAIT_MILLIS);



    JedisPool jedisPool = new JedisPool(poolConfig,RedisSetting.ADDRESS, RedisSetting.PORT, 10000, RedisSetting.PASSWORD);

   ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(10);
    newFixedThreadPool.submit(new Runnable() {

        @Override
        public void run() {


                Jedis jedis = jedisPool.getResource();
                try {
                   jedis.publish("DATA", data);
                } catch (Exception e) {
                   e.printStackTrace();
                } finally {
                   jedisPool.returnResource(jedis);
                }


        }
    });

常数:

public static final Integer MAX_IDLE = 5;

public static final Integer MIN_IDLE = 1;

public static final Boolean TEST_ON_BORROW = true;

public static final Integer NUM_TESTS_PER_EVICTION_RUN = 10;

public static final Integer TIME_BETWEEN_EVICTION_RUNS_MILLIS = 6000;

public static final Integer MAX_WAIT_MILLIS = 10000;

0 个答案:

没有答案