Author: Jaikiran Pai, Mike Musgrove
Level: Intermediate
Technologies: EJB, JNDI
Summary: The ejb-remote
quickstart uses EJB and JNDI to demonstrate how to access an EJB, deployed to WildFly, from a remote Java client application.
Target Product: WildFly
Source: https://github.com/wildfly/quickstart/
The ejb-remote
quickstart shows how to access an EJB from a remote Java client application. It demonstrates the use of EJB and JNDI in WildFly Application Server.
There are two components to this example:
The server component is comprised of a stateful EJB and a stateless EJB. It provides both an EJB JAR that is deployed to the server and a JAR file containing the remote business interfaces required by the remote client application.
The remote client application depends on the remote business interfaces from the server component. This application looks up the stateless and stateful beans via JNDI and invokes a number of methods on them.
Each component is defined in its own standalone Maven module. The quickstart provides a top level Maven module to simplify the packaging of the artifacts.
The application this project produces is designed to be run on WildFly Application Server 11 or later.
All you need to build this project is Java 8.0 (Java SDK 1.8) or later and Maven 3.3.1 or later. See Configure Maven for WildFly 11 to make sure you are configured correctly for testing the quickstarts.
In the following instructions, replace WILDFLY_HOME
with the actual path to your WildFly installation. The installation path is described in detail here: Use of WILDFLY_HOME and JBOSS_HOME Variables.
For Linux: WILDFLY_HOME/bin/standalone.sh
For Windows: WILDFLY_HOME\bin\standalone.bat
Since this quickstart builds two separate components, you can not use the standard Build and Deploy commands used by most of the other quickstarts. You must follow these steps to build, deploy, and run this quickstart.
mvn clean install
server-side/target/ejb-remote-server-side.jar
. You can check the WildFly server console to see information messages regarding the deployment.
mvn wildfly:deploy
cd ../client
mvn clean compile
mvn exec:exec
Note: This quickstart requires quickstart-parent
artifact to be installed in your local Maven repository. To install it, navigate to quickstarts project root directory and run the following command:
mvn clean install
When the client application runs, it performs the following steps:
The output in the terminal window will look like the following:
Obtained a remote stateless calculator for invocation
Adding 204 and 340 via the remote stateless calculator deployed on the server
Remote calculator returned sum = 544
Subtracting 2332 from 3434 via the remote stateless calculator deployed on the server
Remote calculator returned difference = 1102
Obtained a remote stateful counter for invocation
Counter will now be incremented 5 times
Incrementing counter
Count after increment is 1
Incrementing counter
Count after increment is 2
Incrementing counter
Count after increment is 3
Incrementing counter
Count after increment is 4
Incrementing counter
Count after increment is 5
Counter will now be decremented 5 times
Decrementing counter
Count after decrement is 4
Decrementing counter
Count after decrement is 3
Decrementing counter
Count after decrement is 2
Decrementing counter
Count after decrement is 1
Decrementing counter
Count after decrement is 0
Logging statements have been removed from this output here to make it clearer.
The remote client application can also be built as a standalone executable JAR with all of its dependencies.
cd client
mvn clean package assembly:single
You can then run the executable JAR using the java -jar
command. You will see the same console output as above.
java -jar target/ejb-remote-client-jar-with-dependencies.jar
To undeploy the server-side component from the WildFly server:
cd ../server-side
mvn wildfly:undeploy
You can also start the server and deploy the quickstarts or run the Arquillian tests from Eclipse using JBoss tools. For general information about how to import a quickstart, add a WildFly server, and build and deploy a quickstart, see Use JBoss Developer Studio or Eclipse to Run the Quickstarts.
This quickstart consists of multiple projects, so it deploys and runs differently in JBoss Developer Studio than the other quickstarts.
Install the required Maven artifacts and deploy the server side of the quickstart project.
ejb-remote-server-side
project and choose Run As
--> Maven Install
.ejb-remote-server-side
project and choose Run As
--> Run on Server
.Build and run the client side of the quickstart project.
ejb-remote-client
project and choose Run As
--> Java Application
.Select Java Application
window, choose RemoteEJBClient - org.jboss.as.quickstarts.ejb.remote.client
and click OK
.Console
window.To undeploy the project, right-click on the ejb-remote-server-side
project and choose Run As
--> Maven build
. Enter wildfly:undeploy
for the Goals
and click Run
.
If you want to debug the source code of any library in the project, run the following command to pull the source into your local repository. The IDE should then detect it.
mvn dependency:sources