Exploring the Errai Framework

Posted: April 11th, 2013 | Author: | Filed under: Technology | Tags: , , , | No Comments »


Our discussion began with an introduction to Contexts and Dependency Injection and how it can be enabled on the client with the help of Errai. We will now go into some detail how CDI is being leveraged in the application. The @Inject annotation, which was briefly covered earlier in the JAX-RS section, allows for the injection of classes or interfaces into other classes. The benefits of this annotation are that classes become loosely coupled throughout an application. Another annotation which is found in the EntryPoint class ErraiMath is the @PostConstruct annotation. This annotation allows for the execution of a method after dependency injection has completed. However, it must also be mentioned that RPC calls should not be made within methods annotated with the @PostConstruct annotation because RPC proxies initialize asynchronously. The @AfterInitialization annotation should be used instead to prevent the application from producing undesired results.

To enable CDI support in Errai, the following was added to the ErraiMath.gwt.xml file:

<inherits name="org.jboss.errai.enterprise.CDI"/>

Also included in the application is a scoreboard which displays the operation performed, the date the operation was executed and information about the client who made the request. The scoreboard is populated through the firing of events on the server and the client observing these events. Events are fired from the server using the java.enterprise.event.Event and the @Observes annotation is used on the client side to respond to events in a typical publisher/subscriber pattern. In our application, we fire an event when any successful calculation is performed on the server. Before we look at the exact implementation, it is best to also mention that it is possible to fire events in various locations of the application. Clients and servers can each fire and observe events. Since multiple types of events can be found in an application, custom annotations can be used to limit the number of methods which respond to events. In our application we created a @Calculation annotation for this purpose. By inspecting the MathServiceImpl class, you will notice our event is annotated with both a @Inject and @Calculation annotation.

@Inject @Calculation
Event<MathExecution> mathExecutionResponseEvent;

The MathExecution object which is fired as part of the event is used to carry information about the specific execution including the operation performed and information about the client who made the request. The @Portable annotation is added to the MathExecution object so it is eligible to be sent over the ErraiBus. To fire an event, we call the following:

mathExecutionResponseEvent.fire(new MathExecution(userEnvironment, mathExecutionMessage, new Date()));

On the client side, to responds to such event, the following is found in the CalculationStatisticsWidget:

	public void onCalculationEvent(@Observes @Calculation MathExecution mathExecution)
		// Respond to Calculation Event

Since both the Observes and Calculation annotation are used, this method will only respond to events fired with the Calculation annotation.

Finally, to configure properties on the Bus itself, a file called ErraiService.properties was added to the classpath in the src/main/resources folder. The content of the file was taken directly from the Errai documentation as the default values were sufficient.


As mentioned previously, the application can be deployed multiple ways including GWT development mode. Due to issues experienced with the BeanManager and Event injection, the calculation scoreboard will not populate while running in GWT development mode.

Leave a Reply