Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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