JBoss.orgCommunity Documentation

Chapter 1. Introduction

1.1. What is it?
1.2. Required software
1.3. Getting Started with Errai
1.3.1. Technology Primer
1.3.2. Creating your first project
1.3.3. Running and Debugging with GWT’s Super Dev mode
1.3.4. Running and Debugging in Eclipse using Maven tooling
1.3.5. Running and Debugging in your IDE using GWT tooling
1.3.6. A Gentle Introduction to CDI

Errai is a GWT-based framework for building rich web applications using next-generation web technologies. Built on-top of ErraiBus, the framework provides a unified federation and RPC infrastructure with true, uniform, asynchronous messaging across the client and server.

Errai requires a JDK version 6 or higher and depends on Apache Maven to build and run the examples, and for leveraging the quickstart utilities.

Errai is a framework which combines a constellation of web and server-side technologies to assist you in developing large, scaleable rich web applications using a consistent, standardized programming model for client and server development.

Since Errai is an end-to-end framework, in that, parts of the framework run and operate within the client and parts run and operate within the server, there is a set of various technologies upon which Errai relies. This section will detail the basic core technologies which you’ll need to be familiar with.

CDI is a standard part of the Java EE 6.0 stack, and is defined in the JSR-299 specification. CDI is the main programming model explored in this guide. As such, the basic concepts of CDI will be introduced in this guide, so pre-existing knowledge is not strictly necessary.

Errai’s support for CDI is two-fold. For the server-side, Errai has integration with Weld, which is the reference implementation (RI) of the JSR-299 specification. The client-side integration for CDI is provided by the Errai CDI extension. Errai CDI implements a subset of the JSR-299 specification to provide the CDI programming model within client code.

Maven Required

The first thing you’ll need to do if you have not already, is install Maven . If you have not already installed Maven, do so now.

Warning: If you use maven2, you will run into this problem: https://community.jboss.org/thread/177645

You have two options to set up an Errai application. You can start by copying an existing example application (i.e. the errai tutorial demo) or by building an app with the Errai Forge Addon:

Simply download and unzip this demo. Check out the README file and continue with running the app in GWT’s development mode and importing the project into Eclipse .

Another way to start a new project with Errai is to use Forge and the Errai Forge Addon. To use this method, follow the instructions here to install the Errai Forge Addon and create a new project.

In the upcomming sections, we will demonstrate how to run your app in GWT Development Mode through the command line and eclipse, so it would be nice to have something to run so that you are able to verify that everything is working. Here is a sample class you can use that displays an alert when the app loads:

// Add the package declaration here


import javax.annotation.PostConstruct;
import org.jboss.errai.ioc.client.api.EntryPoint;
import com.google.gwt.user.client.Window;
@EntryPoint
public class App {
  @PostConstruct
  public void onLoad() {
    Window.alert("Hello World!");
  }
}

For this code to run properly, you must use the Errai Forge Addon Add Errai Features command to install Errai IOC.

Create new subfolder, client/local, under the folder containing your GWT module file. Then create a file, App.java, in this new package and copy the above sample code (making sure to replace the top comment with the package declaration).

Tip

Plugin Tips

Keep an eye out for tips in the proceeding sections on how you can use the Errai Forge plugin to configure other Errai features for your new project.

There are two ways to configure your project in your IDE. One option is to use the built-in Maven tooling to set up Maven run and debug configurations within your IDE. The other option is to download and use GWT tooling that is available for your IDE, and create custom run/debug configurations with that. Note that for Intellij IDEA, the GWT tooling is only available for users of the Ultimate Edition.

Errai’s EmbeddedWildFlyLauncher provides an embedded WildFly instance within Dev Mode to allow users to debug server- and client-side code simultaneously in a single JVM. Here are the instructions for using it in Eclipse and Intellij IDEA Ultimate Edition:

This method requires the Google Plugin for Eclipse (GPE). You can find the instructions to download and install it here.

Here are the steps to setup a run/debug configuration using the embedded WildFly launcher:

  1. Ensure that your app is using the Google Web Toolkit. Right-click on your project in Eclipse, and select Google → Web Toolkit Settings, and make sure that the "Use Google Web Toolkit" box is checked and the appropriate GWT SDK is selected.


  2. Set up a Web Application run/debug configuration as follows:

    Create a new Web Application run/debug configuration, select your Errai app in the Project field and set com.google.gwt.dev.DevMode as the main class:


  3. In the Server tab, check the Run built-in server checkbox, and enter the port number 8888.


  4. Select Super Development Mode in the GWT tab, and add your GWT module under Available Modules.


  5. Under the Arguments tab, there are two fields, Program arguments and VM arguments. In the Program arguments field, amend the server flag as follows:

    Program Arguments

    -server org.jboss.errai.cdi.server.gwt.EmbeddedWildFlyLauncher

    Make sure the following VM arguments are set:

    VM arguments

    -Xmx2048m -XX:MaxPermSize=512M -Derrai.jboss.home=/home/ddadlani/errai-tutorial/target/wildfly-8.2.0.Final/

    where errai.jboss.home points to your own WildFly installation. This can either be your local WildFly installation directory, or in the target/ directory of your app. For the Errai Tutorial app, errai.jboss.home points to the WildFly installation within the target/ directory, which is redownloaded and installed as part of the build. See below for information on how to automatically download WildFly into the target/ directory prior to running Super Dev Mode.


That’s it! To debug your application within Eclipse, simply select your debug configuration and hit Debug. This will start GWT’s dev mode as well as the debugger within Eclipse.

The Ultimate Edition for Intellij IDEA comes with a built-in GWT plugin that allows you to run and debug GWT apps specifically. We can configure the plugin to use the embedded WildFly launcher for debugging ease, in order to debug both server and client-side code in one debug session. This section describes how to set up a GWT run/debug configuration within Intellij IDEA:

  1. If you have not already done so, add a GWT facet to your main module. Instructions to add a GWT facet to an existing module can be found here.
  2. On the top right hand corner of your Intellij IDEA session, select the dropdown box labeled Edit Configurations to create a new run/debug configuration.


  3. Press the + button under GWT Configurations to create a new GWT configuration. Check the Super Dev Mode checkbox and select the name of your module. Fill out the following parameters in the corresponding boxes:

    VM Options

    -Xmx2048m -XX:MaxPermSize=512M -Derrai.jboss.home=/home/ddadlani/errai-tutorial/target/wildfly-8.2.0.Final/

    where errai.jboss.home points to your WildFly installation directory. This can either be your local WildFly installation directory, or in the target/ directory of your app. For the Errai Tutorial app, errai.jboss.home points to the WildFly installation within the target/ directory, which is redownloaded and installed as part of the build. See below for information on how to automatically download WildFly into the target/ directory prior to running Super Dev Mode.

    Dev Mode Parameters

    -server org.jboss.errai.cdi.server.gwt.EmbeddedWildFlyLauncher


  4. Select the Default server and make sure the start page is index.html. Select your browser and check the with JavaScript debugger box to take advantage of Intellij IDEA’s built-in JavaScript tooling.
  5. Click on the + button under Before Launch and select the Make option. This will tell Intellij to compile your project before launching Dev Mode.

To run or debug your app, select this configuration in the top right corner of Intellij IDEA and click the Run or Debug buttons next to it. Your app should start up in Dev Mode within Intellij automatically and you should be able to use Intellij’s own debugger to debug your code.

Now that you have everything set up, it’s time to move on to coding!

Plugin Tip

Use the Errai Forge Addon Add Errai Features command and select Errai CDI to follow along with this section.

Errai CDI as its namesake implies is based on, and is in fact, a partial implementation of the CDI (Contexts and Dependency Injection) specification. Errai CDI covers most of the programming model but omits the CDI SPI, instead replacing it with it a custom set of APIs which are more appropriate for the client programming model of Errai.

These differences aside, using Errai CDI in conjunction with CDI on the server will provide you with a uniform programming model across the client and server code of your application.

This guide does not assume any past experience with CDI. However, you may wish to consider reading the Weld Documentation in addition to this guide.

Scopes, put simply, are the context within which beans live. Some scopes are short-lived and some are long-lived. For instance, there are beans which you may only want to create during a request, and beans which you want to live for as long as the application is running.

It turns out that CDI includes a set of default scopes which represent these very things.

We’ll start by taking a look at the application scope, which is lovingly represented by the annotation @ApplicationScoped. An application-scoped bean is a bean which will live for the entire duration of the application. In this sense, it is essentially like a singleton. And it’s generally okay to think of it in that way.

So let’s declare an application-scoped bean:

@ApplicationScoped

public class Bar {
  public String getName() {
    return "Mr. Bar";
  }
}

That was almost as easy as making the last bean. The difference between this bean and the last, is Bar will actually be instantiated by the container automatically, and Foo will not.

So what can we do with Foo ? Well, let’s go ahead and get familiar with dependency injection, shall we?

@ApplicationScoped

public class Bar {
  @Inject Foo foo;
  public String getName() {
    return "Mr. Bar";
  }
}

We have added a field of the type Foo which we declared earlier, and we have annotated it with javax.inject.Inject. This tells the container to inject an instance of Foo into our bean. Since our Foo bean is of the dependent scope, the bean manager will actually create a new instance of Foo and pass it in.

This scope of the newly instantiated Foo is dependent on the scope that it was injected into. In this case, the application scope. On the other hand, if we were to turn around an inject Bar into Foo, the behaviour is quite different.

public class Foo {

  @Inject Bar bar;
  public String getName() {
    return "Mr. Foo";
  }
}

Here, every time a new instance of Foo is created, the same instance of Bar will be injected. That is to say: this pseudo-code assertion is now always true:

assert fooInstance.bar.foo == fooInstance

In the case of an Errai application, there are a bunch of application scoped beans which come built-in for common services like ErraiBus. Thus, in an Errai application which uses the message bus, we can inject a handle to the MessageBus service into any of our beans. Let’s go ahead and do that in our Bar class:

@ApplicationScoped

public class Bar {
  @Inject Foo foo;
  @Inject MessageBus bus;
  public String getName() {
    return "Mr. Bar";
  }
}

If working with dependency injection is new to you, then this is where you’ll start seeing some practical benefit. When you need a common service in your client code, you ask the container for it by injecting it. This frees you from worrying about the proper APIs to use in order to access a service; we need to use the message bus in our Bar bean, and so we inject it.