EJB 3 : Eclipse + Maven 3 + Wildfly 9 (Tutorial 1)

 

How to configure an EJB 3 (specifically EJB 3.2) project using Eclipse, Maven 3, Java 8, and WildFly 9?

Settings summary :

  • 2 WildFly instances in the same machine, so this requires fixing offset and node names.
  • Eclipse Mars which requires JDK 8 (but programmers are evilish, they can find million workarounds for it…)
  • Eclipse Mars which requires downloading adapter for WildFly 9
  • Any Eclipse requires pointing on the right Maven 3 installation.

Tutorial 1 : Eclipse + Maven 3 + Wildfly 9 current-tutorial-icon-50 

Tutorial 2 : Session Beans

Tutorial 3 : Solution Design

Tutorial 4 : Session Beans and Business Interfaes Projects 

Tutorial 5 : Local View – Client and EJBs in the Same WAR

Tutorial 6 : Remote View – Client and EJBs in the Same EAR

Tutorial 7 : Remote View – Client and EJBs in Different WARs but Same Container

Tutorial 8 : Remote View – Client and EJBs in different Containers

Tutorial 9 : Standalone Client


reqs 1. Preparing Environment

Requirements :

1.1. JDK 8

To make Eclipse Mars work, you got to install JDK 8 and set the JAVA_HOME environment variable. Otherwise, you would get such errors about a list of modules which failed to load :

org.jboss.tools.foundation.core requires JavaSE version 1.8
org.jboss.ide.eclipse.classpath.ui requires JavaSE version 1.8

And you wouldn’t be able to create a WildFly nor a JBoss AS server.

1.2. WildFly 9 Applicantion Server

1.2.1. Add WildFly support in Eclipse

  1. Go to the tab “Servers”
  2. Click on “No servers are available. Click this link to create a new server…”
  3. Expand “Red Hat JBoss Middleware”
  4. Choose “JBoss AS, WildFly, & EAP Server Tools”
  5. Check “I accept the terms of the license agreement”.

1.2.2. Create WildFly instance in Eclipse

After restarting Eclipse, under the tab “Servers” :

  1. Right Click > “New” > “Server” > “JBoss Community” > “WildFly 9.x” 
  2. Choose a “Server name”, for example “WildFly  9.0.2.Final 1”
  3. Click on “Next”
  4. For “Home Directory” click on “Browse” and select the root directory of WildFly that you have extracted after downloading.

In case you had a previous version of Eclipse, for example Eclipse Luna, you wouldn’t get Wildfly 9.x but maybe Wildfly 8.x. And so after choosing a Wildly 9 version, Eclipse would show you a warning message :

This server type expects a version of 8.0 but the server directory is of version 9.0.2.Final. This combination may cause critical errors.

but don’t worry and if you keep worry, get Eclipse Mars which support Wildfly 9 and Wildfly 10 Experimental 🙂

1.2.3. Add a new WildFly instance in Eclipse

Under the tab “Servers” :

  1. Right Click > “New” > “Server” > “JBoss Community” > “WildFly 9.x” 
  2. Choose a “Server name”, for example “WildFly  9.0.2.Final 2”
  3. Click on “Next”
  4. Select “Create new runtime (next page)”Creating New Instance of Wildfly or JBoss from Eclipse
  5. For “Home Directory” click on “Browse” and select the root directory of the duplicated WildFly.

1.2.4. Set WildFly instances Offset

I don’t have my second laptop with me by the moment, so we will deploy the 2 WildFly instances in the same machine . For sure if we keep the default ports, starting the second WildFly server will fail. That is why we must set an offset for the duplicated WildFly server.

Tutorial Agreement :

  • Application Server  1 : For “WildFly  9.0.2.Final 1”, set the offset to “0”.
  • Application Server 2 :  For “WildFly  9.0.2.Final 2”, set the offset to “100”.

There are 2 ways to set an offset :

1. Either by editing the configuration file that you are using, for example standalone.xml. The default value is “0” :

So change it to “100” for “WildFly  9.0.2.Final 2” :

2. Or by passing this argument to standalone.bat program :

In Eclipse, you can double click “WildFly  9.0.2.Final 2”, then click on “Open launch configuration” :

Wildfly or JBoss Argument Configuration from Eclipse

And add “-Djboss.socket.binding.port-offset=100” in “Program arguments” :server-2-offset

1.2.5. Set WildFly instances different Node Names

And so for the same reason that we will have 2 instances of WildFly in the same machine we must fix different node names -Djboss.node.name :

1.2.6. Run WildFly instances

For each Wildfly server you created, click on “Start”. Check there is no error in the log. If you get this exception :

Caused by: java.net.BindException: Address already in use: bind 

You got then to verify if any software is using the WildFly default ports, such as 8080 and 9990 (idem for the shifted ports using offset). If you are using the default standalone.xml as configuration file for your WildFly instance, open it and scroll it down till you reach the Socket Binding Groups:

In this section, you will find all the ports used by your WildFly 9 instance. You can use CurrentPort tool to kill the responsible process. If you see that process is important for you, then define different offset for WildFly.

As for WildFly 9.0.2.Final , the default ports without offset are : 9990, 9993, 8009, 8080, 8443, 4712, 4713.

1.3. Maven 3

Go to the task “Windows” > “Preferences” > “Maven” > “Installations” > “Add”, then browse to the directory where you extracted Maven that you downloaded.

Then go to “Windows” > “Preferences” > “Maven” > “User Settings”. For me I keep it simple and for both “Global Settings” and “User Settings” I browse to the Maven installation directory and then to the relative subdirectory \conf where I select the file settings.xml. You can optionally change the default maven repository by adding this line under <settings> tag :


2. Maven Configuration

2.1. Compilation Java Version

Setting Java Compiler Version for every project

For every project in our solution you must specify the Java Compiler version by adding to the build description this plugin in pom.xml :

2.2. The Age of Annotations

People who are working on the new Java EE specifications apparently don’t like much XML. So they tend to replace it by Annotations. However, an XML file wins over Annotations. That means if you describe something in a standardized XML file and conflict it using Annotation, it’s the XML description which will be taken into consideration.

However, sometimes Annotations sound just like “hardcoded” settings, mixed in Java classes, not externalized in XML files…

2.2.1. No more web.xml required

One of those standardized XML files is web.xml whose default location in a mavenized project is src > main > webapp > WEB-INF. Once the WAR project packaged, it will be under /WEB-INF. But, since Servlet Specification 3.0 we don’t need web.xml anymore.

But if we create a WAR project, we see Maven still doesn’t like a web application without a web.xml showing this error:

failOnMissingWebXml - Maven Configuration Error

So, let’s just get rid of it and add this Maven Plugin into build section of pom.xml for every WAR project we create :

Following that, you would get this error :

Maven-Update

So just right click on your project and click on option “Maven > Project Update”. Do it every time you get such error.

Remark : In case you forgot to set the Java Compiler Version, you would get this error :

Maven Java EE Configuration Error - Java Version

2.2.2. No more ejb-jar.xml required

Since EJB 3.0, no more ejb-jar.xml required for declaring the EJBs. That was replaced by Annotations. However, you still can use, for example if you want to override the EJB module name. By default it is the JAR/War name without extension, in our case it is : ejb3-server.

2.3. Maven Dependency for Supporting EJB 3.2

Now the good news are… to add EJB 3.2 support for our project we do need only one single dependency in our pom.xml :

Here we have highlighted 2 important lines.

For the scope, we don’t want to add the artifact javaee-api into the WAR file ejb3-server-war because we are expecting it to be provided by our EJB Container WildFly. The good thing is that the application server WildFly has full support of Java EE 7 since its version 8.0.0.Final. That is why we could use version 7.0.

Try to expand javaee-api-7.0.jar in Eclipse, here they are the EJB related packages :

Java EE JAR content and EJB 3 packages

Sure this JAR brings us more awesome things :p such as web service annotations…

Let’s make sure that our WildFly will provide us by EJB 3.2 classes,… and here you are :

If we check the content jboss-ejb-api_3.2_spec-1.0.0.Final.jar we will find the same packages :

EJB API provided by Wildfly

 


Did you like the article? Have you got a question? Do you need more details? Put it in a comment.
If no questions, then move to the next tutorial about  Session Beans.
Good luck learning EJB 3!