Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
bd6ac76b
Commit
bd6ac76b
authored
Aug 12, 2021
by
yanzg
Browse files
Options
Browse Files
Download
Plain Diff
Merge branches 'master' and 'xy' of
http://192.168.0.204/yzg/yzg-util
into xy
parents
aff8222f
db2a2769
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
151 additions
and
44 deletions
+151
-44
HttpCodeException.java
...ava/com/yanzuoguang/util/exception/HttpCodeException.java
+110
-25
StringHelper.java
...c/main/java/com/yanzuoguang/util/helper/StringHelper.java
+12
-0
BaseVo.java
...il-base/src/main/java/com/yanzuoguang/util/vo/BaseVo.java
+6
-1
MqConfigurable.java
...src/main/java/com/yanzuoguang/mq/base/MqConfigurable.java
+1
-10
MessageLogDaoImpl.java
...n/java/com/yanzuoguang/mq/dao/impl/MessageLogDaoImpl.java
+2
-0
MessageSendServiceImpl.java
...m/yanzuoguang/mq/service/impl/MessageSendServiceImpl.java
+12
-8
MqServiceImpl.java
...n/java/com/yanzuoguang/mq/service/impl/MqServiceImpl.java
+8
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/exception/HttpCodeException.java
View file @
bd6ac76b
...
@@ -5,39 +5,56 @@ package com.yanzuoguang.util.exception;
...
@@ -5,39 +5,56 @@ package com.yanzuoguang.util.exception;
*
*
* @author 颜佐光
* @author 颜佐光
*/
*/
public
class
HttpCodeException
extends
RuntimeException
{
public
class
HttpCodeException
extends
RuntimeCodeException
{
private
static
final
long
serialVersionUID
=
-
4625832188480820883L
;
/**
* 错误码
*/
private
String
code
=
"99"
;
/**
/**
* 包含的数据
* 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
(
)
{
public
HttpCodeException
(
String
message
,
Object
target
)
{
return
this
.
code
;
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
(
)
{
public
HttpCodeException
(
String
message
,
Object
target
,
Throwable
cause
)
{
return
target
;
super
(
message
,
target
,
cause
)
;
}
}
/**
/**
* 构造函数
* 构造函数
* throw new CodeException("01","该订单已过期",order);
* throw new CodeException("01","该订单已过期",order);
...
@@ -45,11 +62,10 @@ public class HttpCodeException extends RuntimeException {
...
@@ -45,11 +62,10 @@ public class HttpCodeException extends RuntimeException {
*
*
* @param code 错误码
* @param code 错误码
* @param message 错误消息
* @param message 错误消息
* @param target 错误数据源,如订单数据
*/
*/
public
HttpCodeException
(
String
code
,
String
message
)
{
public
HttpCodeException
(
String
code
,
String
message
,
Object
target
)
{
super
(
message
);
super
(
code
,
message
,
target
);
this
.
code
=
code
;
this
.
target
=
null
;
}
}
/**
/**
...
@@ -66,8 +82,77 @@ public class HttpCodeException extends RuntimeException {
...
@@ -66,8 +82,77 @@ public class HttpCodeException extends RuntimeException {
* unknown.)
* unknown.)
*/
*/
public
HttpCodeException
(
String
code
,
String
message
,
Object
target
,
Throwable
cause
)
{
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
);
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
);
}
}
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/StringHelper.java
View file @
bd6ac76b
...
@@ -1008,6 +1008,18 @@ public class StringHelper {
...
@@ -1008,6 +1008,18 @@ public class StringHelper {
return
getMD5Id
(
froms
);
return
getMD5Id
(
froms
);
}
}
/**
* 获取组合编号
*
* @param args 需要组合的编号
* @return 将ID列表进行组合生成ID
*/
public
static
String
getIdShort
(
String
from
,
Object
...
args
)
{
String
id
=
getId
(
args
);
return
from
.
replaceFirst
(
id
+
":"
,
""
);
}
/**
/**
* 获取组合编号
* 获取组合编号
*
*
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/vo/BaseVo.java
View file @
bd6ac76b
...
@@ -22,6 +22,11 @@ public class BaseVo implements Serializable {
...
@@ -22,6 +22,11 @@ public class BaseVo implements Serializable {
*/
*/
@Override
@Override
public
String
toString
()
{
public
String
toString
()
{
return
JsonHelper
.
serialize
(
this
);
try
{
return
JsonHelper
.
serialize
(
this
);
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
return
super
.
toString
();
}
}
}
}
}
yzg-util-mq/src/main/java/com/yanzuoguang/mq/base/MqConfigurable.java
View file @
bd6ac76b
...
@@ -63,11 +63,7 @@ public class MqConfigurable implements RabbitTemplate.ConfirmCallback, RabbitTem
...
@@ -63,11 +63,7 @@ public class MqConfigurable implements RabbitTemplate.ConfirmCallback, RabbitTem
try
{
try
{
if
(
ack
&&
correlationData
!=
null
if
(
ack
&&
correlationData
!=
null
&&
!
StringHelper
.
isEmpty
(
correlationData
.
getId
()))
{
&&
!
StringHelper
.
isEmpty
(
correlationData
.
getId
()))
{
String
toId
=
getId
(
correlationData
.
getId
());
messageSendService
.
onSuccess
(
correlationData
.
getId
());
// 不是临时数据
if
(
toId
.
equals
(
correlationData
.
getId
()))
{
messageSendService
.
onSuccess
(
toId
);
}
}
else
if
(!
ack
)
{
}
else
if
(!
ack
)
{
System
.
out
.
println
(
"丢失消息:"
+
ack
+
" msg:"
+
cause
);
System
.
out
.
println
(
"丢失消息:"
+
ack
+
" msg:"
+
cause
);
}
}
...
@@ -94,15 +90,10 @@ public class MqConfigurable implements RabbitTemplate.ConfirmCallback, RabbitTem
...
@@ -94,15 +90,10 @@ public class MqConfigurable implements RabbitTemplate.ConfirmCallback, RabbitTem
String
content
=
new
String
(
message
.
getBody
(),
charset
);
String
content
=
new
String
(
message
.
getBody
(),
charset
);
// 组成消息
// 组成消息
MessageVo
messageVo
=
new
MessageVo
(
exchange
,
routingKey
,
content
);
MessageVo
messageVo
=
new
MessageVo
(
exchange
,
routingKey
,
content
);
messageVo
.
setMessageId
(
getId
(
messageProperties
.
getMessageId
()));
// 写入数据库
// 写入数据库
messageSendService
.
onError
(
messageVo
);
messageSendService
.
onError
(
messageVo
);
}
catch
(
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
Log
.
error
(
MqConfigurable
.
class
,
ex
);
Log
.
error
(
MqConfigurable
.
class
,
ex
);
}
}
}
}
private
String
getId
(
String
from
)
{
return
from
.
replace
(
"temp:"
,
""
);
}
}
}
yzg-util-mq/src/main/java/com/yanzuoguang/mq/dao/impl/MessageLogDaoImpl.java
View file @
bd6ac76b
...
@@ -35,6 +35,8 @@ public class MessageLogDaoImpl extends BaseDaoImpl implements MessageLogDao, Ini
...
@@ -35,6 +35,8 @@ public class MessageLogDaoImpl extends BaseDaoImpl implements MessageLogDao, Ini
*/
*/
@Override
@Override
protected
void
init
()
{
protected
void
init
()
{
// 设置缓存3秒
cacheList
.
setClearSecond
(
3
);
// 注册表结构
// 注册表结构
register
(
MessageLogVo
.
class
);
register
(
MessageLogVo
.
class
);
}
}
...
...
yzg-util-mq/src/main/java/com/yanzuoguang/mq/service/impl/MessageSendServiceImpl.java
View file @
bd6ac76b
...
@@ -32,6 +32,8 @@ import java.util.List;
...
@@ -32,6 +32,8 @@ import java.util.List;
@Component
@Component
public
class
MessageSendServiceImpl
implements
MessageSendService
{
public
class
MessageSendServiceImpl
implements
MessageSendService
{
public
static
final
String
TEMP_ID
=
"temp"
;
@Autowired
@Autowired
private
MyRabbitTemplate
rabbitTemplate
;
private
MyRabbitTemplate
rabbitTemplate
;
...
@@ -77,12 +79,11 @@ public class MessageSendServiceImpl implements MessageSendService {
...
@@ -77,12 +79,11 @@ public class MessageSendServiceImpl implements MessageSendService {
@Override
@Override
public
String
send
(
MessageVo
req
)
{
public
String
send
(
MessageVo
req
)
{
req
.
check
();
req
.
check
();
// 获取消息临时Id
// 获取消息临时Id
,消息Id为空时标识为第一次发送,并设置默认消息Id
String
finalMessageId
=
StringHelper
.
getFirst
(
req
.
getMessageId
(),
StringHelper
.
getId
(
"temp"
,
StringHelper
.
getNewID
()));
String
finalMessageId
=
StringHelper
.
getFirst
(
req
.
getMessageId
(),
StringHelper
.
getId
(
TEMP_ID
,
StringHelper
.
getNewID
()));
// 设置编号
// 设置编号
CorrelationData
correlationData
=
new
CorrelationData
();
CorrelationData
correlationData
=
new
CorrelationData
();
correlationData
.
setId
(
finalMessageId
);
correlationData
.
setId
(
finalMessageId
);
rabbitTemplate
.
getRabbitTemplate
().
convertAndSend
(
req
.
getExchangeName
(),
req
.
getRouteKey
(),
req
.
getMessage
(),
new
MessagePostProcessor
()
{
rabbitTemplate
.
getRabbitTemplate
().
convertAndSend
(
req
.
getExchangeName
(),
req
.
getRouteKey
(),
req
.
getMessage
(),
new
MessagePostProcessor
()
{
@Override
@Override
public
Message
postProcessMessage
(
Message
message
)
throws
AmqpException
{
public
Message
postProcessMessage
(
Message
message
)
throws
AmqpException
{
...
@@ -91,8 +92,7 @@ public class MessageSendServiceImpl implements MessageSendService {
...
@@ -91,8 +92,7 @@ public class MessageSendServiceImpl implements MessageSendService {
// 设置持久化
// 设置持久化
properties
.
setDeliveryMode
(
MessageDeliveryMode
.
PERSISTENT
);
properties
.
setDeliveryMode
(
MessageDeliveryMode
.
PERSISTENT
);
// 设置消息编号
// 设置消息编号
properties
.
setMessageId
(
finalMessageId
);
properties
.
setMessageId
(
StringHelper
.
getIdShort
(
finalMessageId
,
TEMP_ID
));
if
(
req
.
getDedTime
()
>
0
)
{
if
(
req
.
getDedTime
()
>
0
)
{
properties
.
setExpiration
(
req
.
getDedTime
()
+
""
);
properties
.
setExpiration
(
req
.
getDedTime
()
+
""
);
}
}
...
@@ -109,10 +109,13 @@ public class MessageSendServiceImpl implements MessageSendService {
...
@@ -109,10 +109,13 @@ public class MessageSendServiceImpl implements MessageSendService {
*/
*/
@Override
@Override
public
String
onSuccess
(
String
messageId
)
{
public
String
onSuccess
(
String
messageId
)
{
if
(!
StringHelper
.
isEmpty
(
messageId
))
{
String
toId
=
StringHelper
.
getIdShort
(
messageId
,
TEMP_ID
);
messageDao
.
remove
(
messageId
);
// 不是临时数据
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 {
...
@@ -122,6 +125,7 @@ public class MessageSendServiceImpl implements MessageSendService {
*/
*/
@Override
@Override
public
String
onError
(
MessageVo
messageVo
)
{
public
String
onError
(
MessageVo
messageVo
)
{
messageVo
.
setMessageId
(
StringHelper
.
getIdShort
(
messageVo
.
getMessageId
(),
TEMP_ID
));
messageVo
.
check
();
messageVo
.
check
();
// 设置处理次数
// 设置处理次数
messageVo
.
setHandleCount
(
messageVo
.
getHandleCount
()
+
1
);
messageVo
.
setHandleCount
(
messageVo
.
getHandleCount
()
+
1
);
...
...
yzg-util-mq/src/main/java/com/yanzuoguang/mq/service/impl/MqServiceImpl.java
View file @
bd6ac76b
...
@@ -6,6 +6,7 @@ import com.yanzuoguang.mq.service.*;
...
@@ -6,6 +6,7 @@ import com.yanzuoguang.mq.service.*;
import
com.yanzuoguang.mq.vo.MessageVo
;
import
com.yanzuoguang.mq.vo.MessageVo
;
import
com.yanzuoguang.mq.vo.QueueVo
;
import
com.yanzuoguang.mq.vo.QueueVo
;
import
com.yanzuoguang.mq.vo.req.*
;
import
com.yanzuoguang.mq.vo.req.*
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
org.springframework.amqp.core.Message
;
import
org.springframework.amqp.core.Message
;
import
org.springframework.amqp.rabbit.core.ChannelAwareMessageListener
;
import
org.springframework.amqp.rabbit.core.ChannelAwareMessageListener
;
import
org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer
;
import
org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer
;
...
@@ -67,6 +68,13 @@ public class MqServiceImpl implements MqService {
...
@@ -67,6 +68,13 @@ public class MqServiceImpl implements MqService {
@Override
@Override
public
String
message
(
MessageVo
req
,
boolean
now
)
{
public
String
message
(
MessageVo
req
,
boolean
now
)
{
req
.
check
();
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
);
return
yzgMqProcedure
.
send
(
req
,
now
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment