Using enums as CDI Events

Java enum can be used as an CDI "command" event:


public enum OrderEvent {
    CREATE, CANCEL;
}

Now you can sent enum instances:


public class OrderProcessor {

    @Inject
    Event<OrderEvent> events;

    public void createOrder() {
        events.fire(OrderEvent.CREATE);
    }

    public void cancelOrder() {
        events.fire(OrderEvent.CANCEL);
    }
}

...and consume them:


public class OrderMetrics {

    //...

    public void onSuccess(@Observes(during = TransactionPhase.AFTER_SUCCESS) OrderEvent event) {
        switch (event) {
            case CREATE:
                //...
                break;
            case CANCEL:
                //...
                break;
        }
    }
    public void onFailure(@Observes(during = TransactionPhase.AFTER_FAILURE) OrderEvent event) {
        switch (event) {
            case CREATE:
                //...
                break;
            case CANCEL:
                //...
                break;
        }
    }
}    

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


NEW: Online Workhop Effective WebApps without Frameworks is also coming to: MUC Airport.

Airport MUC workshops: Web (SPA, PWAs, Offline, Desktop, Mobile) Applications Essentials and Effective Web Applications. No migrations. #usetheplatform

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License