Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
microservice-base
Project
Project
Details
Activity
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
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhouxu
microservice-base
Commits
ef22b5d5
Commit
ef22b5d5
authored
Jan 19, 2020
by
zhouxu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加延时消息
parent
847c67fb
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
4 deletions
+57
-4
AliProducer.java
...java/com/miya/huihua/microservicebase/mq/AliProducer.java
+11
-0
AliProducerImpl.java
.../com/miya/huihua/microservicebase/mq/AliProducerImpl.java
+46
-4
No files found.
src/main/java/com/miya/huihua/microservicebase/mq/AliProducer.java
View file @
ef22b5d5
...
...
@@ -27,4 +27,15 @@ public interface AliProducer {
*/
void
putAsyncEvent
(
String
topic
,
Object
o
,
SendCallback
sendCallback
,
String
...
tags
);
/**
* 发送延时消息
*
* @param topic
* @param o
* @param sendCallback
* @param delayTime 指定执行的时间
* @param tags
*/
void
putDelayEvent
(
String
topic
,
Object
o
,
SendCallback
sendCallback
,
Long
delayTime
,
String
...
tags
);
}
src/main/java/com/miya/huihua/microservicebase/mq/AliProducerImpl.java
View file @
ef22b5d5
...
...
@@ -30,10 +30,9 @@ public class AliProducerImpl implements AliProducer {
@Override
public
void
putEvent
(
String
topic
,
Object
o
,
String
...
tags
)
{
Producer
producer
=
getProducer
(
topic
);
Message
message
=
new
Message
(
topic
,
tags
.
length
<
1
?
TAG_ALL
:
Arrays
.
asList
(
tags
).
stream
().
collect
(
Collectors
.
joining
(
"||"
)),
JSON
.
toJSONString
(
o
).
getBytes
());
try
{
SendResult
sendResult
=
producer
.
send
(
message
);
SendResult
sendResult
=
sendMessage
(
topic
,
getMessage
(
topic
,
o
,
tags
)
);
if
(
sendResult
!=
null
)
{
log
.
info
(
"send msg success,Topic is :{},getMessageId is :{}"
,
sendResult
.
getTopic
(),
sendResult
.
getMessageId
());
}
...
...
@@ -45,11 +44,54 @@ public class AliProducerImpl implements AliProducer {
@Override
public
void
putAsyncEvent
(
String
topic
,
Object
o
,
SendCallback
sendCallback
,
String
...
tags
)
{
sendAsyncMessage
(
topic
,
getMessage
(
topic
,
o
,
tags
),
sendCallback
);
}
@Override
public
void
putDelayEvent
(
String
topic
,
Object
o
,
SendCallback
sendCallback
,
Long
delayTime
,
String
...
tags
)
{
Message
message
=
getMessage
(
topic
,
o
,
tags
);
message
.
setStartDeliverTime
(
delayTime
);
if
(
sendCallback
!=
null
)
{
sendAsyncMessage
(
topic
,
getMessage
(
topic
,
o
,
tags
),
sendCallback
);
}
else
{
SendResult
sendResult
=
sendMessage
(
topic
,
getMessage
(
topic
,
o
,
tags
));
if
(
sendResult
!=
null
)
{
log
.
info
(
"send msg success,Topic is :{},getMessageId is :{}"
,
sendResult
.
getTopic
(),
sendResult
.
getMessageId
());
}
}
}
/**
* 发送异步消息
*
* @param topic
* @param message
* @param sendCallback
*/
public
void
sendAsyncMessage
(
String
topic
,
Message
message
,
SendCallback
sendCallback
)
{
Producer
producer
=
getProducer
(
topic
);
Message
message
=
new
Message
(
topic
,
tags
.
length
<
1
?
TAG_ALL
:
Arrays
.
asList
(
tags
).
stream
().
collect
(
Collectors
.
joining
(
"||"
)),
JSON
.
toJSONString
(
o
).
getBytes
());
producer
.
sendAsync
(
message
,
sendCallback
);
}
/**
* 发送同步消息
*
* @param topic
* @param message
* @return
*/
public
SendResult
sendMessage
(
String
topic
,
Message
message
)
{
Producer
producer
=
getProducer
(
topic
);
SendResult
sendResult
=
producer
.
send
(
message
);
return
sendResult
;
}
private
Message
getMessage
(
String
topic
,
Object
o
,
String
...
tags
)
{
Message
message
=
new
Message
(
topic
,
tags
.
length
<
1
?
TAG_ALL
:
Arrays
.
asList
(
tags
).
stream
().
collect
(
Collectors
.
joining
(
"||"
)),
JSON
.
toJSONString
(
o
).
getBytes
());
return
message
;
}
private
Producer
getProducer
(
String
topic
)
{
ConcurrentHashMap
<
String
,
Producer
>
concurrentHashMap
=
onsManager
.
getProducerConcurrentHashMap
();
return
concurrentHashMap
.
get
(
topic
);
...
...
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