Weblogic / Java EE6
Introduction
This page is a good reminder for the Java EE6 developper that plan to use Eclipse indigo with weblogic 12c or greater for developping Servlets/JSPs/EJB/JMS applications.
Writing a remote client standalone for an EJB
You must have at least one EJB that implements a remote interface. Example :
- package sessions;
-
- import javax.ejb.LocalBean;
- import javax.ejb.Stateless;
-
-
-
-
- @Stateless
- @LocalBean
- public class DemoSessionEJBRemote implements TestRemote {
-
- public DemoSessionEJBRemote() {
-
- }
-
- public String helloWorld() { return "hello world !";
- }}
package sessions;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
/**
* Session Bean implementation class Test
*/
@Stateless
@LocalBean
public class DemoSessionEJBRemote implements TestRemote {
public DemoSessionEJBRemote() {
// TODO Auto-generated constructor stub
}
public String helloWorld() { return "hello world !";
}}
And here is the remote interface (located in the Client project that is automatically created when we do an EAR project):
- package sessions;
-
- import javax.ejb.Remote;
-
- @Remote
- public interface TestRemote {
- public String helloWorld();
- }
package sessions;
import javax.ejb.Remote;
@Remote
public interface TestRemote {
public String helloWorld();
}
The client project is just a Java project, but you must add two things so that it compiles and run:
- Add the EJB project in the build path (the project that contains the EJB you are going to call)
- Add wlfullclient.jar, a jar with all the client libs you need. This jar needs to be built once, using a command line interface 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.
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);
- }
- }
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);
}
}
Writing a client for a weblogic queue
Here is an example of a client that sends TextMessages to a MessageDriven bean.
It is recommended to double check the JNDI names of the JMS connexion factory and of the JMS destination.
- Check that the name used in the MDB is the same as the JNDI name you can find in the JNDI tree for your weblogic server. 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 "jms" then on the connection factory and destination. You will find in the detailed view the JNDI names.
- Check that you used exactly the same names in the annotations of your Message driven bean,
- Check that you used exactly the same names in your client servlet.
Example MDB: