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 35 or later.
All you need to build this project is Java SE 17.0 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 WildFly server is started.
-
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 WildFly server is started.
-
Make sure the quickstart is deployed.
-
Type the following command to run the
verify
goal with theintegration-testing
profile activated.$ mvn verify -Pintegration-testing
When you are finished testing the quickstart, follow these steps to undeploy the archive.
-
Make sure WildFly server is started.
-
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. The functionality is provided by the WildFly Maven Plugin, and you may find its configuration in the quickstart pom.xml
:
<profile>
<id>provisioned-server</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<discover-provisioning-info>
<version>${version.server}</version>
</discover-provisioning-info>
<add-ons>...</add-ons>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</profile>
When built, the provisioned WildFly server can 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.
Follow these steps to run the quickstart using the provisioned server.
-
Make sure the server is provisioned.
$ mvn clean install
-
Start the WildFly provisioned server, using the WildFly Maven Plugin
start
goal.$ mvn -f ear/pom.xml wildfly:start
-
Type the following command to run the integration tests.
$ mvn verify -Pintegration-testing
-
Shut down the WildFly provisioned server.
$ mvn -f ear/pom.xml wildfly:shutdown