The ejb-throws-exception
quickstart demonstrates how to throw and handle exceptions across JARs in an EAR.
The ejb-throws-exception
quickstart demonstrates how to handle exceptions across JARs in an EAR deployed to WildFly Application Server. In this quickstart, an EJB in the EJB JAR throws a custom exception. The web application in the client JAR catches the exception and displays it in a nicely formatted message. The EAR contains: JSF WAR, an EJB JAR and a client library JAR containg classes that both the WAR and EJB JAR use.
This example consists of the following Maven projects, each with a shared parent.
Project | Description |
---|---|
|
This project contains the EJB code and can be built independently to produce the JAR archive.
|
|
This project contains the JSF pages and the CDI managed bean.
|
|
This project builds the EAR artifact and pulls in the ejb, web, and client artifacts. |
|
This project builds the ejb-api library artifact which is used by the ejb, web, as well as remote client artifacts.
|
The root pom.xml
builds each of the subprojects in the above order and deploys the EAR archive to the server.
The example follows the common "Hello World" pattern, using the following workflow.
-
A JSF page asks for a user name.
-
On clicking Say Hello, the value of the Name input text is sent to a managed bean named
GreeterBean
. -
On setting the name, the
Greeter
invokes theGreeterEJB
, which was injected to the managed bean. Notice that the field is annotated with@EJB
. -
The EJB responds with Hello <name> or throws an exception if the name is empty or null.
-
The response or exception’s message from invoking the
GreeterEJB
is stored in a field (response) of the managed bean. -
The managed bean is annotated as
@RequestScoped
, so the same managed bean instance is used only for the request/response.
The application this project produces is designed to be run on WildFly Application Server 31 or later.
All you need to build this project is Java 11.0 (Java SDK 11) or later and Maven 3.6.0 or later. See Configure Maven to Build and Deploy the Quickstarts 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.
When you see the replaceable variable QUICKSTART_HOME, replace it with the path to the root directory of all of the quickstarts.
-
Open a terminal and navigate to the root of the WildFly directory.
-
Start the WildFly server with the default profile by typing the following command.
$ WILDFLY_HOME/bin/standalone.sh
NoteFor Windows, use the WILDFLY_HOME\bin\standalone.bat
script.
-
Make sure you start the WildFly server as described above.
-
Open a terminal and navigate to the root directory of this quickstart.
-
Type the following command to build the quickstart.
$ mvn clean install
-
Type the following command to deploy the quickstart.
$ mvn wildfly:deploy
This deploys the ejb-throws-exception/ear/target/ejb-throws-exception.ear
to the running instance of the server.
You should see a message in the server log indicating that the archive deployed successfully.
The application will be running at the following URL http://localhost:8080/ejb-throws-exception-web.
Enter a name in the input field Name and click the Say Hello button to see the response.
The Response output text will display the response from the EJB. If the Name input text box is not empty, then the Response output text will display Hello <name> If the Name input text box is empty, then the Response output text will display the message of the exception throw back from the EJB.
This quickstart includes integration tests, which are located under the src/test/
directory. The integration tests verify that the quickstart runs correctly when deployed on the server.
Follow these steps to run the integration tests.
-
Make sure you start the WildFly server, as previously described.
-
Make sure you build and deploy the quickstart, as previously described.
-
Type the following command to run the
verify
goal with theintegration-testing
profile activated.$ mvn verify -Pintegration-testing
Note
|
You may also use the environment variable |
When you are finished testing the quickstart, follow these steps to undeploy the archive.
-
Make sure you start the WildFly server as described above.
-
Open a terminal and navigate to the root directory of this quickstart.
-
Type this command to undeploy the archive:
$ mvn wildfly:undeploy
Instead of using a standard WildFly server distribution, you can alternatively provision a WildFly server to deploy and run the quickstart, by activating the Maven profile named provisioned-server
when building the quickstart:
$ mvn clean install -Pprovisioned-server
The provisioned WildFly server, with the quickstart deployed, can then be found in the ear/target/server
directory, and its usage is similar to a standard server distribution, with the simplification that there is never the need to specify the server configuration to be started.
The server provisioning functionality is provided by the WildFly Maven Plugin, and you may find its configuration in the quickstart pom.xml
:
<profile>
<id>provisioned-server</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<feature-packs>
<feature-pack>
<location>org.wildfly:wildfly-galleon-pack:${version.server}</location>
</feature-pack>
</feature-packs>
<layers>...</layers>
<!-- deploys the quickstart on root web context -->
<name>ROOT.war</name>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</profile>
Note
|
Since the plugin configuration above deploys quickstart on root web context of the provisioned server, the URL to access the application should not have the |
The integration tests included with this quickstart, which verify that the quickstart runs correctly, may also be run with a provisioned server.
Follow these steps to run the integration tests.
-
Make sure the server is provisioned.
$ mvn clean install -Pprovisioned-server
-
Start the WildFly provisioned server, this time using the WildFly Maven Plugin, which is recommended for testing due to simpler automation. The path to the provisioned server should be specified using the
jbossHome
system property.$ mvn -f ear/pom.xml wildfly:start -DjbossHome=ear/target/server
-
Type the following command to run the
verify
goal with theintegration-testing
profile activated, and specifying the quickstart’s URL using theserver.host
system property, which for a provisioned server by default ishttp://localhost:8080
.$ mvn verify -Pintegration-testing -Dserver.host=http://localhost:8080
-
Shutdown the WildFly provisioned server, this time using the WildFly Maven Plugin too.
$ mvn -f ear/pom.xml wildfly:shutdown