Errai: The browser as a platform

Thursday, April 21, 2011

Latest enhancements to the Errai CDI integration module

CDI event qualifiers
The Errai CDI integration module (see a previous blog post) has support for event qualifiers now, both for event producers and observers. Multiple qualifiers can be specified for an event and of course custom qualifiers can be used.
CDI conversations
The simple use of @Conversational allows a server to directly address a single client in response to an event.
BusReadyEvent
Clients can observe a BusReadyEvent to be notified when the CDI event system has been initialized and the message bus is ready to be used.

The example code below shows how to use these new features. Notice again the use of a uniform API on both the client and server.  For more details on Errai's CDI support see the online documentation. Any feedback is appreciated!

@EntryPoint
public class SimpleDocumentClient extends LayoutPanel {

    @Inject
    private Event<DocumentSubscription> documentSubscriptionEvent;

    @Inject @Published
    private Event<Document> publishedDocumentEvent;

    public void buildUI(@Observes BusReadyEvent busReadyEvent) {
        documentSubscriptionEvent.fire(new DocumentSubscription());
        // ...
        new Button().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                publishedDocumentEvent.fire(new Document());
            }
        });
    }

    public void onDocumentsReceived(@Observes Documents document) {
        // this event is received as a direct response to the subscription event
        // ...
    }

    public void onDocumentCreated(@Observes @New Document document) {
       // ...
    }

    public void onDocumentUpdated(@Observes @Updated Document document) {
       // ...
    }

    public void onDocumentDeleted(@Observes @Deleted Document document) {
       // ...
    }
}

@ApplicationScoped
public class SimpleDocumentService {

    @Inject
    private Event<Documents> subscriptionResponseEvent;
    @Inject @New
    private Event<Document> documentCreatedEvent;

    @Inject @Updated
    private Event<Document> documentUpdatedEvent;

    @Inject @Deleted
    private Event<Document> documentDeletedEvent;
   
    @Conversational
    public void onDocumentSubscription(@Observes DocumentSubscription subscription) {
        Documents documents = new Documents();
        //...
        // respond to the client directly
        subscriptionResponseEvent.fire(documents);
    }

    public void onDocumentPublished(@Observes @Published Document document) {
        // receive document published events from the clients
        //...
    }
    private void createDocument(Document document) {
        // ...
        documentCreatedEvent.fire(document);
    }
    private void updateDocument(Document document) {
        // ...
        documentUpdatedEvent.fire(document);
    }
    private void deleteDocument(Document document) {
        // ...
        documentDeletedEvent.fire(document);
    }
}

No comments:

Post a Comment