Skip to content

Commit

Permalink
schedule first send second
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Gorzala committed Dec 26, 2023
1 parent 1d04e18 commit fc7d4d7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
import org.springframework.stereotype.Component;

import java.net.URI;
import java.time.OffsetDateTime;
import java.util.UUID;

@Component
@RequiredArgsConstructor
public class ProfileUpdateEventListener {
public class ApplicationEventListener {

public static final Logger log = LoggerFactory.getLogger(ProfileUpdateEventListener.class);
public static final Logger log = LoggerFactory.getLogger(ApplicationEventListener.class);

private static final URI FRONTEND_SOURCE = URI.create("http://dancier.net");
private static final URI BACKEND_SOURCE = URI.create("http://dancer.dancier.net");

private final EventlogService eventlogService;

Expand All @@ -43,8 +47,9 @@ public void handle(ProfileUpdatedEvent profileUpdatedEvent) {
CloudEvent cloudEvent = CloudEventBuilder
.v1()
.withId(UUID.randomUUID().toString())
.withSource(URI.create("F"))
.withSource(BACKEND_SOURCE)
.withType("profile-updated")
.withTime(OffsetDateTime.now())
.withData(objectMapper.writeValueAsBytes(profileUpdatedEvent)).build();
scheduleMessageAdapter.schedule(cloudEvent, profileUpdatedEvent.getDancer().getId().toString());
} catch (JsonProcessingException jpe) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.dancier.dancer.messaging;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.cloudevents.CloudEvent;
import lombok.RequiredArgsConstructor;
import net.dancier.dancer.eventlog.ScheduleMessagePort;
Expand All @@ -14,14 +16,20 @@ public class ScheduleMessageAdapter implements ScheduleMessagePort {

public static final Logger log = LoggerFactory.getLogger(ScheduleMessageAdapter.class);

private final KafkaTemplate kafkaTemplate;
private final OutboxJpaRepository outboxJpaRepository;

private final ObjectMapper objectMapper;

@Override
public void schedule(CloudEvent cloudEvent, String key) {
log.info("sending object: " + cloudEvent);
log.info("with key:" + key);
kafkaTemplate.send(cloudEvent.getType(),
key,
cloudEvent);
OutboxJpaEntity outboxJpaEntity = new OutboxJpaEntity();
outboxJpaEntity.setData(objectMapper.convertValue(cloudEvent, JsonNode.class));
outboxJpaEntity.setType(cloudEvent.getType());
outboxJpaEntity.setKey(key);
outboxJpaEntity.setCreatedAt(cloudEvent.getTime());
outboxJpaEntity.setStatus(OutboxJpaEntity.STATUS.NEW);
outboxJpaRepository.save(outboxJpaEntity);
}
}

0 comments on commit fc7d4d7

Please sign in to comment.