Commit bd6ac76b authored by yanzg's avatar yanzg

Merge branches 'master' and 'xy' of http://192.168.0.204/yzg/yzg-util into xy

parents aff8222f db2a2769
......@@ -5,39 +5,56 @@ package com.yanzuoguang.util.exception;
*
* @author 颜佐光
*/
public class HttpCodeException extends RuntimeException {
private static final long serialVersionUID = -4625832188480820883L;
/**
* 错误码
*/
private String code = "99";
public class HttpCodeException extends RuntimeCodeException {
/**
* 包含的数据
* Constructs a new runtime exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this runtime exception's detail message.
*
* @param code the detail code (which is saved for later retrieval
* by the {@link #getCode()} method).
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
private Object target = null;
public HttpCodeException(String code, String message, Throwable cause) {
super(code, message, cause);
}
/**
* 获取错误码
* 构造函数
* throw new CodeException("01","该订单已过期",order);
* throw new CodeException("02","该订单未到使用时间",order);
*
* @return 返回的结果
* @param message  错误消息
* @param target  错误数据源,如订单数据
*/
public String getCode() {
return this.code;
public HttpCodeException(String message, Object target) {
super(message, target);
}
/**
* 来源数据
* 构造函数
* throw new CodeException("01","该订单已过期",order);
* throw new CodeException("02","该订单未到使用时间",order);
*
* @return
* @param message  错误消息
* @param target  错误数据源,如订单数据
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public Object getTarget() {
return target;
public HttpCodeException(String message, Object target, Throwable cause) {
super(message, target, cause);
}
/**
* 构造函数
* throw new CodeException("01","该订单已过期",order);
......@@ -45,11 +62,10 @@ public class HttpCodeException extends RuntimeException {
*
* @param code  错误码
* @param message  错误消息
* @param target  错误数据源,如订单数据
*/
public HttpCodeException(String code, String message) {
super(message);
this.code = code;
this.target = null;
public HttpCodeException(String code, String message, Object target) {
super(code, message, target);
}
/**
......@@ -66,8 +82,77 @@ public class HttpCodeException extends RuntimeException {
* unknown.)
*/
public HttpCodeException(String code, String message, Object target, Throwable cause) {
super(code, message, target, cause);
}
/**
* Constructs a new runtime exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this runtime exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public HttpCodeException(String message, Throwable cause) {
super(message, cause);
this.code = code;
this.target = target;
}
/**
* Constructs a new runtime exception with {@code null} as its
* detail message. The cause is not initialized, and may subsequently be
* initialized by a call to {@link #initCause}.
*/
public HttpCodeException() {
super();
}
/**
* Constructs a new runtime exception with the specified detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*
* @param code the detail code (which is saved for later retrieval
* by the {@link #getCode()} method).
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public HttpCodeException(String code, String message) {
super(code, message);
}
/**
* Constructs a new runtime exception with the specified detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public HttpCodeException(String message) {
super(message);
}
/**
* Constructs a new runtime exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this runtime exception's detail message.
*
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public HttpCodeException(Throwable cause) {
super(cause);
}
}
......@@ -1008,6 +1008,18 @@ public class StringHelper {
return getMD5Id(froms);
}
/**
* 获取组合编号
*
* @param args 需要组合的编号
* @return 将ID列表进行组合生成ID
*/
public static String getIdShort(String from, Object... args) {
String id = getId(args);
return from.replaceFirst(id + ":", "");
}
/**
* 获取组合编号
*
......
......@@ -22,6 +22,11 @@ public class BaseVo implements Serializable {
*/
@Override
public String toString() {
try {
return JsonHelper.serialize(this);
} catch (Exception ex) {
ex.printStackTrace();
return super.toString();
}
}
}
......@@ -63,11 +63,7 @@ public class MqConfigurable implements RabbitTemplate.ConfirmCallback, RabbitTem
try {
if (ack && correlationData != null
&& !StringHelper.isEmpty(correlationData.getId())) {
String toId = getId(correlationData.getId());
// 不是临时数据
if (toId.equals(correlationData.getId())) {
messageSendService.onSuccess(toId);
}
messageSendService.onSuccess(correlationData.getId());
} else if (!ack) {
System.out.println("丢失消息:" + ack + " msg:" + cause);
}
......@@ -94,15 +90,10 @@ public class MqConfigurable implements RabbitTemplate.ConfirmCallback, RabbitTem
String content = new String(message.getBody(), charset);
// 组成消息
MessageVo messageVo = new MessageVo(exchange, routingKey, content);
messageVo.setMessageId(getId(messageProperties.getMessageId()));
// 写入数据库
messageSendService.onError(messageVo);
} catch (Exception ex) {
Log.error(MqConfigurable.class, ex);
}
}
private String getId(String from) {
return from.replace("temp:", "");
}
}
......@@ -35,6 +35,8 @@ public class MessageLogDaoImpl extends BaseDaoImpl implements MessageLogDao, Ini
*/
@Override
protected void init() {
// 设置缓存3秒
cacheList.setClearSecond(3);
// 注册表结构
register(MessageLogVo.class);
}
......
......@@ -32,6 +32,8 @@ import java.util.List;
@Component
public class MessageSendServiceImpl implements MessageSendService {
public static final String TEMP_ID = "temp";
@Autowired
private MyRabbitTemplate rabbitTemplate;
......@@ -77,12 +79,11 @@ public class MessageSendServiceImpl implements MessageSendService {
@Override
public String send(MessageVo req) {
req.check();
// 获取消息临时Id
String finalMessageId = StringHelper.getFirst(req.getMessageId(), StringHelper.getId("temp", StringHelper.getNewID()));
// 获取消息临时Id,消息Id为空时标识为第一次发送,并设置默认消息Id
String finalMessageId = StringHelper.getFirst(req.getMessageId(), StringHelper.getId(TEMP_ID, StringHelper.getNewID()));
// 设置编号
CorrelationData correlationData = new CorrelationData();
correlationData.setId(finalMessageId);
rabbitTemplate.getRabbitTemplate().convertAndSend(req.getExchangeName(), req.getRouteKey(), req.getMessage(), new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message) throws AmqpException {
......@@ -91,8 +92,7 @@ public class MessageSendServiceImpl implements MessageSendService {
// 设置持久化
properties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
// 设置消息编号
properties.setMessageId(finalMessageId);
properties.setMessageId(StringHelper.getIdShort(finalMessageId, TEMP_ID));
if (req.getDedTime() > 0) {
properties.setExpiration(req.getDedTime() + "");
}
......@@ -109,10 +109,13 @@ public class MessageSendServiceImpl implements MessageSendService {
*/
@Override
public String onSuccess(String messageId) {
if (!StringHelper.isEmpty(messageId)) {
messageDao.remove(messageId);
String toId = StringHelper.getIdShort(messageId, TEMP_ID);
// 不是临时数据
if (!toId.equals(messageId) || StringHelper.isEmpty(toId)) {
return StringHelper.EMPTY;
}
return messageId;
messageDao.remove(toId);
return toId;
}
/**
......@@ -122,6 +125,7 @@ public class MessageSendServiceImpl implements MessageSendService {
*/
@Override
public String onError(MessageVo messageVo) {
messageVo.setMessageId(StringHelper.getIdShort(messageVo.getMessageId(), TEMP_ID));
messageVo.check();
// 设置处理次数
messageVo.setHandleCount(messageVo.getHandleCount() + 1);
......
......@@ -6,6 +6,7 @@ import com.yanzuoguang.mq.service.*;
import com.yanzuoguang.mq.vo.MessageVo;
import com.yanzuoguang.mq.vo.QueueVo;
import com.yanzuoguang.mq.vo.req.*;
import com.yanzuoguang.util.helper.StringHelper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
......@@ -67,6 +68,13 @@ public class MqServiceImpl implements MqService {
@Override
public String message(MessageVo req, boolean now) {
req.check();
// 设置默认消息Id
String defaultId = StringHelper.getFirst(req.getMessageId(), StringHelper.getNewID());
// 将Id去掉temp:
String simpleId = StringHelper.getId(MessageSendServiceImpl.TEMP_ID, StringHelper.getIdShort(defaultId, MessageSendServiceImpl.TEMP_ID));
// 增加temp标识第一次发送
req.setMessageId(simpleId);
return yzgMqProcedure.send(req, now);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment