Created
July 25, 2021 15:42
-
-
Save dunithd/dde21aaf0a956984d9fe8ebdd46d238a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.edu.samples.messagelog; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.Table; | |
import java.time.Instant; | |
@Entity | |
@Table(name = "consumed_messages") | |
public class ConsumedMessage { | |
@Id | |
private String eventId; | |
private Instant timeOfReceiving; | |
ConsumedMessage() { | |
} | |
public ConsumedMessage(String eventId, Instant timeOfReceiving) { | |
this.eventId = eventId; | |
this.timeOfReceiving = timeOfReceiving; | |
} | |
public String getEventId() { | |
return eventId; | |
} | |
public void setEventId(String eventId) { | |
this.eventId = eventId; | |
} | |
public Instant getTimeOfReceiving() { | |
return timeOfReceiving; | |
} | |
public void setTimeOfReceiving(Instant timeOfReceiving) { | |
this.timeOfReceiving = timeOfReceiving; | |
} | |
} |
This file contains hidden or 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
package com.edu.samples.messagelog; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.enterprise.context.ApplicationScoped; | |
import javax.persistence.EntityManager; | |
import javax.persistence.PersistenceContext; | |
import javax.transaction.Transactional; | |
import java.time.Instant; | |
import java.util.UUID; | |
@ApplicationScoped | |
public class MessageLog { | |
private static final Logger LOG = LoggerFactory.getLogger(MessageLog.class); | |
@PersistenceContext | |
EntityManager entityManager; | |
@Transactional(value= Transactional.TxType.MANDATORY) | |
public void processed(String eventId) { | |
entityManager.persist(new ConsumedMessage(eventId, Instant.now())); | |
} | |
@Transactional(value= Transactional.TxType.MANDATORY) | |
public boolean alreadyProcessed(String eventId) { | |
LOG.debug("Looking for event with id {} in message log", eventId); | |
return entityManager.find(ConsumedMessage.class, eventId) != null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment