[英]Redis Streams one message per consumer with Java
I'am trying to implement a java application with redis streams where every consomer consumes exactly one message.我正在尝试使用 redis 流实现 java 应用程序,其中每个消费者只消费一条消息。 Like a pipeline/queue where every consumer takes exactly one message, processes it and after finishing the consumer takes the next message which was not processed so far in the stream.就像管道/队列一样,每个消费者只接收一条消息,对其进行处理,并在完成后消费者接收到 stream 中迄今为止尚未处理的下一条消息。 What works is that every message is consumed by exactly one consumer (with xreadgroup).有效的是每条消息都由一个消费者(使用 xreadgroup)消费。
I started with this tutorial from redislabs我从 redislabs 开始阅读本教程
The code:编码:
RedisClient redisClient = RedisClient.create("redis://pw@host:port");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisCommands<String, String> syncCommands = connection.sync();
try {
syncCommands.xgroupCreate(XReadArgs.StreamOffset.from(STREAM_KEY, "0-0"), ID_READ_GROUP);
} catch (RedisBusyException redisBusyException) {
System.out.println(String.format("\t Group '%s' already exists", ID_READ_GROUP));
}
System.out.println("Waiting for new messages ");
while (true) {
List<StreamMessage<String, String>> messages = syncCommands.xreadgroup(
Consumer.from(ID_READ_GROUP, ID_WORKER), ReadArgs.StreamOffset.lastConsumed(STREAM_KEY));
if (!messages.isEmpty()) {
System.out.println(messages.size()); //
for (StreamMessage<String, String> message : messages) {
System.out.println(message.getId());
Thread.sleep(5000);
syncCommands.xack(STREAM_KEY, ID_READ_GROUP, message.getId());
}
}
}
My current problem is that a consumer takes more that one message from the queue and in some situations the other consumers are waiting and one consumer is processing 10 messages at once.我当前的问题是消费者从队列中获取多于一条消息,在某些情况下,其他消费者正在等待,而一个消费者一次处理 10 条消息。
Thanks in advance!提前致谢!
Notice that XREADGROUP can get COUNT
argument.请注意, XREADGROUP可以获得COUNT
参数。
See the JavaDoc how to do it in Lettuce
xreadgroup , by passing XReadArgs.通过传递 XReadArgs,查看 JavaDoc 如何在Lettuce
xreadgroup中执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.