优化
This commit is contained in:
+11
-6
@@ -1,6 +1,7 @@
|
||||
package com.baiyue.lastleg.wallet;
|
||||
|
||||
import com.baiyue.common.event.EventEnvelope;
|
||||
import com.baiyue.common.event.DeliveryAck;
|
||||
import com.baiyue.common.event.EventEnvelopeValidator;
|
||||
import com.baiyue.common.event.EventPayloadCanonicalizer;
|
||||
import com.baiyue.common.event.EventPayloadHash;
|
||||
@@ -26,23 +27,27 @@ public class WalletCommandCompletedConsumer {
|
||||
private final ObjectMapper objectMapper;
|
||||
private final List<WalletResultHandler> handlers;
|
||||
|
||||
@Transactional
|
||||
public int consume(EventEnvelope envelope) {
|
||||
@Transactional(isolation = org.springframework.transaction.annotation.Isolation.READ_COMMITTED)
|
||||
public DeliveryAck consume(EventEnvelope envelope) {
|
||||
WalletCommandCompletedV1 result = validate(envelope);
|
||||
List<String> existing = jdbc.queryForList("SELECT payload_hash FROM ll_wallet_event_inbox WHERE event_id=?", String.class, envelope.eventId());
|
||||
List<String> existing = jdbc.queryForList("SELECT payload_hash FROM ll_wallet_event_inbox WHERE event_id=? AND status='PROCESSED'", String.class, envelope.eventId());
|
||||
if (!existing.isEmpty()) {
|
||||
if (!existing.get(0).equals(envelope.payloadHash())) throw bad("同一资金结果事件ID对应不同载荷");
|
||||
return 0;
|
||||
return DeliveryAck.of(envelope, DeliveryAck.Outcome.DUPLICATE);
|
||||
}
|
||||
try {
|
||||
jdbc.update("INSERT INTO ll_wallet_event_inbox(event_id,command_event_id,payload_hash,payload_json,status,received_at) VALUES(?,?,?,CAST(? AS JSON),'RECEIVED',NOW(3))",
|
||||
envelope.eventId(), result.commandEventId(), envelope.payloadHash(), json(envelope));
|
||||
} catch (DuplicateKeyException exception) { return 0; }
|
||||
} catch (DuplicateKeyException exception) {
|
||||
List<String> raced = jdbc.queryForList("SELECT payload_hash FROM ll_wallet_event_inbox WHERE event_id=? AND status='PROCESSED'", String.class, envelope.eventId());
|
||||
if (raced.size() != 1 || !raced.get(0).equals(envelope.payloadHash())) throw bad("资金结果事件身份冲突或尚未完成");
|
||||
return DeliveryAck.of(envelope, DeliveryAck.Outcome.DUPLICATE);
|
||||
}
|
||||
WalletResultHandler handler = handlers.stream().filter(value -> value.supports(result.businessType())).findFirst()
|
||||
.orElseThrow(() -> bad("没有可处理该资金结果的业务模块"));
|
||||
handler.handle(result);
|
||||
jdbc.update("UPDATE ll_wallet_event_inbox SET status='PROCESSED',processed_at=NOW(3) WHERE event_id=?", envelope.eventId());
|
||||
return 1;
|
||||
return DeliveryAck.of(envelope, DeliveryAck.Outcome.APPLIED);
|
||||
}
|
||||
|
||||
private WalletCommandCompletedV1 validate(EventEnvelope envelope) {
|
||||
|
||||
Reference in New Issue
Block a user