diff --git a/docus-api-common/src/main/java/com/docus/server/common/consumer/bean/HandleResult.java b/docus-api-common/src/main/java/com/docus/server/common/consumer/bean/HandleResult.java new file mode 100644 index 0000000..10ad976 --- /dev/null +++ b/docus-api-common/src/main/java/com/docus/server/common/consumer/bean/HandleResult.java @@ -0,0 +1,138 @@ +package com.docus.server.common.consumer.bean; + +import com.docus.server.common.consumer.type.HandleSignalEnum; +import org.springframework.util.StringUtils; + +/** + * Copyright © 2018 五月工作室. All rights reserved. + * + * @Package com.docus.taskdistribute.common.send + * @ClassName SendService + * @Description 处理结果 + * @Author Amos + * @Modifier + * @Date 2019/7/1 15:11 + * @Version 1.0 + **/ +public class HandleResult { + /** + * 是否回调 + */ + private boolean callback; + /** + * 类型 + */ + private Integer type; + /** + * 内容 + */ + private String content; + /** + * 消息 + */ + private String msg; + /** + * 是否处理成功 + */ + private boolean result; + + public HandleResult(CallBack callBack) { + this.callback = callBack.callback; + this.content = callBack.content; + this.type = callBack.type; + this.msg = callBack.msg; + this.result = callBack.callback; + } + + public boolean isCallback() { + return this.callback; + } + + public void setCallback(boolean callback) { + this.callback = callback; + } + + public Integer getType() { + if (StringUtils.isEmpty(this.type)) { + this.type = HandleSignalEnum.SINGAL_CALLBACK.getCode(); + } + return this.type; + } + + public void setType(Integer type) { + this.type = type; + } + + public String getContent() { + return this.content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getMsg() { + return this.msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public boolean getResult() { + return this.result; + } + + public void setResult(boolean result) { + this.result = result; + } + + public static class CallBack { + /** + * 是否回调 + */ + private boolean callback = true; + /** + * 回调请求类型 + */ + private Integer type; + /** + * 回调内容 + */ + private String content; + + private String msg; + /** + * 是否处理成功 + */ + private boolean result; + + public CallBack(boolean result) { + this.result = result; + } + + public CallBack type(Integer type) { + this.type = type; + return this; + } + + public CallBack content(String content) { + this.content = content; + return this; + } + + public CallBack callback(boolean callback) { + this.callback = callback; + return this; + } + + public CallBack msg(String msg) { + this.msg = msg; + return this; + } + + public HandleResult builder() { + return new HandleResult(this); + } + } +} diff --git a/docus-api-common/src/main/java/com/docus/server/common/consumer/command/AbstractCommand.java b/docus-api-common/src/main/java/com/docus/server/common/consumer/command/AbstractCommand.java new file mode 100644 index 0000000..5ad85f2 --- /dev/null +++ b/docus-api-common/src/main/java/com/docus/server/common/consumer/command/AbstractCommand.java @@ -0,0 +1,30 @@ +package com.docus.server.common.consumer.command; + + +import com.docus.server.common.consumer.bean.HandleResult; +import com.docus.server.common.message.MessageData; + +/** + * Copyright © 2018 五月工作室. All rights reserved. + * + * @Package com.docus.taskdistribute.common.send + * @ClassName SendService + * @Description 命令抽象类 + *

+ * 把接收消息的类型封装成一个命令 并且交给指定的接收者出处理 + * 方便扩展每个命令的处理 + * @Author Amos + * @Modifier + * @Date 2019/7/1 15:11 + * @Version 1.0 + **/ +public abstract class AbstractCommand { + + /** + * 每个命令都必须被处理 + * + * @param messageData + * @return + */ + public abstract HandleResult execute(MessageData messageData); +} diff --git a/docus-api-common/src/main/java/com/docus/server/common/consumer/command/CallBackCommand.java b/docus-api-common/src/main/java/com/docus/server/common/consumer/command/CallBackCommand.java new file mode 100644 index 0000000..bf10561 --- /dev/null +++ b/docus-api-common/src/main/java/com/docus/server/common/consumer/command/CallBackCommand.java @@ -0,0 +1,47 @@ +package com.docus.server.common.consumer.command; + +import com.docus.server.common.consumer.bean.HandleResult; +import com.docus.server.common.consumer.handle.AbstractHandler; +import com.docus.server.common.message.MessageData; +import org.springframework.stereotype.Component; + + +/** + * Copyright © 2018 五月工作室. All rights reserved. + * + * @Package com.docus.taskdistribute.common.send + * @ClassName SendService + * @Description 回调函数 消息处理 + * @Author Amos + * @Modifier + * @Date 2019/7/1 15:11 + * @Version 1.0 + **/ +@Component +public class CallBackCommand extends AbstractCommand { + /** + * 定义handler来进行命令处理 + */ + private AbstractHandler handler; + + public CallBackCommand(AbstractHandler handler) { + this.handler = handler; + } + + + public CallBackCommand init(AbstractHandler handler) { + this.handler = handler; + return this; + } + + /** + * 执行业务处理 + * + * @param unicomData + * @return + */ + @Override + public HandleResult execute(MessageData unicomData) { + return this.handler.handle(unicomData); + } +} diff --git a/docus-api-common/src/main/java/com/docus/server/common/consumer/entity/Inventory.java b/docus-api-common/src/main/java/com/docus/server/common/consumer/entity/Inventory.java new file mode 100644 index 0000000..45a339c --- /dev/null +++ b/docus-api-common/src/main/java/com/docus/server/common/consumer/entity/Inventory.java @@ -0,0 +1,21 @@ +package com.docus.server.common.consumer.entity; + +import lombok.Data; + +/** + * Copyright © 2018 五月工作室. All rights reserved. + * + * @Package com.docus.taskdistribute.consumer.entity + * @ClassName Inventory + * @Description TODO + * @Author Amos + * @Modifier + * @Date 2019/8/18 22:11 + * @Version 1.0 + **/ +@Data +public class Inventory { + private String id; + private Integer count; + +} diff --git a/docus-api-common/src/main/java/com/docus/server/common/consumer/handle/AbstractHandler.java b/docus-api-common/src/main/java/com/docus/server/common/consumer/handle/AbstractHandler.java new file mode 100644 index 0000000..1be85db --- /dev/null +++ b/docus-api-common/src/main/java/com/docus/server/common/consumer/handle/AbstractHandler.java @@ -0,0 +1,28 @@ +package com.docus.server.common.consumer.handle; + + +import com.docus.server.common.consumer.bean.HandleResult; +import com.docus.server.common.message.MessageData; + +/** + * Copyright © 2018 五月工作室. All rights reserved. + * + * @Package com.docus.taskdistribute.common.send + * @ClassName SendService + * @Description 处理消息 抽象类 + *

+ * 业务处理需要继承该基类,实现处理的方法 + * @Author Amos + * @Modifier + * @Date 2019/7/1 15:11 + * @Version 1.0 + **/ +public abstract class AbstractHandler { + /** + * 自定义处理 + * + * @param data + * @return + */ + public abstract HandleResult handle(MessageData data); +} diff --git a/docus-api-common/src/main/java/com/docus/server/common/consumer/handle/CallBackHandler.java b/docus-api-common/src/main/java/com/docus/server/common/consumer/handle/CallBackHandler.java new file mode 100644 index 0000000..6672e1e --- /dev/null +++ b/docus-api-common/src/main/java/com/docus/server/common/consumer/handle/CallBackHandler.java @@ -0,0 +1,33 @@ +package com.docus.server.common.consumer.handle; + + +import com.docus.server.common.consumer.bean.HandleResult; +import com.docus.server.common.message.MessageData; +import org.springframework.stereotype.Component; + +/** + * Copyright © 2018 五月工作室. All rights reserved. + * + * @Package com.docus.taskdistribute.common.send + * @ClassName SendService + * @Description 回调消息处理者 + * @Author Amos + * @Modifier + * @Date 2019/7/1 15:11 + * @Version 1.0 + **/ +@Component +public class CallBackHandler extends AbstractHandler { + + /** + * 修改消息 + * + * @param data + * @return + */ + @Override + public HandleResult handle(MessageData data) { + // TODO 自定义业务逻辑处理 + return new HandleResult.CallBack(true).callback(false).msg("处理成功").builder(); + } +} diff --git a/docus-api-common/src/main/java/com/docus/server/common/consumer/handle/Invoker.java b/docus-api-common/src/main/java/com/docus/server/common/consumer/handle/Invoker.java new file mode 100644 index 0000000..4b1c4bb --- /dev/null +++ b/docus-api-common/src/main/java/com/docus/server/common/consumer/handle/Invoker.java @@ -0,0 +1,42 @@ +package com.docus.server.common.consumer.handle; + +import com.docus.server.common.consumer.bean.HandleResult; +import com.docus.server.common.consumer.command.AbstractCommand; +import com.docus.server.common.message.MessageData; +import org.springframework.stereotype.Component; + + +/** + * Copyright © 2018 五月工作室. All rights reserved. + * + * @Package com.docus.taskdistribute.common.send + * @ClassName SendService + * @Description 命令的调用者 + * @Author Amos + * @Modifier + * @Date 2019/7/1 15:11 + * @Version 1.0 + **/ +@Component +public class Invoker { + + private AbstractCommand command; + + public void setCommand(AbstractCommand command) { + this.command = command; + } + + public HandleResult execute(MessageData messageData) { + return this.command.execute(messageData); + } + + public static void main(String[] args) { + Invoker invoker = new Invoker(); + invoker.setCommand(new AbstractCommand() { + @Override + public HandleResult execute(MessageData messageData) { + return null; + } + }); + } +} diff --git a/docus-api-common/src/main/java/com/docus/server/common/consumer/receiver/AbstractReceiver.java b/docus-api-common/src/main/java/com/docus/server/common/consumer/receiver/AbstractReceiver.java new file mode 100644 index 0000000..a4f5442 --- /dev/null +++ b/docus-api-common/src/main/java/com/docus/server/common/consumer/receiver/AbstractReceiver.java @@ -0,0 +1,90 @@ +package com.docus.server.common.consumer.receiver; + +import com.docus.server.common.bean.Result; +import com.docus.server.common.consumer.bean.HandleResult; +import com.docus.server.common.consumer.service.Receiver; +import com.docus.server.common.message.MessageData; +import com.docus.server.common.type.ResultEnum; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Copyright © 2018 五月工作室. All rights reserved. + * + * @Package com.docus.taskdistribute.common.send + * @ClassName SendService + * @Description 定义通用消息接收处理基类 + * @Author Amos + * @Modifier + * @Date 2019/7/1 15:11 + * @Version 1.0 + **/ +public abstract class AbstractReceiver implements Receiver { + + private static final Logger logger = LoggerFactory.getLogger(AbstractReceiver.class); + + /** + * 用户自定义消息处理 + * + * @param messageData + * @return + */ + public abstract HandleResult exec(MessageData messageData) throws Exception; + + /** + * 用户自定义验证 + * + * @param messageData + * @return + */ + public abstract Result validate(MessageData messageData); + + /** + * 成功处理 + * + * @param messageData + * @return + */ + public abstract HandleResult handleSuccess(MessageData messageData); + + /** + * 失败处理 + * + * @param messageData + * @return + */ + public abstract HandleResult handleFail(MessageData messageData); + + /** + * 处理 + * + * @param messageData + * @return + */ + @Override + public final HandleResult handleMessage(MessageData messageData) { + logger.info(this.getClass().getSimpleName() + "-->handleMessage()参数 unicomData:{}", messageData.toString()); + HandleResult handleResult = null; + try { + // 如果自定义验证不通过 + Result result = this.validate(messageData); + if (!ResultEnum.success().equals(result.getCode())) { + // 如果验证失败 进行失败处理 + + return this.handleFail(messageData); + } + // 根据自行处理的返回结果 + handleResult = this.exec(messageData); + + // 执行成功处理的逻辑 + handleResult = this.handleSuccess(messageData); + } catch (Exception e) { + e.printStackTrace(); + messageData.setContent(e.getMessage()); + return this.handleFail(messageData); + } + return handleResult; + } + +} diff --git a/docus-api-common/src/main/java/com/docus/server/common/consumer/receiver/DataReceiver.java b/docus-api-common/src/main/java/com/docus/server/common/consumer/receiver/DataReceiver.java new file mode 100644 index 0000000..5dba055 --- /dev/null +++ b/docus-api-common/src/main/java/com/docus/server/common/consumer/receiver/DataReceiver.java @@ -0,0 +1,95 @@ +package com.docus.server.common.consumer.receiver; + +import com.docus.taskdistribute.common.bean.Result; +import com.docus.taskdistribute.common.message.MessageData; +import com.docus.taskdistribute.common.util.ResultWapper; +import com.docus.taskdistribute.consumer.bean.HandleResult; +import com.docus.taskdistribute.consumer.type.HandleSignalEnum; +import com.docus.taskdistribute.consumer.type.MessageTypeEnum; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +/** + * Copyright © 2018 五月工作室. All rights reserved. + * + * @Package com.docus.taskdistribute.common.send + * @ClassName SendService + * @Description 数据消息的接收处理 + *

+ * 这里主要是业务方集成AbstractReceiver 实现自定义的数据处理 + * 一般对于内容型的消息都需要返回给对方我方到底是否成功消费,还是系统或业务出现了异常 + * @Author Amos + * @Modifier + * @Date 2019/7/1 15:11 + * @Version 1.0 + **/ +@Component +public class DataReceiver extends AbstractReceiver { + private static final Logger logger = LoggerFactory.getLogger(AbstractReceiver.class); + + /** + * 对内容消息进行处理 + * + * @param messageData + * @return + * @throws Exception + */ + @Override + public HandleResult exec(MessageData messageData) throws Exception { + HandleResult handleResult; + + try { + // TODO 业务方的处理 并且添加自定义的日志记录 + + handleResult = new HandleResult.CallBack(Boolean.TRUE).type(MessageTypeEnum.SEND_TYPE_CALLBACK.getCode()).builder(); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + e.printStackTrace(); + } + handleResult = new HandleResult.CallBack(Boolean.FALSE).type(MessageTypeEnum.SEND_TYPE_CALLBACK.getCode()).msg("接收处理错误:" + e.getMessage()).builder(); + } + + return handleResult; + } + + /** + * 是否需要进行好友验证 + * + * @return + */ + @Override + public Result validate(MessageData messageData) { + // TODO 实现用户自定义校验逻辑 + return ResultWapper.success(); + } + + /** + * 成功处理 + * 内容需要回调 + * + * @param messageData + * @return + */ + @Override + public HandleResult handleSuccess(MessageData messageData) { + // TODO 自定义成功的业务处理 + HandleResult handleResult = new HandleResult.CallBack(true).type(HandleSignalEnum.SINGAL_CALLBACK.getCode()).builder(); + return handleResult; + } + + + /** + * 失败处理 + * + * @param messageData + * @return + */ + @Override + public HandleResult handleFail(MessageData messageData) { + // TODO 自定义失败的业务处理 + HandleResult handleResult = new HandleResult.CallBack(false).type(HandleSignalEnum.SINGAL_CALLBACK.getCode()).builder(); + return handleResult; + } + +} diff --git a/docus-api-common/src/main/java/com/docus/server/common/consumer/receiver/OperReceiver.java b/docus-api-common/src/main/java/com/docus/server/common/consumer/receiver/OperReceiver.java new file mode 100644 index 0000000..2cea013 --- /dev/null +++ b/docus-api-common/src/main/java/com/docus/server/common/consumer/receiver/OperReceiver.java @@ -0,0 +1,78 @@ +package com.docus.server.common.consumer.receiver; + +import com.docus.server.common.bean.Result; +import com.docus.server.common.consumer.bean.HandleResult; +import com.docus.server.common.consumer.command.CallBackCommand; +import com.docus.server.common.consumer.handle.AbstractHandler; +import com.docus.server.common.consumer.handle.Invoker; +import com.docus.server.common.consumer.type.HandleSignalEnum; +import com.docus.server.common.consumer.type.MessageTypeEnum; +import com.docus.server.common.message.MessageData; +import com.docus.server.common.util.ResultWapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Component; + + +/** + * Copyright © 2018 五月工作室. All rights reserved. + * + * @Package com.docus.taskdistribute.common.send + * @ClassName SendService + * @Description 指令消息处理 + * @Author Amos + * @Modifier + * @Date 2019/7/1 15:11 + * @Version 1.0 + **/ +@Component +public class OperReceiver extends AbstractReceiver { + + @Autowired + Invoker invoker; + + @Autowired + CallBackCommand callBackCommand; + + @Autowired + @Qualifier("callBackHandler") + AbstractHandler callbackHandler; + + /** + * 针对不同的指令进行不同的处理 + * + * @param messageData + * @return + */ + @Override + public HandleResult exec(MessageData messageData) { + + // 如果是回调指令 则交给回调处理者来处理 + if (MessageTypeEnum.SEND_TYPE_CALLBACK.getCode().equals(messageData.getMsgType())) { + + this.invoker.setCommand(this.callBackCommand.init(this.callbackHandler)); + } + + return this.invoker.execute(messageData); + } + + /** + * 指令消息不需要经过验证 + * + * @return + */ + @Override + public Result validate(MessageData messageData) { + return ResultWapper.success(); + } + + @Override + public HandleResult handleSuccess(MessageData messageDat) { + return new HandleResult.CallBack(true).type(HandleSignalEnum.SINGAL_CALLBACK.getCode()).builder(); + } + + @Override + public HandleResult handleFail(MessageData messageDat) { + return new HandleResult.CallBack(true).type(HandleSignalEnum.SINGAL_CALLBACK.getCode()).builder(); + } +} diff --git a/docus-api-common/src/main/java/com/docus/server/common/consumer/service/Receiver.java b/docus-api-common/src/main/java/com/docus/server/common/consumer/service/Receiver.java new file mode 100644 index 0000000..0052696 --- /dev/null +++ b/docus-api-common/src/main/java/com/docus/server/common/consumer/service/Receiver.java @@ -0,0 +1,25 @@ +package com.docus.server.common.consumer.service; + +import com.docus.server.common.consumer.bean.HandleResult; +import com.docus.server.common.message.MessageData; + +/** + * Copyright © 2018 五月工作室. All rights reserved. + * + * @Package com.docus.taskdistribute.common.send + * @ClassName SendService + * @Description 对消息进行处理 + * @Author Amos + * @Modifier + * @Date 2019/7/1 15:11 + * @Version 1.0 + **/ +public interface Receiver { + /** + * 对消息进行处理 + * + * @param messageData + * @return + */ + HandleResult handleMessage(MessageData messageData); +} diff --git a/docus-api-common/src/main/java/com/docus/server/common/consumer/type/HandleSignalEnum.java b/docus-api-common/src/main/java/com/docus/server/common/consumer/type/HandleSignalEnum.java new file mode 100644 index 0000000..386fbe0 --- /dev/null +++ b/docus-api-common/src/main/java/com/docus/server/common/consumer/type/HandleSignalEnum.java @@ -0,0 +1,48 @@ +package com.docus.server.common.consumer.type; + + +/** + * Copyright © 2018 五月工作室. All rights reserved. + * + * @Package com.docus.taskdistribute.common.send + * @ClassName SendService + * @Description 消息处理信号枚举 + * @Author Amos + * @Modifier + * @Date 2019/7/1 15:11 + * @Version 1.0 + **/ +public enum HandleSignalEnum { + /** + * 回调 + */ + SINGAL_CALLBACK(0, "回调"), + /** + * 内容 + */ + SIGNAL_DATA(6, "内容"); + + private Integer code; + private String desc; + + private HandleSignalEnum(Integer code, String desc) { + this.code = code; + this.desc = desc; + } + + public Integer getCode() { + return this.code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getDesc() { + return this.desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } +} diff --git a/docus-api-common/src/main/java/com/docus/server/common/consumer/type/MessageTypeEnum.java b/docus-api-common/src/main/java/com/docus/server/common/consumer/type/MessageTypeEnum.java new file mode 100644 index 0000000..3a89dde --- /dev/null +++ b/docus-api-common/src/main/java/com/docus/server/common/consumer/type/MessageTypeEnum.java @@ -0,0 +1,59 @@ +package com.docus.server.common.consumer.type; + +/** + * Copyright © 2018 五月工作室. All rights reserved. + * + * @Package com.docus.taskdistribute.common.send + * @ClassName SendService + * @Description 消息的类型 + * @Author Amos + * @Modifier + * @Date 2019/7/1 15:11 + * @Version 1.0 + **/ +public enum MessageTypeEnum { + /** + * 发送内容 + */ + SEND_TYPE_CONTENT(1, "内容"), + /** + * 拒绝 + */ + SEND_TYPE_RESEND(-1, "拒绝"), + /** + * 回调 + */ + SEND_TYPE_CALLBACK(0, "回调"), + /** + * 同意 + */ + SEND_TYPE_AGREE(2, "同意"), + /** + * 异常 + */ + SEND_TYPE_ERROR(9, "异常"); + + private Integer code; + private String desc; + + private MessageTypeEnum(Integer code, String desc) { + this.code = code; + this.desc = desc; + } + + public Integer getCode() { + return this.code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getDesc() { + return this.desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } +}