Skip to main content

EJB Creation and Consumption with NetBeans and JBoss

Posted by ddevore on January 10, 2006 at 8:02 AM PST

At the time of writing this guide NetBeans IDE was in version 5.0 Beta. This guide was written using the latest development build #200601052030 to take advantage of the bug fixes since NetBeans IDE 5.0 went into beta. Also, NetBeans officially supports JBoss Application Server 4.0.3 so this guide will walk through the JBoss Application Server 4.0.3sp1 setup. If you are using a previous version of JBoss I suggest switching to 4.0.3sp1 because of some support changes in JBoss for Web Services which will be discussed when I get to Web Services. If you have not setup NetBeans to work with JBoss please see NetBeans with JBoss Setup.

Creating Projects

Now that you have NetBeans setup with JBoss you can create a project to create and consume EJBs. The EJB Module is the only project which NetBeans will allow you to create EJBs. There is an Enterprise Application project which by default creates an EJB Module and Web Module, used for Web Services and pages, as sub projects. Since most project now days will not have only EJBs this guide will use the Enterprise Application project as the main project, though you can use the same methods for a project without the use of the Enterprise Application project.

Creating a Enterprise Application Project


To create an Enterprise Application either right click in the project view and select New Project or from the main menu select File | New Project. Both will bring up the New Project dialog. In this dialog select the Enterprise node and Enterprise Application from the list of projects on the right and select Next. In the next screen enter the project name, location and server. Because of the incomplete implementation of JAX-RPC in JBoss NetBeans will not create a web service with the JBoss server as the target. So to create an Enterprise Application project which will have Web Services the target server of the Web Module of the Enterprise Application must be the Sun Java System Application Server this can be changed later. If you choose the JBoss server at this stage the target server will have to be changed in the Web Module to create Web Services. You also have the opportunity to change the name of the EJB Module and Web Module which will be created with this project. For this guide I am leaving the default names for these modules and naming the project NBJBossApplication. Once this is done click Finish. This will create the project and open them up in the projects tab see Figure 1 below.

projectview1.jpg


Figure 1

You can see in Figure 1 above that NetBeans created not only the NBJBossApplication project but the NBJBossApplication-EJBModule and NBJBossApplication-WebModule as well. The directory structure of the NBJBossApplication project with the sub projects can be seen in Figure 2 below.

fileview1.jpg


Figure 2

As you can see by simply looking at the file layout in Figure 2 Enterprise Application projects are a very good way of organizing large applications which is why I am using them in this guide. A complete discussion of the Enterprise Application project is outside the scope of this guide and will not be discussed here.

Creating a EJB Module


You create an EJB Module the same way as the Enterprise Application. Right click in the project view and select New Project or from the main menu select File | New Project. Both will open the New Project dialog. In this dialog select the Enterprise node on the left and then the EJB Module on the right and click Next. Enter the project name, location and server. This also allows you to add this module to an Enterprise Application project if you wish. This can be very useful if you are making an enterprise project with all components contained in the same parent project. If you are adding this project to an Enterprise Application you do not need to put it in the Enterprise Application directory though it may prove easier to put it in the same directory for organization.

Working with EJBs and JBoss


Working with EJBs in NetBeans for JBoss is not really any different from working with any other server. You create and consume them the same way as any other targeted server. When I am developing both EJBs and Web Services I usually use the Sun Java System Application Server as the target and deploy the project manually, mostly because NetBeans will not compile a Web Service correctly with the JBoss server as the target.

Creating EJBs


To create an EJB you can use either the New | File/Folder from the main menu or use the context menu for the EJB Module by right clicking on the EJB Module. You can also select the specific bean type from the EJB Module, if it is in the list under the New option. If you selected the New File option you will get the New File dialog, select the Enterprise node on the left and the bean type on the right. This guide will walk through the creation of a Session Bean, though we will not be creating other beans they are created in the same manner though the information in the wizards is slightly different.

Session Beans


In the New Session Bean dialog enter the EJB name and the package name. You can also select the source location if you have multiple source locations setup in the project. Mark the correct session type and the interface types to create. If you are not going to be calling this bean from a remote server you only need to select the local interface, though I usually select both remote and local interfaces for flexibility just in case. For the guide I created a stateless EJB with the name NBJBossSession in the package com.nbjboss.ejb.session with both remote and local interfaces. This gives the following structure in the Projects view see Figure 3.

sessionejb1.jpg


Figure 3

As you can see this creates all the files needed for a remote interface as well as a local interface. You will also notice a NBJBossSessionSB node under the Enterprise Beans node with Remote Methods and Local Methods nodes. These nodes allow you to create new business methods for both the remote and local interfaces. That is all there is to creating an EJB.

Working with EJBs


Once you have an EJB created expand the Enterprise Beans node for the module you created the bean in. You will see the bean you created if you then expand the node you will see the interfaces you created for the bean. You can add a business method to the bean by right clicking on the bean and select Add | Business Method. This will bring up a dialog for adding the method information. Enter the method name, return type, which interface to add it to, and any parameters and exceptions for the method. For this example I used the simple sayHello business method with a String return type and String parameter named name. As you can see in Figure 4 it added the business method to the remote and local interfaces for me.

sessionejb2.jpg


Figure 4

You can access the business method by double clicking on the method name in either the remote or local interfaces and entering the logic, both of these nodes take you to the same file. The following is the code I added to the method for the sayHello logic.

    public String sayHello(java.lang.String name) {

        return "Hello " + name;

    }

Code 1

As you can see I simply returned the input name concatenated onto Hello.

Deploying and Testing


Once you have the logic you need in the EJB build the project to check for any errors, deploying will automatically build the module also. If you have JBoss setup as the target server you can right click on the module and select deploy to deploy it directly to JBoss.

Deploying


Before you deploy the first time you want to check the target server to insure the correct server is selected. If you followed the setup guide for NetBeans with JBoss the target server will likely be the Sun Java System Application Server and will need to be changed. To do this you need to right click on the EJB Module and select properties. Then select the Run node and make sure the server dropdown is set to the correct JBoss server see Figure 5.

targetserver.jpg


Figure 5

Once this is done you need to right click on the EJB Module and select Deploy Project. The server will be started and the project will be deployed. If the server is running outside of NetBeans you will get a deployment error so make sure you do not have a server running outside of NetBeans. If all goes well you can go to the server and get the default JBoss screen. If you are running locally use localhost:portfor the address. The Output tab on the bottom of NetBeans will also allow you to start, start in debug, restart, stop and refresh on the server at the appropriate times by using the icons on the left of the output tab for the target server.

Testing


For testing the EJB you will need to have something to call it. I will do it from a web page within a web application which will be a part of the NBJBossApplication project used as the parent to the NBJBossApplication-EJBModule. So create a Web Application by selecting New Project and selecting the Web node on the left of the dialog and Web Application on the right and select Next. Then enter the name NBJBossTestWebApplication, set the location to the NBJBossApplication directory and add it to the NBJBossApplication project and select Finish see Figure 6.

testwebapp.jpg


Figure 6

You will then have the default web application module created and opened as a project and need to make a call to the EJB. There are 2 ways NetBeans will write the code to cal the EJB for you. The first is through a Service Locator pattern from the J2EE design patterns. The second is to create a class to call the service from, right click in the class and select Enterprise Resource | Call Enterprise Bean. In the dialog select the NBJBossSessionBean, select remote or local interface and click OK. This will create the required code to do a lookup on the bean you selected. To call this from a JSP you will have to either add a wrapper method or change the method to public. The second method is only available to the EJB module project or a project within the same Enterprise Project. For this guide we will look at the first method though I included the JSP code for the second method below. Both methods use the remote interface to access the EJB.


Create a Service Locator by right clicking on the NBJBossTestApplication node and selecting New | File/Folder. On the right side select Enterprise and Service Locator from the list on the right. Name the Service Locator ServiceLocator and make the package com.nbjboss.util and click Finish see Figure 7.

servicelocator.jpg


Figure 7

Next you need to enter the code into the JSP. To do this Expand the project and the Web Pages node. You will see an index.jsp page there, open this and enter the following code just above the </body> tag. You may want to change the parameter from Dru unless your name is Dru also.

    <h4>Calling NBJBossSessionBean sayHello()</h4>

    <%

        com.nbjboss.util.ServiceLocator sl =

            new com.nbjboss.util.ServiceLocator();

        com.nbjboss.ejb.session.NBJBossSessionRemote bean =

                ((com.nbjboss.ejb.session.NBJBossSessionRemoteHome)

                sl.getRemoteHome("java:comp/env/ejb/NBJBossSessionBean",

                com.nbjboss.ejb.session.NBJBossSessionRemoteHome.class)).create();

        out.println(bean.sayHello("Dru"));

    %>

Code 2

Before you build the project you will need to add the EJB Module to the Libraries. To do this right click on the project and select Properties and select the Libraries node and click Add Project on the right. Browse to the EJB project and select Add Project JAR Files. If you are going to deploy this and the EJB Module to the same server you will need to uncheck the package button.

For the second method add the following code to the JSP and create a class as mentioned above.

    <h4>Calling NBJBossSessionBean sayHello()</h4>

    <%

        com.nbjboss.util.ServiceLookup sl =

            new com.nbjboss.util.ServiceLookup();

        com.nbjboss.ejb.session.NBJBossSessionRemote bean =

                sl.lookupNBJBossSessionBean();

        out.println(bean.sayHello("Dru"));

    %>

Code 3

Now we have the bean being called we need to get it deployed but we need to set the libs correctly. To do this right click on the project and select Properties in the properties dialog select the libraries node and uncheck the checkbox next to the NBJBossApplication-EJBModule. This will tell NetBeans not to include the EJB Module with the war file created for the test application if it is included you will get a class cast exception. Now if the EJB Module is still deployed simply deploy the NBJBossTestApplication. If not then deploy the NBJBossApplication-EJBModule first then deploy the Test Application. Another option is to deploy them both with the NBJBossApplication parent project. This is possible if the EJB Module and the Test Application are both included in the Enterprise Application project. To check to see if they are right click on the NBJBossApplication and select Properties then select the Packaging node under the Build node. This will list all the projects which are part of the parent project. Now point your browser to the server for the Test Application. If you have followed this guide and the server is local with port number 8080 then the address will be http://localhost:8080/NBJBossTestWebApplication/ make sure the port is correct for your installation.

Conclusion


NetBeans has setup the easiest way of working with EJBs of any IDE that I have seen and with the built in support for the servers it towers above any other IDE. Once you start working with EJBs in NetBeans you will see that the development is easy and consuming them is even easier. With the work that NetBeans has done with writing the code and XML for you it allows you the time to do the real work.


You can download the source for this project here.

NOTE: If you deployed the individual modules through NetBeans and attempt to deploy the Enterprise Project you will get an error. This is due to JBoss wanting to deploy the individual modules with the Enterprise Project which will cause duplicate deployments. To fix this go to the deploy directory of the server you are using and delete the module files before deploying the Enterprise Project. Also, please note that this will work if you are deploying to Sun Java System Application Server. All you need to do is to change the target to the Sun Java System Application Server and deploy.

Related Topics >>