Author: Wolf-Dieter Fink
Level: Advanced
Technologies: EJB, EAR
Summary: EJB applications deployed to different servers that communicate via EJB remote calls
Target Product: EAP
Product Versions: EAP 6.1, EAP 6.2
Source: https://github.com/jboss-developer/jboss-eap-quickstarts/
This quickstart demonstrates communication between applications deployed to different servers. Each application is deployed as an EAR and contains a simple EJB3.1 bean. The only function of each bean is to log the invocation.
This example consists of the following Maven projects, each with a shared parent:
Sub-project | Description |
---|---|
app-main |
An application that can be called by the client . It can also call the different sub-applications. |
app-one and app-two |
These are simple applications that contain an EJB sub-project to build the ejb.jar file and an EAR sub-project to build the app.ear file. Each application contains only one EJB that logs a statement on a method call and returns the jboss.node.name and credentials. |
app-web |
A simple WAR application. It consists of one Servlet that demonstrates how to invoke EJBs on a different server. |
client |
This project builds the standalone client and executes it. |
The root pom.xml
builds each of the subprojects in an appropriate order.
The server configuration is done using CLI batch scripts located in the root of the quickstart folder.
The application this project produces is designed to be run on Red Hat JBoss Enterprise Application Platform 6.1 or later.
All you need to build this project is Java 6.0 (Java SDK 1.6) or later, Maven 3.0 or later.
If you have not yet done so, you must Configure Maven before testing the quickstarts.
The following users must be added to the ApplicationRealm
to run this quickstart. Be sure to use the names and passwords specified in the table as they are required to run this example.
UserName | Realm | Password | Roles |
---|---|---|---|
quickuser | ApplicationRealm | quick-123 | leave blank for none |
quickuser1 | ApplicationRealm | quick123+ | leave blank for none |
quickuser2 | ApplicationRealm | quick+123 | leave blank for none |
To add the users, open a command prompt and type the following commands:
For Linux:
JBOSS_HOME/bin/add-user.sh -a -u quickuser -p quick-123
JBOSS_HOME/bin/add-user.sh -a -u quickuser1 -p quick123+
JBOSS_HOME/bin/add-user.sh -a -u quickuser2 -p quick+123
For Windows:
JBOSS_HOME\bin\add-user.bat -a -u quickuser -p quick-123
JBOSS_HOME\bin\add-user.bat -a -u quickuser1 -p quick123+
JBOSS_HOME\bin\add-user.bat -a -u quickuser2 -p quick+123
If you prefer, you can use the add-user utility interactively. For an example of how to use the add-user utility, see instructions in the root README file located here: Add an Application User.
NOTE - Before you begin:
-
If it is running, stop the JBoss EAP server.
-
Backup the following files, replacing JBOSS_HOME with the path to your server:
JBOSS_HOME/domain/configuration/domain.xml JBOSS_HOME/domain/configuration/host.xml
-
After you have completed testing and undeployed this quickstart, you can replace these files to restore the server to its original configuration.
-
Unzip or install a fresh JBoss EAP instance.
-
Open a command prompt and navigate to the root of the server directory. Start the server using the following command:
bin/domain.sh
Open a new command prompt, navigate to the root directory of this quickstart, and run the following command:
JBOSS_HOME/bin/jboss-cli.sh --connect --file=install-domain.cli
This script configures and starts multiple servers needed to run this quickstart. You should see "outcome" => "success" for all of the commands.
NOTE: The following build command assumes you have configured your Maven user settings. If you have not, you must include Maven setting arguments on the command line. See Build and Deploy the Quickstarts for complete instructions and additional options.
-
Make sure you have started and configured the JBoss Server successful as described above.
-
Open a command prompt and navigate to the root directory of this quickstart.
-
Type this command to build the artifacts:
mvn clean install
-
Open a new command prompt and navigate to the root directory of this quickstart. Deploy the applications using the provided CLI batch script by typing the following command:
JBOSS_HOME/bin/jboss-cli.sh --connect --file=deploy-domain.cli
This will deploy the app-*.ear files to different server-groups of the running domain.
NOTE: If ERRORs appear in the server.log when the installing or deploying the quickstart, please stop the domain and restart it. This should ensure further steps run correctly.
This example shows how to invoke an EJB from a remote standalone application. It also demonstrates how to invoke an EJB from a client using a scoped-context rather than a properties file containing the parameters required by the InitialContext.
-
Make sure that the deployments are successful as described above.
-
Navigate to the quickstart
client/
subdirectory. -
Type this command to run the application:
mvn exec:java
The client will output the following information provided by the applications:
InvokeAll succeed: MainApp[anonymous]@master:app-main > [ app1[anonymous]@master:app-oneA > app2[quickuser2]@master:app-twoA ; app2[quickuser2]@master:app-twoA ]
This output shows that the
MainApp
is called with the useranonymous
at nodemaster:app-main
and the sub-call is proceeded by themaster:app-oneA
node andmaster:app-twoA
node asquickuser2
.Review the server log files to see the bean invocations on the servers.
-
To invoke the bean that uses the
scoped-client-context
, you must pass a property. Type the following commandmvn exec:java -DUseScopedContext=true
The invocation of
appTwo
throws ajava.lang.reflect.InvocationTargetException
since the secured method is called and there is no Role for the user defined. You get aBUILD FAILURE
and the client outputs the following information:[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project jboss-ejb-multi-server-client: An exception occured while executing the Java class. null: InvocationTargetException: JBAS014502: Invocation on method: public abstract java.lang.String org.jboss.as.quickstarts.ejb.multi.server.app.AppTwo.invokeSecured(java.lang.String) of bean: AppTwoBean is not allowed -> [Help 1]
Update the user
quickuser1
andquickuser2
and give them one of the RolesAppTwo
orIntern
. To update the roles, open a command prompt and type the following commands:For Linux: JBOSS_HOME/bin/add-user.sh -a -u quickuser1 -p quick123+ --role Intern JBOSS_HOME/bin/add-user.sh -a -u quickuser2 -p quick+123 --role AppTwo For Windows: JBOSS_HOME\bin\add-user.bat -a -u quickuser1 -p quick123+ --role Intern JBOSS_HOME\bin\add-user.bat -a -u quickuser2 -p quick+123 --role AppTwo
If the connection was established before changing the roles it might be necessary to restart the main server, or even the whole domain. After that the invocation will be successful. The log output of the
appTwo
servers shows which Role is applied to the user. The output of the client will show you a simple line with the information provided by the different applications:InvokeAll succeed: MainAppSContext[anonymous]@master:app-main > [ {app1[quickuser1]@master:app-oneA, app1[quickuser1]@master:app-oneA, app1[quickuser2]@master:app-oneB, app1[quickuser2]@master:app-oneA, app1[quickuser2]@master:app-oneA, app1[quickuser2]@master:app-oneB, app1[quickuser2]@master:app-oneA, app1[quickuser2]@master:app-oneA} > appTwo loop(7 time A-B expected){app2[quickuser1]@master:app-twoA, app2[quickuser2]@master:app-twoB, app2[quickuser1]@master:app-twoA, app2[quickuser2]@master:app-twoB, app2[quickuser1]@master:app-twoA, app2[quickuser2]@master:app-twoB, app2[quickuser1]@master:app-twoA, app2[quickuser2]@master:app-twoB, app2[quickuser1]@master:app-twoA, app2[quickuser2]@master:app-twoB, app2[quickuser1]@master:app-twoA, app2[quickuser2]@master:app-twoB, app2[quickuser1]@master:app-twoA, app2[quickuser2]@master:app-twoB, app2[quickuser1]@master:app-twoA, app2[quickuser2]@master:app-twoB} ]
The resulting output in detail:
- The client calls the
MainAppSContext
bean on theapp-main
server on host master with no application security. - The
MainAppSContext
bean inapp-main
calls theAppOne
bean inapp-one
using the scoped-context and establishes the clustered view.- It initially connects using
quickuser1
- The clustered view is created using
quickuser2
. This takes some time, but once it takes effect, all calls are load-balanced.
- It initially connects using
- The calls to the 'AppTwo' bean in
app-two
are made using two different scoped-context settings and both are used alternately 7 times. This means the serversapp-twoA
andapp-twoB
are called alternately seven times each.
- The client calls the
-
If it is necessary to invoke the client with a different JBoss version the main class can be invoked by using the following command from the root directory of this quickstart. Replace $JBOSS_HOME with your current installation path. The output should be similar to the previous mvn executions.
java -cp $JBOSS_HOME/bin/client/jboss-client.jar:app-main/ejb/target/jboss-ejb-multi-server-app-main-ejb-client.jar:app-two/ejb/target/jboss-ejb-multi-server-app-two-ejb-client.jar:client/target/jboss-ejb-multi-server-client.jar org.jboss.as.quickstarts.ejb.multi.server.Client
NOTE:
- If exec is called multiple times, the invocation for
app1
might useapp-oneA
andapp-oneB
node due to cluster loadbalancing. - A new feature introduced in JBoss EAP 6.1 or later will deny the invocation of unsecured methods of
appOne
/appTwo
since security is enabled but the method does not include @Roles. You need to set 'default-missing-method-permissions-deny-access = false' for theejb3
subsystem within the domain profile "ha" and "default" to allow the method invocation. See the install-domain.cli script.
The JSF example shows different annotations to inject the EJB. Also how to handle the annotation if different beans implement the same interface and therefore the container is not able to decide which bean needs to be injected without additional informations.
- Make sure that the deployments are successful as described above.
- Use a browser to access the JSF application at the following URL: http://localhost:8080/jboss-ejb-multi-server-app-main-web/
- Insert a message in the Text input and invoke the different methods. The result is shown in the browser.
- See server logfiles and find your given message logged as INFO.
NOTE :
- If you try to invoke
MainAppSContext
you need to update the userquickuser1
andquickuser2
and give them one of the RolesAppTwo
orIntern
.
An example how to access EJB's from a separate instance which only contains a web application.
- Make sure that the deployments are successful as described above.
- Use a browser to access the Servlet at the following URL: http://localhost:8380/jboss-ejb-multi-server-app-web/
- The Servlet will invoke the remote EJBs directly and show the results, compare that the invocation is successful
NOTE : A new feature in EAP 6.1 or later will deny the invocation of unsecured methods of appOne
/appTwo
since security is enabled but the method does not include @Roles. You need to set 'default-missing-method-permissions-deny-access = false' for the ejb3
subsystem within the domain profile "ha" and "default" to allow the method invocation. See the install-domain.cli script.
-
Make sure you have started the JBoss Server as described above.
-
Open a command prompt and navigate to the root directory of this quickstart.
-
When you are finished testing, type this command to undeploy the archive:
JBOSS_HOME/bin/jboss-cli.sh --connect --file=undeploy-domain.cli
You can remove the domain configuration by manually restoring the back-up copies the configuration files or by running the JBoss CLI Script.
- If it is running, stop the JBoss EAP server.
- Restore the
JBOSS_HOME/domain/configuration/domain.xml
andJBOSS_HOME/domain/configuration/host.xml
files with the back-up copies of the files. Be sure to replace JBOSS_HOME with the path to your server.
Note: This script returns the server to a default configuration and the result may not match the server configuration prior to testing this quickstart. If you were not running with the default configuration before testing this quickstart, you should follow the intructions above to manually restore the configuration to its previous state.
-
Start the JBoss EAP server by typing the following:
For Linux: JBOSS_HOME/bin/domain.sh For Windows: JBOSS_HOME\bin\domain.bat
-
Open a new command prompt, navigate to the root directory of this quickstart, and run the following command, replacing JBOSS_HOME with the path to your server.
JBOSS_HOME/bin/jboss-cli.sh --connect --file=remove-configuration.cli
This script removes the server configuration that was done by the install-domain.cli
script. You should see the following result following the script commands:
The batch executed successfully.
You can also start the server and deploy the quickstarts from Eclipse using JBoss tools. For more information, see Use JBoss Developer Studio or Eclipse to Run the Quickstarts
If you want to debug the source code or look at the Javadocs of any library in the project, run either of the following commands to pull them into your local repository. The IDE should then detect them.
mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc