Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2204

how to acknownlege redis stream when using spring data redis

$
0
0

when I using this code to acknowlege redis(v6.2.5) stream items in java 11 spring boot project:

    @Override    public void onMessage(MapRecord<String, String, String> message) {        try {            log.debug("receive message from redis:" + JSON.toJSONString(body));            this.stringRedisTemplate.opsForStream().acknowledge(groupName, message);        } catch (Exception e) {            log.error("handle redis stream message error", e);        }    }

the stream element still exists in redis after running this code to acknowlege items, am I doing the wrong? what should I do to acknowlege the redis stream elements? This is the full code that consume element from redis streams:

package com.dolphin.soa.post.common.mq;import com.alibaba.fastjson.JSON;import com.dolphin.soa.post.service.IArticleService;import com.dolphin.soa.post.service.ISubRelationService;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.data.redis.connection.stream.MapRecord;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.stream.StreamListener;import org.springframework.stereotype.Component;import java.util.Map;/** * @author dolphin */@Component@Slf4jpublic class StreamMessageListener implements StreamListener<String, MapRecord<String, String, String>> {    @Value("${dolphin.redis.stream.group}")    private String groupName;    @Autowired    private StreamMessageHandler streamMessageHandler;    private final StringRedisTemplate stringRedisTemplate;    private final RedisTemplate<String, Object> articleRedisTemplate;    private final RedisTemplate<String, Long> redisLongTemplate;    private final ISubRelationService subRelationService;    private final IArticleService articleService;    public StreamMessageListener(StringRedisTemplate stringRedisTemplate,                                 @Qualifier("redisObjectTemplate") RedisTemplate<String, Object> articleRedisTemplate,                                 ISubRelationService subRelationService,                                 @Qualifier("redisLongTemplate") RedisTemplate<String, Long> redisLongTemplate,                                 IArticleService articleService) {        this.stringRedisTemplate = stringRedisTemplate;        this.articleRedisTemplate = articleRedisTemplate;        this.subRelationService = subRelationService;        this.redisLongTemplate = redisLongTemplate;        this.articleService = articleService;    }    @Override    public void onMessage(MapRecord<String, String, String> message) {        try {            Map<String, String> body = message.getValue();            log.debug("receive message from redis:" + JSON.toJSONString(body));            streamMessageHandler.handleArticle(body);            this.stringRedisTemplate.opsForStream().acknowledge(groupName, message);        } catch (Exception e) {            log.error("handle redis stream message error", e);        }    }}

Viewing all articles
Browse latest Browse all 2204

Trending Articles