[agent] start rabbitmq , accomplish init queue procedure
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package io.wdd.wddcommon;
|
||||
package io.wdd.common;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.wddcommon.utils;
|
||||
package io.wdd.common.beans;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.wddcommon.utils;
|
||||
package io.wdd.common.beans;
|
||||
|
||||
public enum ResultStat {
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package io.wdd.common.beans.rabbitmq;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@SuperBuilder(toBuilder = true)
|
||||
public class OctopusMessage {
|
||||
|
||||
String uuid;
|
||||
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
LocalDateTime init_time;
|
||||
|
||||
OctopusMessageType type;
|
||||
|
||||
// server send message content
|
||||
Object content;
|
||||
|
||||
// agent reply message content
|
||||
Object result;
|
||||
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
LocalDateTime ac_time;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.wdd.common.beans.rabbitmq;
|
||||
|
||||
public enum OctopusMessageType {
|
||||
|
||||
// agent initialization
|
||||
INIT,
|
||||
|
||||
// important things agent should do with itself
|
||||
AGENT,
|
||||
|
||||
// common shell or order execution
|
||||
EXECUTOR,
|
||||
|
||||
// update or report agent status
|
||||
STATUS
|
||||
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package io.wdd.common.handler;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import io.wdd.common.beans.R;
|
||||
import io.wdd.common.beans.ResultStat;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLIntegrityConstraintViolationException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 自定义 全局异常处理类
|
||||
* 全局处理响应数据的全局处理类,继承ResponseBodyAdvice接口重写其中的方法,
|
||||
* (带有@RequestMapping注解的方法上抛出的异常都会拦截),在此统一处理并统一返回数据格式
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
@Slf4j(topic = "Global Exception")
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@Autowired
|
||||
private MyMessageSource messageSource;
|
||||
|
||||
/**
|
||||
* 全局异常拦截方法
|
||||
* 这里拦截此异常,将异常中的信息提取返回有效信息!
|
||||
*
|
||||
* @param e SpringBoot参数校验(valid)过程中,检验失败会产生此异常,在此处拦截
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public R<Object> MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
|
||||
|
||||
// 从异常对象中拿到ObjectError对象,获取所有的错误信息
|
||||
List<ObjectError> objectError = e.getBindingResult().getAllErrors();
|
||||
|
||||
// 然后提取错误提示信息进行返回
|
||||
HashMap<Integer, String> errorMap = new HashMap<>();
|
||||
objectError.forEach(objectError1 -> errorMap.put(objectError.indexOf(objectError1), objectError1.getDefaultMessage()));
|
||||
|
||||
// 使用标准化返回体返回数据
|
||||
return R.resetResult(ResultStat.VALIDATE_FAILED.getCode(), ResultStat.VALIDATE_FAILED.getDescription(), errorMap);
|
||||
//return errorMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param exception 参数类型错误,拦截器
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
|
||||
public Object methodArgumentNotValidException(MethodArgumentTypeMismatchException exception) {
|
||||
//按需重新封装需要返回的错误信息
|
||||
Map<String, String> invalidMap = new LinkedHashMap(99);
|
||||
//解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息
|
||||
invalidMap.put(exception.getParameter().getParameterName(), ResultStat.PARAM_ERROR.getDescription());
|
||||
R<Map<String, String>> vo = new R<>();
|
||||
|
||||
vo.setCode(ResultStat.PARAM_ERROR.getCode());
|
||||
vo.setMsg(getLocaleMsg(ResultStat.PARAM_ERROR.getDescription()));
|
||||
vo.setData(invalidMap);
|
||||
|
||||
log.debug(exception.getMessage(), exception);
|
||||
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截数据库异常
|
||||
*
|
||||
* @param exception 数据库异常
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value = {SQLException.class, DuplicateKeyException.class})
|
||||
public R<String> sqlExceptionHandler(SQLException exception) {
|
||||
|
||||
R<String> vo = new R<>();
|
||||
|
||||
vo.setCode(ResultStat.PARAM_ERROR.getCode());
|
||||
vo.setMsg(getLocaleMsg(ResultStat.PARAM_ERROR.getDescription()));
|
||||
if (exception instanceof SQLIntegrityConstraintViolationException) {
|
||||
vo.setData("Data already exsit ! 操作失败!");
|
||||
} else {
|
||||
vo.setData("数据库异常,操作失败!");
|
||||
}
|
||||
|
||||
log.debug(exception.getMessage(), exception);
|
||||
|
||||
return vo;
|
||||
}
|
||||
|
||||
@ExceptionHandler(MyRuntimeException.class)
|
||||
public R<Object> interceptMyRuntimeE(MyRuntimeException exception) {
|
||||
|
||||
R<Object> R = new R<>();
|
||||
ResultStat status = exception.getStatus();
|
||||
if (status != null) {
|
||||
R.setMsg(getLocaleMsg(exception.getMessage(), exception.getParams()));
|
||||
R.setCode(status.getCode());
|
||||
R.setData(exception.getData());
|
||||
} else {
|
||||
R.setCode(ResultStat.FAILED.getCode());
|
||||
R.setMsg(getLocaleMsg(exception.getMessage(), exception.getParams()));
|
||||
R.setData(null);
|
||||
}
|
||||
return R;
|
||||
}
|
||||
// /**
|
||||
// *
|
||||
// * 统一Spring Security的认证错误
|
||||
// * */
|
||||
// @ExceptionHandler(value = BadCredentialsException.class)
|
||||
// public R<Object> badCredentialsException(BadCredentialsException exception){
|
||||
//
|
||||
// log.error(exception.getDescription());
|
||||
//
|
||||
// return R.failed(ResultStat.USER_AUTH_FAILED);
|
||||
// }
|
||||
|
||||
/**
|
||||
* validate 验证错误handle
|
||||
*/
|
||||
@ExceptionHandler(value = BindException.class)
|
||||
public Object bindExceptionHandler(BindException exception) {
|
||||
//按需重新封装需要返回的错误信息
|
||||
Map<String, String> invalidMap = Maps.newLinkedHashMap();
|
||||
|
||||
//解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息
|
||||
if (exception != null) {
|
||||
List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
|
||||
fieldErrors.stream().sorted(Comparator.comparing(FieldError::getCode)).forEach(error -> {
|
||||
String defaultMessage = error.getDefaultMessage();
|
||||
String finalMessage = defaultMessage;
|
||||
if (defaultMessage != null && defaultMessage.startsWith("{") && defaultMessage.endsWith("}")) {
|
||||
finalMessage = messageSource.getMessage(defaultMessage.substring(1, defaultMessage.length() - 1));
|
||||
}
|
||||
|
||||
if (StringUtils.isNotEmpty(invalidMap.get(error.getField()))) {
|
||||
invalidMap.put(error.getField(),
|
||||
invalidMap.get(error.getField()) + "," + finalMessage);
|
||||
} else {
|
||||
invalidMap.put(error.getField(), finalMessage);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
R<Map<String, String>> vo = new R<>();
|
||||
vo.setCode(ResultStat.VALIDATE_FAILED.getCode());
|
||||
vo.setMsg(getLocaleMsg(ResultStat.VALIDATE_FAILED.getDescription()));
|
||||
vo.setData(invalidMap);
|
||||
|
||||
log.debug(exception.getMessage(), exception);
|
||||
|
||||
return vo;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认异常统一处理 Exception
|
||||
*/
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
public Object exceptionHandel(Exception e) {
|
||||
return getResponseVO(e, getLocaleMsg("系统错误,请联系管理员"));
|
||||
}
|
||||
|
||||
private Object getResponseVO(Exception e, String msg) {
|
||||
R<String> R = new R<>();
|
||||
R.setCode(ResultStat.FAILED.getCode());
|
||||
R.setMsg(msg);
|
||||
R.setMsg(e.getMessage());
|
||||
log.error(e.getMessage(), e);
|
||||
return R;
|
||||
}
|
||||
|
||||
private String getLocaleMsg(String msgCode, Object... params) {
|
||||
return messageSource.getMessage(msgCode, params);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package io.wdd.common.handler;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Locale;
|
||||
|
||||
@Component
|
||||
public class MyMessageSource {
|
||||
|
||||
@Resource
|
||||
private MessageSource messageSource;
|
||||
|
||||
public MyMessageSource() {
|
||||
}
|
||||
|
||||
public String getMessage(String code, Object... params) {
|
||||
return this.getMessage(code, (Object[])null, params);
|
||||
}
|
||||
|
||||
public String getMessageIgnoreMissMatch(String code, Object... params) {
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
String message = this.messageSource.getMessage(code, (Object[])null, code, locale);
|
||||
return this.parse(message, true, params);
|
||||
}
|
||||
|
||||
public String getMessage(String code, Object[] args, Object... params) {
|
||||
return this.getMessage(code, args, code, params);
|
||||
}
|
||||
|
||||
public String getMessage(String code, Object[] args, String defaultMessage, Object... params) {
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
String message = this.messageSource.getMessage(code, args, defaultMessage, locale);
|
||||
return this.parse(message, false, params);
|
||||
}
|
||||
|
||||
private String parse(String s, boolean ingoreParamsMissMath, Object... params) {
|
||||
if (s == null) {
|
||||
return null;
|
||||
} else if (params == null) {
|
||||
return s;
|
||||
} else {
|
||||
String[] splits = s.split("\\{}", -1);
|
||||
if (splits.length != params.length + 1) {
|
||||
if (ingoreParamsMissMath) {
|
||||
return s;
|
||||
} else {
|
||||
throw new IllegalArgumentException("The number of parameters is inconsistent with the parameter value");
|
||||
}
|
||||
} else if (splits.length == 1) {
|
||||
return s;
|
||||
} else {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
for(int i = 0; i < splits.length; ++i) {
|
||||
String split = splits[i];
|
||||
stringBuilder.append(split);
|
||||
if (i < params.length) {
|
||||
stringBuilder.append(params[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package io.wdd.common.handler;
|
||||
|
||||
import io.wdd.common.beans.ResultStat;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MyRuntimeException extends RuntimeException {
|
||||
|
||||
private Object data;
|
||||
private ResultStat status;
|
||||
|
||||
private Object[] params;
|
||||
|
||||
public MyRuntimeException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public MyRuntimeException(String msg, Object... params) {
|
||||
super(msg);
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
public MyRuntimeException(ResultStat status, Object data, String msg, Object... params) {
|
||||
super(msg == null ? status.getDescription() : msg);
|
||||
this.data = data;
|
||||
this.status = status;
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
public MyRuntimeException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public MyRuntimeException(Throwable cause, String msg) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.wddcommon;
|
||||
package io.wdd.common;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
Reference in New Issue
Block a user