rabbitmq cunsumer
parent
ca342f8153
commit
1919a573cb
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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 命令抽象类
|
||||
* <p/>
|
||||
* 把接收消息的类型封装成一个命令 并且交给指定的接收者出处理
|
||||
* 方便扩展每个命令的处理
|
||||
* @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);
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue