/java/

Using Events in CDI Applications

2015-07-09 21:27:56

From docs: Events allow beans to communicate without any compile-time dependency. One bean can define an event, another bean can fire the event, and yet another bean can handle the event. The beans can be in separate packages and even in separate tiers of the application.

Defining Events

package pl.btbw.events;

public class PaymentEvent {

    private String name;

    public PaymentEvent(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

as you see, that class is very simple

Events Handle

package pl.btbw.events;

import pl.btbw.events.annotation.PaymentTypeA;
import pl.btbw.events.annotation.PaymentTypeB;

import javax.enterprise.event.Observes;

public class PaymentHandler {

    public void creditPaymentA(@Observes @PaymentTypeA PaymentEvent paymentEvent) {
        System.out.println("Event: (Handler1) " + paymentEvent.getName());
    }

    public void creditPaymentB(@Observes @PaymentTypeB PaymentEvent paymentEvent) {
        System.out.println("Event: (Handler2) " + paymentEvent.getName());
    }

}

here we have to method with @PaymentTypeA and @PaymentTypeB Qualifier, it is optional, you can have one method to catch events (without Qualifier)

@Qualifier

package pl.btbw.events.annotation;

import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface PaymentTypeA {
}
package pl.btbw.events.annotation;

import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface PaymentTypeB {
}

Fire event

package pl.btbw;

import pl.btbw.events.PaymentEvent;
import pl.btbw.events.annotation.PaymentTypeA;
import pl.btbw.events.annotation.PaymentTypeB;

import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/")
public class AppCtrl {

    @Inject
    @PaymentTypeA
    private Event

<paymentevent> eventEventA;

    @Inject
    @PaymentTypeB
    private Event<paymentevent> eventEventB;

    @GET
    @Path("/")
    public String getTestAString(){
        eventEventA.fire(new PaymentEvent("some information for event"));
        return "fire PaymentEvent with @PaymentTypeA @Qualifier";
    }

    @GET
    @Path("/b")
    public String getTestBString(){
        eventEventB.fire(new PaymentEvent("some information for event"));
        return "fire PaymentEvent with @PaymentTypeB @Qualifier";
    }

}

Github