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);
|
}
|
}
|