JBoss.orgCommunity Documentation

Errai

Errai CDI Reference Guide

Legal Notice

Abstract

This is the Errai CDI reference documentation.


Preface
1. Document Conventions
2. Feedback
1. CDI Introduction
2. Errai CDI Features
2.1. Integration with the CDI event subsystem
2.1.1. Conversational events
2.2. RPC Style Invocations on CDI beans
2.3. Publish/Subscribe with CDI managed components
2.4. CDI Producers
3. Client-Server Event Example
4. Deploying Errai CDI
4.1. Deployment in Development Mode
4.2. Deployment to a Servlet Engine
4.3. Deployment to an Application Server
4.4. Configuration Options
5. Errai CDI License
A. Revision History

CDI (Contexts and Dependency Injection) is the Jave EE standard (JSR-299) for handling dependency injection. In addition to dependency injection, the standard encompasses component lifecycle, application configuration, call-interception and a decoupled, type-safe eventing specification.

The Errai CDI extension implements a subset of the specification for use inside of client-side applications within Errai, as well as additional capabilities such as distributed eventing.

Errai CDI does not currently implement all life cycles specified in JSR-299 or interceptors. These deficiencies may be addressed in future versions.

Beans that are deployed to a CDI container will automatically be registered with Errai and exposed to your GWT client application. So, you can use Errai to communicate between your GWT client components and your CDI backend beans. There are several very easy-to-use options:

  • Wiring up your GWT application with the CDI event subsystem

  • RPC style invocations on beans through a typed interface

  • Access beans in a publish/subscribe manner

Further, Errai enables you to make use of CDI producer methods and fields in your GWT client!

Any CDI managed component may produce and consume events . This allows beans to interact in a completely decoupled fashion. Beans consume events by registering for a particular event type and optional qualifiers. The Errai CDI extension simply extends this concept into the client tier. A GWT client application can simply register an Observer for a particular event type and thus receive events that are produced on the server-side. Likewise and using the same API, GWT clients can produce events that are consumed by a server-side observer.

Let's take a look at an example.


Two things are noteworthy in this example:

  1. Injection of an Event dispatcher proxy

  2. Creation of an Observer method for a particular event type

The event dispatcher is responsible for sending events created on the client-side to the server-side event subsystem (CDI container). This means any event that is fired through a dispatcher will eventually be consumed by a CDI managed bean, if there is an corresponding Observer registered for it on the server side.

In order to consume events that are created on the server-side you need to declare an client-side observer method for a particular event type. In case an event is fired on the server this method will be invoked with an event instance of type you declared.

To complete the example, let's look at the corresponding server-side CDI bean:


A key feature of the Errai CDI framework is the ability to federate the CDI eventing bus between the client and the server. This permits the observation of server produced events on the client, and vice-versa.

Example server code:


Domain-model:



Client application logic:


If you do not care about the deployment details for now and just want to get started take a look at the CDI Quickstart Guide .

The CDI integration is a plugin to the Errai core framework and represents a CDI portable extension. Which means it is discovered automatically by both Errai and the CDI container. In order to use it, you first need to understand the different runtime models involved when working GWT, Errai and CDI.

Typically a GWT application lifecycle begins in Development Mode and finally a web application containing the GWT client code will be deployed to a target container (Servlet Engine, Application Server). This is no way different when working with CDI components to back your application.

What's different however is availability of the CDI container across the different runtimes. In GWT development mode and in a pure servlet environment you need to provide and bootstrap the CDI environment on your own. While any Java EE 6 Application Server already provides a preconfigured CDI container. To accomodate these differences, we need to do a little trickery when executing the GWT Development Mode and packaging our application for deployment.

We provide integration with the JBoss Application Server , but the requirements are basically the same for other vendors. When running a GWT client app that leverages CDI beans on a Java EE 6 application server, CDI is already part of the container and accessible through JNDI ( java:/BeanManager ).

Errai CDI is distributed under the terms of the Apache License, Version 2.0. See the full Apache license text .

Revision History
Revision ToDo 0-0ToDo Wed Jan 19 2011ToDo Dude ToDo McPants
ToDo Initial creation of book