Created
March 28, 2024 09:17
-
-
Save serdarmumcu/7cbfbe229c1174b686745ea0c6f9e2de to your computer and use it in GitHub Desktop.
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
@Service | |
public class EventPublisherService { | |
@Autowired | |
private OutboxRepository outboxRepository; | |
@Autowired | |
private KafkaTemplate<String, String> kafkaTemplate; | |
@Scheduled(fixedRate = 10000) // Every 10 seconds | |
public void publishEvents() { | |
List<Outbox> events = outboxRepository.findAll(); | |
for (Outbox event : events) { | |
kafkaTemplate.send("messages-topic", event.getPayload()).thenAccept(sendResult -> { | |
outboxRepository.delete(event); | |
}).exceptionally(throwable -> { | |
// Log error or implement retry mechanism | |
return null; | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment