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.