FirehoseEventListener.java
/*
* Copyright 2021 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gringlobal.component.firehose;
import java.util.Objects;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalApplicationListener;
import org.springframework.transaction.event.TransactionalEventListener;
/**
*
* The FirehoseEventListener is a {@link TransactionalApplicationListener} that
* is attached to the {@link TransactionPhase} {@code TransactionPhase.AFTER_COMMIT}
*
* The event listener then updates the Firehose delay queues accordingly.
*
* @author Artem Hrybeniuk
*/
@Component
public class FirehoseEventListener {
@Resource(name = "updatedEventSet")
private Set<FirehoseEvent> updatedEvents;
@Resource(name = "removedEventSet")
private Set<FirehoseEvent> removedEvents;
@Resource(name = "createdEventSet")
private Set<FirehoseEvent> createdEvents;
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void handleEvent(FirehoseEvent firehoseEvent) {
FirehoseEvent.EventType eventType = firehoseEvent.getEventType();
if (FirehoseEvent.EventType.DELETE == eventType) {
// Remove given FirehoseEvent from CreatedEvents and UpdatedEvents
createdEvents.removeIf(firehoseEvent::sameReference);
updatedEvents.removeIf(firehoseEvent::sameReference);
removedEvents.add(firehoseEvent);
} else if (FirehoseEvent.EventType.CREATE == eventType) {
createdEvents.add(firehoseEvent);
} else if (FirehoseEvent.EventType.UPDATE == eventType) {
putUpdatedEvent(firehoseEvent);
}
}
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void handleDeleteAllEvent(FirehoseDeleteAllEvent firehoseDeleteAllEvent) {
// Remove from created and updated only
createdEvents.removeIf(event -> Objects.equals(event.getClazz(), firehoseDeleteAllEvent.getClazz()));
updatedEvents.removeIf(event -> Objects.equals(event.getClazz(), firehoseDeleteAllEvent.getClazz()));
}
private void putUpdatedEvent(FirehoseEvent firehoseEvent) {
// replace equal FirehoseEvent in CreatedEvents if contains
boolean replaceCreatedEvent = replaceIfContains(createdEvents, firehoseEvent);
if (!replaceCreatedEvent) {
// replace equal FirehoseEvent in UpdatedEvents if contains
boolean replaceUpdatedEvent = replaceIfContains(updatedEvents, firehoseEvent);
if (!replaceUpdatedEvent) {
// add FirehoseEvent to UpdatedEvents
updatedEvents.add(firehoseEvent);
}
}
}
private boolean replaceIfContains(Set<FirehoseEvent> events, FirehoseEvent firehoseEvent) {
// replace Entity and ModifiedDate for equal FirehoseEvent in given Collection
if (events.removeIf(firehoseEvent::sameReference)) {
events.add(firehoseEvent);
return true;
}
return false;
}
}