Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
schbrain-parent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
panwangnan
schbrain-parent
Commits
d4661932
Commit
d4661932
authored
Jul 17, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce MessageProducer in kafka-starter
parent
4904fa60
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
1 deletion
+57
-1
starters/kafka-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/kafka/KafkaAutoConfiguration.java
...framework/autoconfigure/kafka/KafkaAutoConfiguration.java
+3
-1
starters/kafka-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/kafka/MessageProducer.java
...hbrain/framework/autoconfigure/kafka/MessageProducer.java
+54
-0
No files found.
starters/kafka-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/kafka/KafkaAutoConfiguration.java
View file @
d4661932
...
...
@@ -3,12 +3,14 @@ package com.schbrain.framework.autoconfigure.kafka;
import
com.schbrain.framework.autoconfigure.kafka.properties.KafkaProperties
;
import
org.springframework.boot.autoconfigure.AutoConfiguration
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.Import
;
/**
* @author liaozan
* @since 2023-04-29
*/
@AutoConfiguration
@Import
(
MessageProducer
.
class
)
@EnableConfigurationProperties
(
KafkaProperties
.
class
)
public
class
KafkaAutoConfiguration
{
...
...
starters/kafka-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/kafka/MessageProducer.java
0 → 100644
View file @
d4661932
package
com.schbrain.framework.autoconfigure.kafka
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.kafka.core.KafkaTemplate
;
import
org.springframework.kafka.support.SendResult
;
import
org.springframework.util.concurrent.ListenableFutureCallback
;
/**
* @author liaozan
* @since 2023/7/17
*/
@Slf4j
public
class
MessageProducer
{
@Autowired
private
KafkaTemplate
<
String
,
String
>
kafkaTemplate
;
/**
* producer 异步方式发送数据
*
* @param topic topic名称
* @param message producer发送的数据
* @param description 消息描述
*/
public
void
sendMessageAsync
(
String
topic
,
String
message
,
String
description
)
{
sendMessageAsync
(
topic
,
message
,
new
ListenableFutureCallback
<>()
{
@Override
public
void
onSuccess
(
SendResult
<
String
,
String
>
sendResult
)
{
log
.
debug
(
"{} 消息发送成功, message: {}"
,
description
,
message
);
}
@Override
public
void
onFailure
(
Throwable
exception
)
{
log
.
error
(
"{} 消息发送失败, {}"
,
description
,
exception
.
getMessage
(),
exception
);
}
});
}
/**
* producer 异步方式发送数据
*
* @param topic topic名称
* @param message producer发送的数据
*/
public
void
sendMessageAsync
(
String
topic
,
String
message
,
ListenableFutureCallback
<
SendResult
<
String
,
String
>>
callback
)
{
try
{
kafkaTemplate
.
send
(
topic
,
message
).
addCallback
(
callback
);
}
catch
(
Exception
exception
)
{
log
.
error
(
"消息发送失败, {}"
,
exception
.
getMessage
(),
exception
);
}
}
}
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