2014-10-01 69 views
1

在我的应用程序使用多个消费者接收来自redis发行商的消息。但现在的问题是数据丢失和重复数据我的意思是多个消费者接收reeving相同的消息。我在redis中解决这个问题?并且还可以提供java中的示例,它是redis messaging的新功能。请帮助我。数据丢失的情况下,多个消费者的redis消息

这里是我的接收机

@Configuration 
@EnableScheduling 
public class ScheduledRecevierService { 

    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class); 
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); 

    @Bean 
    RedisConnectionFactory redisConnectionFactory() { 
     LOGGER.info("in redisConnectionFactory"); 
     JedisConnectionFactory redis = new JedisConnectionFactory(); 
     redis.setHostName("ipaddress"); 
     redis.setPort(6379); 
     return redis; 
    } 

    @Bean 
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) { 
     LOGGER.info("in template"); 
     return new StringRedisTemplate(connectionFactory); 
    } 

    @Scheduled(fixedRate = 1000) 
    public void getScheduledMessage() { 

     StringRedisTemplate template = template(redisConnectionFactory()); 
     System.out.println("The time is now " + dateFormat.format(new Date())); 
     LOGGER.info("Sending messages..."); 

     String message = template.opsForList().leftPop("point-to-point-test"); // latch.await(); 
     // template.convertAndSend("chat", "Hello from Redis! count: " + i); 
     LOGGER.info("Got message " + message + " from chat1 channel"); // 
    } 

} 

我运行在多种消费instances.My队列这个应用程序的“点至点测试”有1000条短信是我观察到在多个服务器日志阅读相同的消息。

我们可以使用java在redis中实现点对点协议通信吗?

redis中的RPOPLPUSH命令解决了这个问题?如果是在java中发布一些示例。

从快速几天正在努力修复Redis的消息这些问题,请帮我

+0

尝试显示自己的调查。 – 2014-10-01 07:52:13

回答

0

使用Redis的交易,以确保您的所有指令顺序执行http://redis.io/topics/transactions

使用Jedis作为Java客户端库,请参阅其对交易使用的测试https://github.com/xetorthio/jedis/blob/master/src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java

+0

我的问题是多个消费者只在单一的情况下工作正常。但是,如果我使用多个消费者一些消息丢失,同一消息正在阅读的多个消费者? – user4097819 2014-10-01 08:43:04

+0

您是使用任何客户端库还是编写了执行redis命令的代码?使用Jedis编写消费者,然后看看你是否仍然面临问题 – 2014-10-01 09:02:38

+0

因为redis中的这些命令是原子的,所以当消费者执行LPOP时,它将从列表中删除一个项目,并且其他消费者不会获得该项目。你不应该看到任何重复。尝试只用jedis编写用户,不要使用弹簧数据并查看。 – 2014-10-01 09:19:51

相关问题