Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
amos-boot-biz
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
项目统一框架
amos-boot-biz
Commits
dd41da46
Commit
dd41da46
authored
Aug 01, 2023
by
刘林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(message):添加省级消息转发
parent
a32d0d83
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
17 deletions
+44
-17
KafkaConsumerService.java
.../com/yeejoin/amos/message/kafka/KafkaConsumerService.java
+44
-17
No files found.
amos-boot-utils/amos-boot-utils-message/src/main/java/com/yeejoin/amos/message/kafka/KafkaConsumerService.java
View file @
dd41da46
package
com
.
yeejoin
.
amos
.
message
.
kafka
;
import
lombok.extern.slf4j.Slf4j
;
import
net.sf.json.JSONArray
;
import
net.sf.json.JSONObject
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.kafka.clients.consumer.ConsumerRecord
;
import
org.eclipse.paho.client.mqttv3.MqttException
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -9,9 +11,8 @@ import org.springframework.kafka.annotation.KafkaListener;
import
org.springframework.kafka.support.Acknowledgment
;
import
org.springframework.stereotype.Service
;
import
org.typroject.tyboot.component.emq.EmqKeeper
;
import
java.io.UnsupportedEncodingException
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Arrays
;
import
java.util.Optional
;
/**
...
...
@@ -24,30 +25,27 @@ import java.util.Optional;
@Service
public
class
KafkaConsumerService
{
private
static
final
String
MQTT_TOPIC
=
"romaSite/data/transmit"
;
private
static
final
String
PROVINCE_MQTT_TOPIC
=
"province/data/transport"
;
@Autowired
protected
EmqKeeper
emqKeeper
;
private
static
final
String
MQTT_TOPIC
=
"romaSite/data/transmit"
;
/**
* 消费单条消息,topics 可以监听多个topic,如:topics = {"topic1", "topic2"}
*
* @param message 消息
*/
@KafkaListener
(
id
=
"consumerSingle"
,
idIsGroup
=
false
,
topics
=
"#{'${kafka.topics}'.split(',')}"
,
concurrency
=
"2"
)
public
void
consumerSingle
(
String
message
,
Acknowledgment
ack
)
{
public
void
consumerSingle
(
String
message
,
Acknowledgment
ack
)
{
JSONObject
messageObj
=
JSONObject
.
fromObject
(
message
);
try
{
String
topic
=
messageObj
.
getString
(
"topic"
);
JSONObject
data
=
messageObj
.
getJSONObject
(
"data"
);
emqKeeper
.
getMqttClient
().
publish
(
topic
,
data
.
toString
().
getBytes
(
"UTF-8"
),
0
,
false
);
}
catch
(
MqttException
e
)
{
e
.
printStackTrace
();
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
emqKeeper
.
getMqttClient
().
publish
(
topic
,
data
.
toString
().
getBytes
(
StandardCharsets
.
UTF_8
),
0
,
false
);
}
catch
(
Exception
e
)
{
// log.info("单条消息 ====> message: {}", messag
e);
log
.
error
(
"消息转发失败"
+
e
.
getMessage
(),
e
);
}
// log.info("单条消息 ====> message: {}", message);
ack
.
acknowledge
();
}
...
...
@@ -58,17 +56,46 @@ public class KafkaConsumerService {
try
{
JSONObject
messageObj
=
JSONObject
.
fromObject
(
record
.
value
());
JSONObject
data
=
messageObj
.
getJSONObject
(
"body"
);
if
(
data
.
size
()
==
0
)
{
if
(
data
.
isEmpty
())
{
data
=
messageObj
;
data
.
put
(
"datatype"
,
"state"
);
data
.
put
(
"datatype"
,
"state"
);
}
log
.
info
(
"接收到Roma消息对象: {}"
,
data
);
//
log.info("接收到Roma消息对象: {}", data);
emqKeeper
.
getMqttClient
().
publish
(
MQTT_TOPIC
,
data
.
toString
().
getBytes
(
StandardCharsets
.
UTF_8
),
0
,
false
);
}
catch
(
MqttException
e
)
{
e
.
printStackTrace
();
log
.
error
(
"消息转发失败"
+
e
.
getMessage
(),
e
);
ack
.
acknowledge
();
}
}
}
/**
* 省级消息转发
* @param message 省级消息
* @param ack ack
*/
@KafkaListener
(
id
=
"provinceMessage"
,
groupId
=
"province"
,
topics
=
"#{'${kafka.topics}'.split(',')}"
,
concurrency
=
"2"
)
public
void
consumerSingle1
(
String
message
,
Acknowledgment
ack
)
{
Optional
<?>
messages
=
Optional
.
ofNullable
(
message
);
if
(
messages
.
isPresent
())
{
try
{
JSONObject
jsonObject
=
JSONObject
.
fromObject
(
message
);
String
type
=
jsonObject
.
optString
(
"type"
);
String
table
=
jsonObject
.
optString
(
"table"
);
if
(
StringUtils
.
isNoneEmpty
(
type
,
table
))
{
if
(
Arrays
.
asList
(
"INSERT"
,
"UPDATE"
).
contains
(
type
))
{
JSONArray
array
=
jsonObject
.
getJSONArray
(
"data"
);
JSONObject
data
=
(
JSONObject
)
array
.
get
(
0
);
data
.
put
(
"type"
,
type
);
data
.
put
(
"table"
,
table
);
emqKeeper
.
getMqttClient
().
publish
(
PROVINCE_MQTT_TOPIC
,
data
.
toString
().
getBytes
(
StandardCharsets
.
UTF_8
),
0
,
false
);
}
}
}
catch
(
MqttException
e
)
{
log
.
error
(
"消息转发失败"
+
e
.
getMessage
(),
e
);
ack
.
acknowledge
();
}
}
ack
.
acknowledge
();
}
...
...
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