Last active
September 14, 2020 21:04
-
-
Save brunocribeiro/b9844ba8a150a0ea8f4afbbce395625e to your computer and use it in GitHub Desktop.
Application Events with Spring
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
import org.springframework.context.event.EventListener; | |
@Component | |
public class EventListener { | |
@EventListener(condition = "#wrapper ne null and #wrapper.valid") | |
public void handleEvent(final EventWrapper wrapper) { | |
var events = wrapper.getEvents(); | |
// do your stuff | |
} | |
} |
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
public interface EventPublisher { | |
/** | |
* Notifica listneres que dão matching de acordo com o objeto publicado | |
* | |
* @param event evento a ser publicado | |
*/ | |
<T> void publishEvent(final T event); | |
} |
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
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
public class EventPublisherConfig { | |
@Bean | |
EventPublisher eventPublisher(final ApplicationContext applicationContext) { | |
return new EventPublisherImpl(applicationContext); | |
} | |
} |
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
import org.springframework.context.ApplicationEventPublisher; | |
import lombok.RequiredArgsConstructor; | |
@RequiredArgsConstructor | |
public final class EventPublisherImpl implements EventPublisher { | |
private final ApplicationEventPublisher publisher; | |
/** | |
* Notifica listneres que dão matching de acordo com o objeto publicado | |
* | |
* @param event evento a ser publicado | |
* @see ApplicationEventPublisher#publishEvent(Object) | |
*/ | |
public <T> void publishEvent(final T event) { | |
publisher.publishEvent(event); | |
} | |
} |
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
import java.util.List; | |
import java.util.Map; | |
import lombok.AccessLevel; | |
import lombok.AllArgsConstructor; | |
import lombok.Builder; | |
import lombok.Getter; | |
import lombok.Singular; | |
@Getter | |
@Builder | |
@AllArgsConstructor(access = AccessLevel.PRIVATE) | |
public class EventWrapper { | |
@Singular | |
private final List<Event> events; | |
public boolean isValid() { | |
return events != null && !events.isEmpty(); | |
} | |
@Getter | |
@Builder | |
@NoArgsConstructor(access = AccessLevel.PRIVATE) | |
@AllArgsConstructor(access = AccessLevel.PRIVATE) | |
public static final class Event { | |
private Map<String, Object> data; | |
} | |
} |
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
import org.springframework.stereotype.Component; | |
import org.springframework.transaction.annotation.Propagation; | |
import org.springframework.transaction.annotation.Transactional; | |
import org.springframework.transaction.event.TransactionalEventListener; | |
@Component | |
public class TransactionalEventListener { | |
@Transactional(propagation = Propagation.REQUIRES_NEW) | |
@TransactionalEventListener(condition = "#wrapper ne null and #wrapper.valid") | |
public void handleEvent(final EventWrapper wrapper) { | |
var events = wrapper.getEvents(); | |
// do your stuff | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment