Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug when create rocketmq producer #591

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,24 @@ public class RocketMQClientTemplate extends AbstractMessageSendingTemplate<Strin

private SimpleConsumerBuilder simpleConsumerBuilder;

private Producer producer;
private volatile Producer producer;

private SimpleConsumer simpleConsumer;
private volatile SimpleConsumer simpleConsumer;

private RocketMQMessageConverter rocketMQMessageConverter = new RocketMQMessageConverter();

private String charset = "UTF-8";

public Producer getProducer() {
if (Objects.isNull(producer)) {
try {
return producerBuilder.build();
} catch (ClientException e) {
throw new RuntimeException(e);
synchronized (RocketMQClientTemplate.class) {
if (Objects.isNull(producer)) {
try {
this.producer = producerBuilder.build();
} catch (ClientException e) {
throw new RuntimeException(e);
}
}
}
}
return producer;
Expand All @@ -77,10 +81,14 @@ public void setProducer(Producer producer) {

public SimpleConsumer getSimpleConsumer() {
if (Objects.isNull(simpleConsumer)) {
try {
return simpleConsumerBuilder.build();
} catch (ClientException e) {
throw new RuntimeException(e);
synchronized (RocketMQClientTemplate.class) {
if (Objects.isNull(simpleConsumer)) {
try {
this.simpleConsumer = simpleConsumerBuilder.build();
} catch (ClientException e) {
throw new RuntimeException(e);
}
}
}
}
return simpleConsumer;
Expand Down
Loading