rabbitmq cunsumer

segment2.0
linrf 2 years ago
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,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
* <p/>
*
* @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);
}

@ -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,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
* <p/>
* 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;
}
}

@ -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…
Cancel
Save