= ejb-remote: Remote EJB Client Example
:author: Jaikiran Pai, Mike Musgrove
:productName: WildFly
:productNameFull: WildFly Application Server
:jbossHomeName: WILDFLY_HOME
:productVersion: 12
:buildRequirements: Java 8.0 (Java SDK 1.8) or later and Maven 3.3.1 or later
include::../shared-doc/attributes.adoc[]

:level: Intermediate
:technologies: EJB, JNDI
:source: {githubRepoUrl}

[abstract]
The `ejb-remote` quickstart uses EJB and JNDI to demonstrate how to access an EJB, deployed to {productName}, from a remote Java client application.

:standalone-server-type: default
:archiveType: jar
// Override the archive name
:archiveDir: {artifactId}/server-side/target
:archiveName: {artifactId}-server-side

== What is it?

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 {productNameFull}.

There are two components to this example:

. A server side component:
+
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.

. A remote client application that accesses the server component.
+
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.

//*************************************************
// Add System Requirements
//*************************************************
// == System Requirements
include::../shared-doc/system-requirements.adoc[leveloffset=+1]

//*************************************************
// Add Use of JBoss Home Name
//*************************************************
// == Use of {jbossHomeName}
include::../shared-doc/use-of-jboss-home-name.adoc[leveloffset=+1]

//*************************************************
// Start the server with the default profile
//*************************************************
// == Start the {productName} Standalone Server
include::../shared-doc/start-the-standalone-server.adoc[leveloffset=+1]

//*************************************************
// Install the quickstart parent artifact
//*************************************************
// == Install the Quickstart Parent Artifact in Maven
include::../shared-doc/install-quickstart-parent-artifact.adoc[leveloffset=+1]


//*************************************************
// Build and deploy the quickstart JAR
//*************************************************
// == Build and Deploy the Quickstart
include::../shared-doc/build-and-deploy-the-quickstart.adoc[leveloffset=+1]

== Build and Run the Client Application

. Navigate to the client subdirectory.
+
----
$ cd ../client
----

. Compile the client code.
+
----
$ mvn clean compile
----

. Execute the client application within Maven.
+
----
$ mvn exec:exec
----

== Investigate the Console Output

When the client application runs, it performs the following steps:

. Obtains a stateless session bean instance.
. Sends method invocations to the stateless bean to add two numbers, and then displays the result.
. Sends a second invocation to the stateless bean subtract two numbers, and then displays the result.
. Obtains a stateful session bean instance.
. Sends several method invocations to the stateful bean to increment a field in the bean, displaying the result each time.
. Sends several method invocations to the stateful bean to decrement a field in the bean, displaying the result each time.

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.

[[build_and_run_the_quickstart_as_an_executable_jar]]
== Build and Run The Quickstart as an Executable JAR

The remote client application can also be built as a standalone executable JAR with all of its dependencies.

. Open a terminal and navigate to the ejb-remote/client quickstart directory
+
----
$ cd client
----

. Type the following in the command line:
+
----
$ 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.
+
[source,subs="attributes+",options="nowrap"]
----
$ java -jar target/{artifactId}-client-jar-with-dependencies.jar
----

== Undeploy the Archive

To undeploy the server-side component from the {productName} server:

. Navigate to the server-side subdirectory:
+
----
$ cd ../server-side
----

. Type the following command:
+
----
$ mvn wildfly:undeploy
----

== Using EJB over HTTP

You can use HTTP as the transport for remote EJB invocations by specifying `-Dhttp=true` on both the Maven and command line based execution. This should not change the output, but it will use EJB over HTTP for the invocations. This means that each EJB request maps to a single HTTP request (actually HTTP/2 by default, if it is enabled on the server).

Before you can use it, you need to set up a user on the server as HTTP does not support transparent authentication. The next section describes how to xref:add_the_application_user[add the authorized application user] so you can test the quickstart using HTTP as the transport.

//*************************************************
// Add the Authorized Application User
//*************************************************
// == Add the Authorized Application User
include::../shared-doc/add-application-user.adoc[leveloffset=+1]

//*************************************************
// Add JBoss Developer Studio instructions
//*************************************************
// == Run the Quickstart in Red Hat JBoss Developer Studio or Eclipse
include::../shared-doc/run-the-quickstart-in-jboss-developer-studio.adoc[leveloffset=+1]

// Additional JBoss Developer Studio instructions
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.

** Right-click on the *{artifactId}-server-side* project and choose *Run As* -> *Maven Install*.
** Right-click on the *{artifactId}-server-side* project and choose *Run As* -> *Run on Server*.
. Build and run the client side of the quickstart project.

** Right-click on the *{artifactId}-client* project and choose *Run As* -> *Java Application*.
** In the *Select Java Application* window, choose *RemoteEJBClient - org.jboss.as.quickstarts.ejb.remote.client* and click *OK*.
** The client output displays in the *Console* window.
. To undeploy the project, right-click on the *{artifactId}-server-side* project and choose *Run As* -> *Maven build*. Enter `wildfly:undeploy` for the *Goals* and click *Run*.

== Debug the Application

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
----
