Skip to content

Instantly share code, notes, and snippets.

@dunithd
Created July 25, 2021 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dunithd/dde21aaf0a956984d9fe8ebdd46d238a to your computer and use it in GitHub Desktop.
Save dunithd/dde21aaf0a956984d9fe8ebdd46d238a to your computer and use it in GitHub Desktop.
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;
}
}
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