Vous n'êtes pas connecté. Connexion
|
|
JavaEE6 Weblogic exercice 2De $1Table des matières
IntroductionThis time we will look at new features of Java EE6/JPA:
Create an EAR project, an EJB project and a Dynamic Web/JPA projectAn Enterprise Application project (EAR) 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:
So, you should get 4 projects: DemoEAR, demoEJB, demoEJBClient and demoWeb. You can check that everything is ok by looking at the properties of the DemoEAR projec, then look at "Deployment Assembly", you should see that the 3 other projects are in the assembly. Add a remote EJB to the dynamic web projectAdd a session bean stateless but this tome check the "remote interface" option, and indicate for the name of the bean " DemoSessionEJB" and for the remote interface you should see that the dialog proposes automatically DemoSessionEJBRemote. 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; Notice that the bean implements DemoSessionEJBRemote , and that this interface has been automatically added to the DemoEJBClient project. This "generated project" is useful for standalone clients, as we will see now... Add the helloWord() method to the remote interface, in the file DemoSessionEJBRemote.java, from the DemoEJBClient project. The code should look like that: package sessions; 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 consoleYou 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.DemoSessionEJB!sessions.DemoSessionEJBRemote " We will use this JNDI identifier in the standalone client...
Write a standalone client for this EJBA 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. Create a new project of type "Java project", name it "DemoEJBClientStandalone". You must add two things in the build path so that the client code can compile and run:
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.DemoSessionEJB!sessions.DemoSessionEJBRemote"; 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.) Redeploy the EAR project, just to be sure (right click on server, then "add and remove"). Run the standalone client project (as a "Java application"). 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 applicationThis time, we will write a real application. A small bank account manager. Create an empty MyBank database in javaDB (from Eclipse), connect it to WeblogicUsing 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. Once created Eclipse connects the database, that is, for the moment, empty. Connect this database to Weblogic 12c, follow the same procedure as we did in Exercice 1. 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 AccountUse 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:
Remember: a property is defined by getters and setters, so do not forget to generate/write getters and setters for id, name, balance. This time we will use an auto-generated primary key: @Id The @GeneratedValue annotation indicates that the PK is auto-generated, using any method, you do not care (AUTO). Other possible values = SEQUENCE, ID, etc. see the JPA course. 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: package entities; Like we did yesterday add a AccountFacade session bean to a package named "sessions" to the DemoEJB projectCreate 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 Account create(String name, int balance) { Account a = new Account(name, balance); // insert in database Add a singleton EJB that will populate the database at deployment time, when the project is runpackage entities; The @Singleton annotation guarantees
|
Powered by MindTouch Deki Open Source Edition v.8.08 |