Calling a remotely deployed EJB from a client application

Author: Nico Coetzee <nicc777 at gmail dot com> Date: 26 June 2010

This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

Table of Contents
Problem Statement.....................................................................................................................................2 Implementation Overview..........................................................................................................................2 The EJB Project.........................................................................................................................................3 Step 1: Define the new project in NetBeans..........................................................................................3 Step 2: Define your package.................................................................................................................4 Step 3: Add a new EJB..........................................................................................................................5 Step 4: Add the business function ........................................................................................................6 Step 5: Add the business function logic (source code)..........................................................................7 Step 6: Compile and deploy..................................................................................................................8 The client application.................................................................................................................................9 Step 1: Define the new project in NetBeans..........................................................................................9 Step 2: Add required files to the project..............................................................................................10 Step 3: Add the source code to the “Main” method............................................................................11 Step 4: RUN the application ...............................................................................................................12

Problem Statement
Deploy EJB business functions in a remote Application Server (JBoss) and call the remote EJB's from a local client. The test was conducted using the following software: Operating System: JBoss version: Editor: JDK: Windows 7 Ultimate 64bit 5.1.0 Final (Released 2009-05-23) NetBeans 6.9 RC2 (June 2010) Version 1.6.0 update 20

Implementation Overview
The following projects will be created in NetBeans: 1. A EJB Project which will be deployed to the remote JBoss Application Server1 2. A local plain Java Project calling the remote EJB service The invocation of the EJB can be confirmed by looking at the “JMX MBean View”. An example of the output can be seen below:

1 For the purpose of the test, the JBoss server is actually deployed on the same host as the test client

The EJB Project
Step 1: Define the new project in NetBeans
Open the NetBeans editor and follow these steps: 1. Select “File” | “New Project...” from the menu options 2. In the dialog box, select “Java EE” from the “Categories” list and “EJB Module” from the “Projects” list and click the “Next” button 3. Set the “Project Name” to “myMathEJB” and click the “Next” button 4. Ensure the JBoss server is selected in the server drop down list (You may need to add the JBoss server manually if it's not in the list. Refer to the NetBeans documentation on how to do this2) 5. Click on the “Finish” button 6. The new project will now be created:

2 For JBoss servers on a different physical host, refer to the JBoss documentation on how to deploy your new JAR file.

Step 2: Define your package
1. Right click on “Source Packages” and select the option “New” | “Java Package...” 2. In the “Package Name” field enter “test.ejb” and click the “Finish” button

Step 3: Add a new EJB
1. Right click on the package name “test.ejb” and select “New” | “Session Bean...”. If the option is NOT available, select “New” | “Other...” and then “Java EE” from the “Categories” list and “Session Bean” from the “File Types” list. Finally click the “Next” button. 2. Set the “EJB Name” field to “mathAdderBean” 3. The “Session Type” must be “Stateless” 4. The “Create Interface” section must have only “Remote” selected and “myMathEJB” in the drop down list 5. Click the “Finish” button

Step 4: Add the business function
1. In the source code, right click in line 19 and select “Insert Code...” 2. Select “Add Business Method...” from the list 3. In the “Name” field, enter “simpleAdder” 4. The “Return type” field must be “int” 5. We will now add parameters. (a) Click on the “Add” button (b) The “Name” field must be “n1” (c) In the “Type” field, select “int” from the drop down list 6. Repeat step 5 to add a “n2” parameter 7. Click the “Ok” button when done. 8. Save the project The dialog box with all completed fields:

The source code with the method added:

Step 5: Add the business function logic (source code)
Modify the “simpleAdder” method to look like the following code snippet:

Step 6: Compile and deploy
1. Right click on “myMathEJB” (the project name in the “Projects” list) and select “Clean and Build” 2. Wait until you are sure the build is complete 3. Again, right click on “myMathEJB” and select “Deploy” You should now see the following entry in the JBoss logs:
2010-06-26 22:34:58,496 INFO [org.jboss.ejb3.deployers.Ejb3DependenciesDeployer] (HDScanner) Encountered deployment AbstractVFSDeploymentContext@3882344{vfszip:/C:/opt/jboss5.1.0.GA/server/default/deploy/myMathEJB.jar/} 2010-06-26 22:34:58,496 INFO [org.jboss.ejb3.deployers.Ejb3DependenciesDeployer] (HDScanner) Encountered deployment AbstractVFSDeploymentContext@3882344{vfszip:/C:/opt/jboss5.1.0.GA/server/default/deploy/myMathEJB.jar/} 2010-06-26 22:34:58,497 INFO [org.jboss.ejb3.deployers.Ejb3DependenciesDeployer] (HDScanner) Encountered deployment AbstractVFSDeploymentContext@3882344{vfszip:/C:/opt/jboss5.1.0.GA/server/default/deploy/myMathEJB.jar/} 2010-06-26 22:34:58,497 INFO [org.jboss.ejb3.deployers.Ejb3DependenciesDeployer] (HDScanner) Encountered deployment AbstractVFSDeploymentContext@3882344{vfszip:/C:/opt/jboss5.1.0.GA/server/default/deploy/myMathEJB.jar/} 2010-06-26 22:34:58,504 INFO [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner) Created KernelDeployment for: myMathEJB.jar 2010-06-26 22:34:58,504 INFO [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner) installing bean: jboss.j2ee:jar=myMathEJB.jar,name=mathAdderBean,service=EJB3 2010-06-26 22:34:58,505 INFO [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner) with dependencies: 2010-06-26 22:34:58,505 INFO [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner) and demands: 2010-06-26 22:34:58,505 INFO [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner) jboss.ejb:service=EJBTimerService 2010-06-26 22:34:58,505 INFO [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner) and supplies: 2010-06-26 22:34:58,505 INFO [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner) jndi:mathAdderBean/remote 2010-06-26 22:34:58,505 INFO [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner) jndi:mathAdderBean/remote-test.ejb.mathAdderBeanRemote 2010-06-26 22:34:58,506 INFO [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner) Class:test.ejb.mathAdderBeanRemote 2010-06-26 22:34:58,506 INFO [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner) Added bean(jboss.j2ee:jar=myMathEJB.jar,name=mathAdderBean,service=EJB3) to KernelDeployment of: myMathEJB.jar 2010-06-26 22:34:58,507 INFO [org.jboss.ejb3.endpoint.deployers.EJB3EndpointDeployer] (HDScanner) Deploy AbstractBeanMetaData@60d136{name=jboss.j2ee:jar=myMathEJB.jar,name=mathAdderBean,service=EJB3_endpoint bean=org.jboss.ejb3.endpoint.deployers.impl.EndpointImpl properties=[container] constructor=null autowireCandidate=true} 2010-06-26 22:34:58,550 INFO [org.jboss.ejb3.session.SessionSpecContainer] (HDScanner) Starting jboss.j2ee:jar=myMathEJB.jar,name=mathAdderBean,service=EJB3 2010-06-26 22:34:58,551 INFO [org.jboss.ejb3.EJBContainer] (HDScanner) STARTED EJB: test.ejb.mathAdderBean ejbName: mathAdderBean 2010-06-26 22:34:58,557 INFO [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] (HDScanner) Binding the following Entries in Global JNDI: mathAdderBean/remote - EJB3.x Default Remote Business Interface mathAdderBean/remote-test.ejb.mathAdderBeanRemote - EJB3.x Remote Business Interface

The EJB deployment is now ready to be used by a client application.

The client application
The client application is a simple command line application, very similar to the familiar “Hello world” applications many new developers start off with.

Step 1: Define the new project in NetBeans
Open the NetBeans editor and follow these steps: 1. Select “File” | “New Project...” from the menu options 2. In the dialog box, select “Java” from the “Categories” list and “Java Application” from the “Projects” list and click the “Next” button 3. In the “Project Name” field, type “myMathApplication” and click on the “Finish” button

Step 2: Add required files to the project
1. Right click on “myMathApplication” in the “Projects” list and select “Properties” 2. Select “Libraries” from the “Categories” list 3. Click on the “Add JAR/Folder” button 4. Navigate to your “myMathEJB.jar” file and add it to the list 5. Repeat step 4 to add the following JBoss JAR files (in the “client” folder”)3: (a) jboss-client.jar (b) jnp-client.jar (c) jboss-logging-spi.jar (d) jboss-ejb3-proxy-impl-client.jar (e) jboss-ejb3-common-client.jar (f) jboss-remoting.jar (g) jboss-aop-client.jar (h) jboss-aspect-jdk50-client.jar (i) jbossall-client.jar 6. When done, click the “Ok” button 7. Wait until the project has canned all files

3 This is what it took to get it run on my system. I'm not 100% convinced it's correct though. Some warnings will be generated.

Step 3: Add the source code to the “Main” method
Modify the source code to reflect the following:

Step 4: RUN the application
You should now be able to click the “Run” icon on the NetBeans toolbar. The following output will be generated:

You can also verify in your “JMX MBean View” window that the bean was called.

Sign up to vote on this title
UsefulNot useful