Vous n'êtes pas connecté. Connexion
|
|
JavaEE6 Weblogic exercice 2De $1IntroductionThis time we will look at new features of Java EE6/JPA:
Create an EAR project, an EJB project and a Dynamic Web/JPA projectAn 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:
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 " 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; 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 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.DemoSessionEJBRemote!sessions.TestRemote" 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, but you must add two things so that it compiles 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.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.) |
Powered by MindTouch Deki Open Source Edition v.8.08 |