Firing CDI events

The following example is a new version of the CustomerInfoController class we discussed in the previous section. The class has been modified to fire an event every time the user navigates to a new page:

package net.ensode.javaee8book.cdievents.controller; 
 
import java.io.Serializable; 
import javax.enterprise.context.Conversation; 
import javax.enterprise.context.RequestScoped; 
import javax.enterprise.event.Event; 
import javax.inject.Inject; 
import javax.inject.Named; 
import net.ensode.javaee8book.cdievents.event.NavigationInfo; 
import net.ensode.javaee8book.cdievents.model.Customer; 
 
@Named 
@RequestScoped 
public class CustomerInfoController implements Serializable { 
 
    @Inject 
    private Conversation conversation; 
    @Inject 
    private Customer customer; 
    @Inject 
    private Event<NavigationInfo> navigationInfoEvent; 
 
    public String customerInfoEntry() { 
        conversation.begin(); 
        NavigationInfo navigationInfo = new NavigationInfo(); 
        navigationInfo.setPage("1"); 
        navigationInfo.setCustomer(customer); 
 
        navigationInfoEvent.fire(navigationInfo); 
        return "page1"; 
    } 
 
    public String navigateToPage1() { 
        NavigationInfo navigationInfo = new NavigationInfo(); 
        navigationInfo.setPage("1"); 
        navigationInfo.setCustomer(customer); 
 
        navigationInfoEvent.fire(navigationInfo); 
 
        return "page1"; 
    } 
 
    public String navigateToPage2() { 
        NavigationInfo navigationInfo = new NavigationInfo(); 
        navigationInfo.setPage("2"); 
        navigationInfo.setCustomer(customer); 
 
        navigationInfoEvent.fire(navigationInfo); 
        return "page2"; 
    } 
 
    public String navigateToPage3() { 
        NavigationInfo navigationInfo = new NavigationInfo(); 
        navigationInfo.setPage("3"); 
        navigationInfo.setCustomer(customer); 
 
        navigationInfoEvent.fire(navigationInfo); 
        return "page3"; 
    } 
 
    public String navigateToConfirmationPage() { 
        NavigationInfo navigationInfo = new NavigationInfo(); 
        navigationInfo.setPage("confirmation"); 
        navigationInfo.setCustomer(customer); 
 
        navigationInfoEvent.fire(navigationInfo); 
        conversation.end(); 
        return "confirmation"; 
    } 
} 

As we can see, to create an event, we inject an instance of javax.enterprise.event.Event. This class uses generics, therefore, we need to specify its type; the type of the Event class can be any class implementing java.io.Serializable. In our case, we are passing an instance of a simple POJO we wrote as the type parameter. Our POJO is called NavigationInfo and has two properties: one Customertype, and a String containing the page the user is navigating to. Recall from the previous sections that each of the methods on our CustomerInfoController class triggers navigation from one page in the application to another. In this version of the controller, a CDI event is fired every time we navigate to a new page. In each case, we create a new instance of NavigationInfo, populate it, then fire the event by invoking the fire() method on our instance of javax.enterprise.event.Event.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.71.115