From 43069fd4bbae4765f9bc8e3c3f6cd0c4d1ffb7aa Mon Sep 17 00:00:00 2001 From: yu199195 <549477611@qq.com> Date: Thu, 7 Nov 2019 16:51:00 +0800 Subject: [PATCH] to switch enum. --- .../hmily/common/enums/EventTypeEnum.java | 14 ++++++++++ .../handler/HmilyConsumerLogDataHandler.java | 26 ++++++++++++------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/hmily-common/src/main/java/org/dromara/hmily/common/enums/EventTypeEnum.java b/hmily-common/src/main/java/org/dromara/hmily/common/enums/EventTypeEnum.java index 6ff858e8..d2a5f8de 100644 --- a/hmily-common/src/main/java/org/dromara/hmily/common/enums/EventTypeEnum.java +++ b/hmily-common/src/main/java/org/dromara/hmily/common/enums/EventTypeEnum.java @@ -18,6 +18,9 @@ package org.dromara.hmily.common.enums; import lombok.Getter; import lombok.RequiredArgsConstructor; +import org.dromara.hmily.common.exception.HmilyRuntimeException; + +import java.util.Arrays; /** * The enum Coordinator action enum. @@ -52,4 +55,15 @@ public enum EventTypeEnum { private final String desc; + /** + * Build by code event type enum. + * + * @param code the code + * @return the event type enum + */ + public static EventTypeEnum buildByCode(int code) { + return Arrays.stream(EventTypeEnum.values()).filter(e -> e.code == code).findFirst() + .orElseThrow(() -> new HmilyRuntimeException("can not support this code!")); + } + } diff --git a/hmily-core/src/main/java/org/dromara/hmily/core/disruptor/handler/HmilyConsumerLogDataHandler.java b/hmily-core/src/main/java/org/dromara/hmily/core/disruptor/handler/HmilyConsumerLogDataHandler.java index 348b5c0d..7d3480b7 100644 --- a/hmily-core/src/main/java/org/dromara/hmily/core/disruptor/handler/HmilyConsumerLogDataHandler.java +++ b/hmily-core/src/main/java/org/dromara/hmily/core/disruptor/handler/HmilyConsumerLogDataHandler.java @@ -38,15 +38,23 @@ public class HmilyConsumerLogDataHandler extends AbstractDisruptorConsumerExecut public void executor(final HmilyTransactionEvent event) { String transId = event.getHmilyTransaction().getTransId(); executor.select(transId).execute(() -> { - if (event.getType() == EventTypeEnum.SAVE.getCode()) { - coordinatorService.save(event.getHmilyTransaction()); - } else if (event.getType() == EventTypeEnum.UPDATE_PARTICIPANT.getCode()) { - coordinatorService.updateParticipant(event.getHmilyTransaction()); - } else if (event.getType() == EventTypeEnum.UPDATE_STATUS.getCode()) { - final HmilyTransaction hmilyTransaction = event.getHmilyTransaction(); - coordinatorService.updateStatus(hmilyTransaction.getTransId(), hmilyTransaction.getStatus()); - } else if (event.getType() == EventTypeEnum.DELETE.getCode()) { - coordinatorService.remove(event.getHmilyTransaction().getTransId()); + EventTypeEnum eventTypeEnum = EventTypeEnum.buildByCode(event.getType()); + switch (eventTypeEnum) { + case SAVE: + coordinatorService.save(event.getHmilyTransaction()); + break; + case DELETE: + coordinatorService.remove(event.getHmilyTransaction().getTransId()); + break; + case UPDATE_STATUS: + final HmilyTransaction hmilyTransaction = event.getHmilyTransaction(); + coordinatorService.updateStatus(hmilyTransaction.getTransId(), hmilyTransaction.getStatus()); + break; + case UPDATE_PARTICIPANT: + coordinatorService.updateParticipant(event.getHmilyTransaction()); + break; + default: + break; } event.clear(); });