Java 从Redis接收重复消息
我正在使用Spring+Redis,并遵循此文档-> My web可以从redis cli发送消息,但它会收到两次相同的消息,这是意外的 我的pom.xmlJava 从Redis接收重复消息,java,spring,redis,Java,Spring,Redis,我正在使用Spring+Redis,并遵循此文档-> My web可以从redis cli发送消息,但它会收到两次相同的消息,这是意外的 我的pom.xml <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.0</version> </dependency
<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>
当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;
XML配置看起来不错。看起来您的
redisConnectionFactory
/侦听器容器已注册两次。客户端列表
输出是此行为的另一个指标。您是否要写下发布消息的代码?XML配置看起来很好。看起来您的redisConnectionFactory
/侦听器容器已注册两次。客户端列表
输出是此行为的另一个指标。您是否要写下发布消息的代码?
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;