刘光辉
5 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package jnpf.dmsService.search;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
 
import java.time.Duration;
 
@Component
@RequiredArgsConstructor
public class DmsSearchIndexQueue {
    private final StringRedisTemplate redisTemplate;
    private final ObjectMapper objectMapper;
    private final DmsSearchProperties properties;
 
    public void push(DmsSearchIndexTask task) {
        try {
            redisTemplate.opsForList().leftPush(properties.getRedisQueueKey(),
                    objectMapper.writeValueAsString(task));
        } catch (JsonProcessingException exception) {
            throw new IllegalArgumentException("全文检索任务序列化失败", exception);
        }
    }
 
    public DmsSearchIndexTask take() throws JsonProcessingException {
        String value = redisTemplate.opsForList().rightPop(properties.getRedisQueueKey(),
                Duration.ofSeconds(Math.max(1, properties.getRedisPollTimeoutSeconds())));
        return value == null ? null : objectMapper.readValue(value, DmsSearchIndexTask.class);
    }
}