EJB with NetBeans and Wildfly – part 4

In this part of tutorial, we will build a client application which makes use of our EJB. As a reminder, in previous part of this series, we gave brief introduction to EJBs, created entities and persistence objects, and implemented business logic for application. Now, it’s time to make use of our logic.

Create client application

In NetBeans, we will create EJB client application. To do this, choose New->Project, then select JavaEE in left list, and Enterprise Application Client on the right, like shown in the image bellow:

ejb-client-wiz1

Click n”Next”, and in the next screen, enter ejb-money-client as application name.

ejb-client-wiz2

Click “Next” again, and in the final wizard screen, select pre-configured WildFly application server.

ejb-client-wiz3

Click “Finish”, and NetBeans will generate all files needed for application.

After this is done, we will need to add our peviously developed EJB component as a dependency to client project, so we are able to access EJB interfaces. Right click client project, and select “Properties”. In the left-hand side, select Libraries node. Click Add Project button, and then select ejb-money-03 project. It will be added as dependency.

ejb-client-deps

Coding the client

The first thing we need to do is create some configuration for JNDI lookup. If you’re not familiar with JNDI, check out this Oracle tutorial. JNDI is used to lookup objects based on some predefined name. Since we will use JNDI to access our EJBs, we first need to provide some configuration. Create a file called jndi.properties  in client project and add the following entries to it:

The first step is to load this configuration and obtain JNDI context. Using this context, we can get remote interface implementation. This is done in the following code snippet:

 

Here, we first use static initializer to create JNDI InitialContext . It is initialized with properties specified in jndi.properties  file we created earlier.

We also define method lookupAccountBean , in which we use JNDI context to lookup the bean by it’s JNDI name. WildFly provides several JNDI entries for each deployed bean. You can check out the log when you deploy the bean to WildFly, and you will see that it shows JNDI names for deployed beans. For example, something like this:

Using bean methods

After we obtain the bean reference through JNDI, it si trivial to invoke it’s business logic. It is being invoked like any Java object method. For example, to create new account, we can do something like this:

To deposit money to account, we can use the following code:

From this simple example, you can see the basic principle of accessing remote EJBs and invoking their methods. If you didn’t have the chance, take a look at previous posts of this series for more in-depth look at developing Enterprise Java Beans.

Source code for this example can be found at Github.

Related posts

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *