JavaEE6 Weblogic exercice 2

De $1

Version de 14:52, 24 Oct 2020

cette version.

Revenir à liste des archives.

Voir la version actuelle

Introduction

This time we will look at new features of Java EE6/JPA:
 

  1. Build an Enterprise project, not a "web profile project",
  2. Write an remote EJB and test a standalone remote client (not a Servlet),
  3. Write a bank account management web application that will generate and update the database schema as we update the code of the entity classes,
  4. Use JPA relationships

Create an EAR project, an EJB project and a Dynamic Web/JPA project

An EAR project is a "composite" project, a container that will hold several other projects and deploy all of them at once in a .ear archive.

Typically, an EAR project will be attached with several other subprojects such as:

  • EJB projects,
  • Dynamic web projects (maybe with JPA facets),
  1. So, let's do it. Create a new project called DemoEAR. For the moment, do not set anything, just click finish. We create this project first as when we will create the subprojects, we will speficy that they belong to this EAR project.
  2. Create an EJB project, call it "DemoEJB", indicate that you want to attach it to the EAR project from step 1,
  3. Create a Dynamic Web project,  name it "DemoWeb", indicate that you want to attach it to the EAR project from step 1,
  4. Add a JPA facet to the dynamic web project. 

Add a remote EJB to the dynamic web project

Add a session bean stateless but this tome check the "remote interface" option, and indicate for the name of the bean " DemoSessionEJBRemote" and for the remote interface "TestRemote". Add an hello world method to the bean, like we did at the beginning of exercice one, yesterday.

The code should look like that: 

package sessions;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

/**
* Session Bean implementation class Test
*/
@Stateless
@LocalBean
public class DemoSessionEJBRemote implements TestRemote {

/**
* Default constructor.
*/
public DemoSessionEJBRemote() {
// TODO Auto-generated constructor stub
}

public String helloWorld() {
return "hello world !";
}

Notice that the bean implements TestRemote, and that this interface is located in another project that has been generated, called DemoEJBClient. This "generated project" is useful for standalone clients, as we will see now...

At this stage, save all and deploy the EAR project to weblogic using right click on the server in the server tab of the Java EE perspective, and use the add/remote menu. It is important to deploy this project as we will need the JNDI name of the EJB we created, and this JNDI name is available from the weblogic admin console.

Look for the JNDI name of the EJB in Weblogic console

 You can do that by looking at the weblogic admin console, on the left, click on "base_domain", then "servers", then in the main page click on the name of your server, then in the main window you should have something like "JNDI tree"... Click on that, a new tab opens in which you can explore the names objects on your server. In the small tree, click on "weblogic" then you will find your application and the EJBs deployed. The "liaison name" of the EJB is the JNDI name. It should look like this: " java:global.DemoEAR.DemoEJB.DemoSessionEJBRemote!sessions.TestRemote"

We will use this JNDI identifier in the standalone client... 

Write a standalone client for this EJB

A standalone client is just a Java project with a java class (with a main) that uses the remote EJB we created.

 

The client project is just a Java project, but you must add two things so that it compiles and run:

  1. Add the DemoEJBClient project in the build path (the project that contains the EJB remote interface you are going to call)
  2. Add wlfullclient.jar, a jar with all the client libs you need. This jar needs to be built once, using a command line interface. This is the procedure to build this jar: cd in the WEBLOGIC_INSTALL_LIB/server/lib, then run the command "java -jar wljarbuilder.jar". This will take 30s to build the client lib for your installation. At the end, you should find a wlfullclient.jar file. Add it to your build path.
  3. Copy this source code, check that the  JNDI_NAME is the same as the one you saw in the weblogic admin console/jndi tree. 

Then you should have such a source code for the standalone client:
 

package client;

import java.util.Hashtable;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import sessions.TestRemote;

public class StandAloneClient {
    private static String INITIAL_CONTEXT_NAME = "weblogic.jndi.WLInitialContextFactory";
    private static String PROVIDER_URL = "t3://localhost:7001";
    private static String JNDI_NAME = "java:global.DemoEAR.DemoEJB.DemoSessionEJBRemote!sessions.TestRemote";

    public static void main(String[] args) throws Exception {
        Context ctx = getInitialContext();
        TestRemote helloEJB = (TestRemote) ctx.lookup(JNDI_NAME);
        System.out.println(helloEJB.helloWorld());
        }

    private static Context getInitialContext() throws NamingException {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_NAME);
        env.put(Context.PROVIDER_URL, PROVIDER_URL);
        return new InitialContext(env);
    }
}

These info come from the Weblogic documentation (name of INITIAL_CONTEXT_FACTORY, etc.)

Run the standalone client project. Normally you should see in the console the "Hello World" message. As you saw, we could call the EJB from any external host, as long as we indicate the right URL for the weblogic server.

Write a bank account management application

This time, we will write a real application. A small bank account manager.

Create an empty MyBank database in javaDB (from Eclipse), connect it to Weblogic

Using the "Data Source Explorer" in Eclipse, use the "New menu" but this time to connect to a database named MyBank that does not exist. Put admin/admin as user/password, and check the "create database if needed" option. Click "test database", this should create an empty database.

Follow the same procedure as we did in Exercice 1, connect this database to Weblogic 12c (if you do not remember, just look at the informations in this page:  Weblogic / Java EE6 FAQ and Guides.) 

In the DemoEJB Project, create an entity class for an Account

Use the New/JPA entity menu and add an empty entity class to the project. Put it in a package named "entities" and name it Account.

A bank account has a few properties:

  • Name of ownner,
  • balance,

Remember: a property is defined by getters and setters, so do not forget to generate/write getters and setters for id, name, balance

You may also add some convenience functions like add(int ammount) or remove(int ammont) methods that will add or remove money from the bank account.

In the end the basic Account.java entity class should look like that:

Write a facade for the Acount entity bean

Like we did yesterday add a AccountFacade session bean to a package named "sessions" to the DemoEJB project

Create a stateless session bean. Implement some methods for the CRUD operations like we did yesterday with the ManufacturerFacade. It is not important to have all of them but at least a create(String name, int balance) method so that we can create new bank accounts and a getAllAccounts() that does a select *. This is very similar to yesterday's exercice.

Add also a method for creating some test accounts so that we will be able to play with a populated database:

public void createTestData() {
    create("John Lennon", 150000);  
create("Paul Mac Cartney", 950000);
create("Ringo Starr", 20000);
create("Georges Harrisson", 100000); }

Add a singleton EJB that will populate the database at deployment time, when the project is run

@Startup
@Singleton
public class InitEJB {
@EJB AccountFacade facade;

@PostConstruct
void init {
// called just after the initial allocation of this bean facade.createTestData();
}

}

The @Singleton annotation guarantees