-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marc Gorzala
committed
Dec 27, 2023
1 parent
599d1fa
commit 4da38fa
Showing
2 changed files
with
55 additions
and
8 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/net/dancier/dancer/messaging/SendMessagesJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package net.dancier.dancer.messaging; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.cloudevents.CloudEvent; | ||
import io.cloudevents.core.builder.CloudEventBuilder; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.net.URI; | ||
import java.util.Collection; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class SendMessagesJob { | ||
|
||
private final static Logger log = LoggerFactory.getLogger(SendMessagesJob.class); | ||
|
||
private final OutboxJpaRepository outboxJpaRepository; | ||
|
||
private final KafkaTemplate kafkaTemplate; | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Transactional | ||
@Scheduled(fixedRate = 2000) | ||
public void sendMessages() throws JsonProcessingException { | ||
Collection<OutboxJpaEntity> itemsToSend = outboxJpaRepository.lockAndList(); | ||
for (OutboxJpaEntity item: itemsToSend) { | ||
log.info("Sending: {}", item); | ||
send(item); | ||
item.setStatus(OutboxJpaEntity.STATUS.DONE); | ||
} | ||
kafkaTemplate.flush(); | ||
} | ||
|
||
private void send(OutboxJpaEntity item) throws JsonProcessingException { | ||
CloudEvent cloudEvent = CloudEventBuilder.v1() | ||
.withId(item.getId().toString()) | ||
.withSource(URI.create(item.getSource())) | ||
.withType(item.getType()) | ||
.withData(objectMapper.writeValueAsBytes(item.getData())) | ||
.build(); | ||
|
||
kafkaTemplate.send(item.getType(), item.getKey(), cloudEvent); | ||
|
||
} | ||
} |
10 changes: 2 additions & 8 deletions
10
src/main/java/net/dancier/dancer/messaging/TopicConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters