Author: Serge Pagop
Level: Intermediate
Technologies: SFSB EJB
Summary: The shopping-cart
quickstart demonstrates how to deploy and run a simple Java EE 7 shopping cart application that uses a stateful session bean (SFSB).
Target Product: WildFly
Source: https://github.com/wildfly/quickstart/
The shopping-cart
quickstart demonstrates how to deploy and run a simple Java EE 7 application that uses a stateful session bean (SFSB) in WildFly Application Server. The application allows customers to buy, checkout, and view their cart contents.
The shopping-cart
application consists of the following:
This standalone Java EE module is a JAR containing EJBs. It is responsible for managing the shopping cart.
This simple Java client is launched using a main
method. The remote client looks up a reference to the server module's API, via JNDI. It then uses this API to perform the operations the customer requests.
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.
This example quickstart purposely throws a NoSuchEJBException
exception when the shopping cart is empty. This is the expected result because method is annotated with @Remove
. This means the next invocation after the shopping cart checkout fails because the container has destroyed the instance and it is no longer available. If you do not run this script, you see the following ERROR in the server log, followed by the stacktrace
ERROR [org.jboss.as.ejb3.invocation] (EJB default - 7) WFLYEJB0034: EJB Invocation failed on component ShoppingCartBean for method public abstract java.util.Map org.jboss.as.quickstarts.sfsb.ShoppingCart.getCartContents(): javax.ejb.NoSuchEJBException: WFLYEJB0168: Could not find EJB with id UnknownSessionID [5168576665505352655054705267485457555457535250485552546568575254]
Follow the steps below to suppress system exception logging.
WILDFLY_HOME/standalone/configuration/standalone.xml
For Linux: WILDFLY_HOME/bin/standalone.sh
For Windows: WILDFLY_HOME\bin\standalone.bat
configure-system-exception.cli
file in the root of this quickstart directory. This script sets the log-system-exceptions
attribute to false
in the ejb3
subsystem in the server configuration file.Open a new command prompt, navigate to the root directory of this quickstart, and run the following command, replacing WILDFLY_HOME with the path to your server:
For Linux: WILDFLY_HOME/bin/jboss-cli.sh --connect --file=configure-system-exception.cli
For Windows: WILDFLY_HOME\bin\jboss-cli.bat --connect --file=configure-system-exception.cli
You should see the following result when you run the script:
The batch executed successfully
After stopping the server, open the WILDFLY_HOME/standalone/configuration/standalone.xml
file and review the changes.
You should see the following configuration in the ejb3
subsystem.
<log-system-exceptions value="false"/>
For Linux: WILDFLY_HOME/bin/standalone.sh
For Windows: WILDFLY_HOME\bin\standalone.bat
Make sure you have started the WildFly server. See the instructions in the previous section.
Open a command prompt and navigate to the shopping-cart
quickstart directory
mvn clean install wildfly:deploy
server/target/shopping-cart-server.jar
. You can check the server console to see information messages regarding the deployment.
INFO [org.jboss.as.ejb3.deployment] (MSC service thread 1-2) WFLYEJB0473: JNDI bindings for session bean named 'ShoppingCartBean' in deployment unit 'deployment "shopping-cart-server.jar"' are as follows:
java:global/shopping-cart-server/ShoppingCartBean!org.jboss.as.quickstarts.sfsb.ShoppingCart
java:app/shopping-cart-server/ShoppingCartBean!org.jboss.as.quickstarts.sfsb.ShoppingCart
java:module/ShoppingCartBean!org.jboss.as.quickstarts.sfsb.ShoppingCart
java:jboss/exported/shopping-cart-server/ShoppingCartBean!org.jboss.as.quickstarts.sfsb.ShoppingCart
java:global/shopping-cart-server/ShoppingCartBean
java:app/shopping-cart-server/ShoppingCartBean
java:module/ShoppingCartBean
INFO [org.jboss.weld.deployer] (MSC service thread 1-4) WFLYWELD0006: Starting Services for CDI deployment: shopping-cart-server.jar
INFO [org.jboss.weld.deployer] (MSC service thread 1-8) WFLYWELD0009: Starting weld service for deployment shopping-cart-server.jar
INFO [org.jboss.as.server] (management-handler-thread - 3) WFLYSRV0010: Deployed "shopping-cart-server.jar" (runtime-name : "shopping-cart-server.jar")
Now start a client that will access the beans you just deployed.
You can use the command prompt from the previous step or open a new one and navigate to the root of the shopping-cart
quickstart directory.
Type the following command:
mvn exec:java -f client/pom.xml
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
You should see the following:
32 GB USB 2.0 Flash Drive
and one Wireless Ergonomic Keyboard and Mouse
.checkout()
method in the server ShoppingCartBean
has the @Remove
annotation. This means the container will destroy shopping cart after the call and it will no longer be available.getCartContents()
to make sure the shopping cart was removed after checkout. This results in a javax.ejb.NoSuchEJBException
trace in the server, proving the cart was removed.On the client console, you should see output similar to:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Obtained the remote interface to the shopping cart
Buying a "32 GB USB 2.0 Flash Drive".
Buying another "32 GB USB 2.0 Flash Drive".
Buying a "Wireless Ergonomic Keyboard and Mouse"
Print cart:
1 Wireless Ergonomic Keyboard and Mouse
2 32 GB USB 2.0 Flash Drive
Checkout
Cart was correctly removed, as expected, after Checkout
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
In the server log, you should see:
INFO [stdout] (pool-9-thread-8) implementing checkout() left as exercise for the reader!
You can restore the system exception configuration by running the restore-system-exception.cli
script provided in the root directory of this quickstart or by manually restoring the back-up copy the configuration file.
For Linux: WILDFLY_HOME/bin/standalone.sh
For Windows: WILDFLY_HOME\bin\standalone.bat
For Linux: WILDFLY_HOME/bin/jboss-cli.sh --connect --file=restore-system-exception.cli
For Windows: WILDFLY_HOME\bin\jboss-cli.bat --connect --file=restore-system-exception.cli
This script restores the the log-system-exceptions
attribute value to true
. You should see the following result when you run the script:
The batch executed successfully
WILDFLY_HOME/standalone/configuration/standalone.xml
file with the back-up copy of the file.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.
shopping-cart-server
project and choose Run As
--> Run on Server
.shopping-cart-client
project and choose Run As
--> Java Application
. In the Select Java Application
window, choose Client - org.jboss.as.quickstarts.client
and click OK
. The client output displays in the Console
window.shopping-cart-server
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