Chapter 10. Using EJBs

Table of Contents

10.1. Session Bean Example
10.1.1. Sample Code
10.1.2. Compilation
10.1.3. Deployment
10.1.4. Execution Result
10.2. Java Persistence API Example
10.2.1. Sample File
10.2.2. Compilation
10.2.3. Deployment
10.2.4. Execution Result

This chapter provides examples of developing and deploying an entity using stateless session beans and the Java Persistence API.

A session bean consists of a business interface and a bean class.

Example of an EJB

The following example shows a stateless session bean that has a method that displays helloejb.

The sample file is in the following path.

JEUS_HOME/samples/getting_started/helloejb/helloejb-ejb/src/java/helloejb

Example of a Servlet Client

The following is the implementation of a servlet client that calls helloejb.


The sample file is in the following path.

JEUS_HOME/samples/getting_started/helloejb/helloejb-war/src/java/helloejb

A packaged EJB application can be deployed using WebAdmin or console.

Deploying an EJB Application in WebAdmin

This section describes how to deploy an EJB application in WebAdmin.

Deploying an EJB Application in the Console

This section describes how to deploy an EJB application in the console.

  1. Find the helloejb.ear file that was created.

  2. Log in to JEUS as jeusadmin.

    jeusadmin johan –u administrator –p <password>
  3. Install the application on DAS.

    [DAS]jeus_domain.adminServer>install-application -id helloejb C:\TmaxSoft\JEUS7.0\samples\getting_started\helloejb\dist\helloejb.ear
    Successfully installed application[helloejb].
  4. Deploy the application to an MS (Server1).

    [DAS]jeus_domain.adminServer>deploy helloejb -servers server1
    Succeeded to deploy the application : helloejb

This section describes how to develop an entity using the Java Persistence API and how to compile and deploy the entity.

EJB Example

The sample code consists of a product entity and the ProductManager session bean that processes the entity.

The sample file is in the following path.

JEUS_HOME/samples/getting_started/hellojpa/hellojpa-ejb/src/java/hellojpa

Servlet Client Example

A servlet client that saves and processes data in a database by using the ProductManager EJB is implemented as in the following.

[Example 10.7] <<ProductManagerClient.java>>

package hellojpa;

import java.io.*;
import java.util.Collection;
import javax.ejb.EJB;

import javax.servlet.*;
import javax.servlet.http.*;

public class ProductManagerClient extends HttpServlet {
    @EJB
    private ProductManager productManager;    

    protected void processRequest(HttpServletRequest request,
        HttpServletResponse response)
    throws ServletException, IOException {        
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        out.println("SERVLET CLIENT CONSOLE OUTPUT:\n");
        
        productManager.removeAllProducts();
        out.println("Cleaned up existing products.\n");
        
        out.println("Creating products...");
        Product p1 = productManager.createProduct("1", 10.00, "Ceramic Dog");
        Product p2 = productManager.createProduct("2", 13.00, "Wooden Duck");
        Product p3 = productManager.createProduct("3", 19.00, "Ivory Cat");
        Product p4 = productManager.createProduct("4", 33.00, "Ivory Cat");
        Product p5 = productManager.createProduct("5", 22.00, "Chrome Fish");
        
        Collection products;

        out.println("Created products:");
        products = productManager.findAllProducts();
        for(Object product : products){
             out.println(product);
        }
        out.println();
        
        out.println("Find product with productId 1:");
        Product pp1 = productManager.getProduct("1");
        out.println("Found = " + pp1.getDescription() + " $" + pp1.getPrice());
        
        out.println("Update the price of this product to 12.00");
        pp1.setPrice(12.00);
        productManager.updateProduct(pp1);
        
        Product pp2 = productManager.getProduct("1");
        out.println("Product " + pp2.getDescription() + " is now $" + pp2.getPrice());
        out.println();
        
        out.println("Find products with description:");
        products = productManager.findProductsByDescription("Ivory Cat");
        for(Object product : products){
            out.println(product);
        }
        out.println();
        
        out.println("Find products with price range between 10.00 and 20.00");
        products = productManager.findProductsInRange(10.00, 20.00);
        for(Object product : products){
            out.println(product);
        }
        out.println();
        
        out.println("Removed all products.");
        productManager.removeProduct(p1);
        productManager.removeProduct(p2);
        productManager.removeProduct(p3);
        productManager.removeProduct(p4);
        productManager.removeProduct(p5);
        
        out.close();
    }
    
    protected void doGet(HttpServletRequest request,
        HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request,
        HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
}


The sample file is in the following path.

JEUS_HOME/samples/getting_started/hellojpa/hellojpa-war/src/java/hellojpa

The following are the steps for compiling the sample code using jant.

  1. Execute the jant build command from the directory of the sample code file.

    C:\TmaxSoft\JEUS7.0\samples\getting_started\hellojpa>jant build
  2. After the sample code has been built correctly, the 'dist/hellojpa.ear' EAR file is created.

    Since this example requires a database, Derby must be running. Configure the database with the 'jdbc/sample' data source. For detailed information, refer to "Chapter 8. Configuring a JEUS System" .

    After Derby has been started, create the following database table. In this example, a database named 'sample' is used.

    CREATE TABLE PRODUCT (PRODUCTID VARCHAR(255) NOT NULL, PRICE FLOAT,
        DESCRIPTION VARCHAR(255), PRIMARY KEY (PRODUCTID));
  3. Create the database table by executing the jant setup command as in the following.

    C:\TmaxSoft\JEUS7.0\samples\getting_started\hellojpa>jant setup 

Once the sample code has been built and the database has been set up, the packaged module is ready to be deployed.

A packaged EJB application can be deployed using WebAdmin or console.

Deploying an EJB Application in WebAdmin

This section describes how to deploy an EJB application in WebAdmin.

Note

As discussed in"8.1.3. Adding Data Sources", deployment will fail if the data source is not configured correctly, such as having an incorrect password or IP address.

Deploying an EJB Application using a Console Tool

This section describes how to deploy an EJB application in the console.