|
|
@ -5,7 +5,6 @@ import com.bnyer.common.core.enums.EnumMessageStatus; |
|
|
import com.bnyer.common.core.utils.StringUtils; |
|
|
import com.bnyer.common.core.utils.StringUtils; |
|
|
import com.bnyer.common.rocketmq.config.RepeatConsumerConfig; |
|
|
import com.bnyer.common.rocketmq.config.RepeatConsumerConfig; |
|
|
import com.bnyer.common.rocketmq.constant.RocketMqConstant; |
|
|
import com.bnyer.common.rocketmq.constant.RocketMqConstant; |
|
|
import com.bnyer.common.rocketmq.domain.BaseMessage; |
|
|
|
|
|
import com.bnyer.common.rocketmq.domain.BnyerMessage; |
|
|
import com.bnyer.common.rocketmq.domain.BnyerMessage; |
|
|
import com.bnyer.common.rocketmq.domain.RepeatElement; |
|
|
import com.bnyer.common.rocketmq.domain.RepeatElement; |
|
|
import com.bnyer.common.rocketmq.persist.IPersist; |
|
|
import com.bnyer.common.rocketmq.persist.IPersist; |
|
|
@ -22,7 +21,7 @@ import org.springframework.data.redis.core.StringRedisTemplate; |
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
import javax.annotation.PostConstruct; |
|
|
import javax.annotation.Resource; |
|
|
import javax.annotation.Resource; |
|
|
import java.util.function.Function; |
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @author :WXC |
|
|
* @author :WXC |
|
|
@ -48,6 +47,10 @@ public abstract class EnhanceMessageHandler { |
|
|
|
|
|
|
|
|
private RepeatConsumerConfig repeatConsumerConfig; |
|
|
private RepeatConsumerConfig repeatConsumerConfig; |
|
|
|
|
|
|
|
|
|
|
|
private IPersist persist = null; |
|
|
|
|
|
|
|
|
|
|
|
private RepeatElement repeatElement = null; |
|
|
|
|
|
|
|
|
@Resource |
|
|
@Resource |
|
|
private RocketMQEnhanceTemplate rocketMQEnhanceTemplate; |
|
|
private RocketMQEnhanceTemplate rocketMQEnhanceTemplate; |
|
|
|
|
|
|
|
|
@ -58,6 +61,10 @@ public abstract class EnhanceMessageHandler { |
|
|
public void init(){ |
|
|
public void init(){ |
|
|
this.applicationName = env.getProperty("spring.application.name"); |
|
|
this.applicationName = env.getProperty("spring.application.name"); |
|
|
this.repeatConsumerConfig = RepeatConsumerConfig.enableRedisConfig(applicationName,stringRedisTemplate); |
|
|
this.repeatConsumerConfig = RepeatConsumerConfig.enableRedisConfig(applicationName,stringRedisTemplate); |
|
|
|
|
|
this.persist = this.repeatConsumerConfig.getPersist(); |
|
|
|
|
|
if (Objects.isNull(persist)){ |
|
|
|
|
|
throw new RuntimeException("消息去重持久层配置初始化失败..."); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -138,10 +145,6 @@ public abstract class EnhanceMessageHandler { |
|
|
sendReturnFailsMessage(message); |
|
|
sendReturnFailsMessage(message); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
IPersist persist = repeatConsumerConfig.getPersist(); |
|
|
|
|
|
RepeatElement repeatElement = new RepeatElement(repeatConsumerConfig.getApplicationName(), message.getConsumerGroupName(),message.getTopic() |
|
|
|
|
|
, message.getTag()==null ? "" : message.getTag() |
|
|
|
|
|
, repeatMessageKey(message)); |
|
|
|
|
|
//消费消息,末尾消费失败会删除消费记录,消费成功则更新消费状态
|
|
|
//消费消息,末尾消费失败会删除消费记录,消费成功则更新消费状态
|
|
|
try { |
|
|
try { |
|
|
long now = System.currentTimeMillis(); |
|
|
long now = System.currentTimeMillis(); |
|
|
@ -208,13 +211,17 @@ public abstract class EnhanceMessageHandler { |
|
|
* @return |
|
|
* @return |
|
|
*/ |
|
|
*/ |
|
|
protected Boolean handleMsgRepeat(final BnyerMessage message) { |
|
|
protected Boolean handleMsgRepeat(final BnyerMessage message) { |
|
|
|
|
|
//构建重复消息对象
|
|
|
|
|
|
this.repeatElement = new RepeatElement(this.repeatConsumerConfig.getApplicationName() |
|
|
|
|
|
, repeatMessageKey(message), message.getConsumerGroupName(),message.getTopic() |
|
|
|
|
|
, message.getTag()==null ? "" : message.getTag()); |
|
|
RepeatConsumerStrategy strategy = new NormalRepeatStrategy(); |
|
|
RepeatConsumerStrategy strategy = new NormalRepeatStrategy(); |
|
|
Function<BaseMessage, String> repeatKeyFunction = baseMessage -> repeatMessageKey(message); |
|
|
//Function<BnyerMessage, String> repeatKeyFunction = bnyerMessage -> repeatMessageKey(message);
|
|
|
if (repeatConsumerConfig.getRepeatStrategy() == RepeatConsumerConfig.REPEAT_STRATEGY_CONSUME_LATER) { |
|
|
if (repeatConsumerConfig.getRepeatStrategy() == RepeatConsumerConfig.REPEAT_STRATEGY_CONSUME_LATER) { |
|
|
strategy = new RedisRepeatStrategy(repeatConsumerConfig, repeatKeyFunction); |
|
|
strategy = new RedisRepeatStrategy(repeatConsumerConfig, repeatElement); |
|
|
} |
|
|
} |
|
|
//调用对应的去重策略
|
|
|
//调用对应的去重策略
|
|
|
return strategy.invoke(message); |
|
|
return strategy.invoke(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|