Pages

Thursday, September 22, 2011

EJB3 – Helloworld example using weblogic 10.3

It is sample EJB3 Program. It uses weblogic 10.3 server , jdk 1.6 and ant tool

you need the following jars to complie and run the EJB3 Application

wlfullclient.jar
javax.ejb_3.0.1.jar
weblogic.jar
The Ear file consisting of ejb was deployed in weblogic sevrer and it was invoked from a stand alone java client.

Below are the steps to develop sample EJB3 application

1.Create the directory structure and source files as shown below


Remote Interface

package com.upog.demo;

import javax.ejb.Remote;
@Remote
public interface HelloWorld

{
public void sayHello(String name);
}

Bean Class

package com.upog.demo;

import javax.ejb.Stateless;
@Stateless(mappedName=”HelloWorld”)
public class HelloWorldBean implements HelloWorld

{
public void sayHello(String name)

{
System.out.println(“Hello ” + name + ” It’s Working!”);

}

}

Client Program

package com.upog.demo;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;

public class HelloWorldClient {

private static HelloWorld helloWorld;
public static void main(String[] args)

{
try

{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,”weblogic.jndi.WLInitialContextFactory”);
env.put(Context.SECURITY_PRINCIPAL,”weblogic”);
env.put(Context.SECURITY_CREDENTIALS,”weblogic”);
env.put(Context.PROVIDER_URL,”t3://localhost:8003″);
Context ctx = new InitialContext(env);
System.out.println(“Initial Context created”);
helloWorld = (HelloWorld) ctx.lookup(“HelloWorld#com.upog.demo.HelloWorld”);
System.out.println(“lookup successful”);
System.out.println(“Calling EJB method . . .”);
helloWorld.sayHello(“Upog”);
System.out.println(“Output will be in Managed server console”);
}

catch (Exception e)
{
e.printStackTrace();
}

}

}

Note : Change the value of PROVIDER_URL as per your weblogic server. Default value is 7001.

Also give your weblogic id and password for SECURITY_PRINCIPAL and SECURITY_CREDENTIALS

JNDI naming convention for other servers will be different(vendor specific).

For weblogic (mappedName#fully_qualified_name)

developer.properties

BEA_HOME = D:/data/bea
JAVA_HOME = ${BEA_HOME}/jdk160_05
WLS_HOME = ${BEA_HOME}/wlserver_10.3
project.home = D:/data/workspace/EJB-HelloWorld

wls.domain.path = ${BEA_HOME}/user_projects/domains/test_domain
wls.application.path = ${wls.domain.path}/applications

Note : change the value of BEA_HOME variable as per your weblogic

Build.xml






























“Cleaning the directory ”







“Compiling EJB ”











“Compiling Client class ”










“Building EJB EAR”





















2. Place the following jars in ${project.home}/lib folder

wlfullclient.jar

javax.ejb_3.0.1.jar

weblogic.jar

Note: To create wlfullclient.jar. pls refer weblogic docs

javax.ejb_3.0.1.jar will be in ${BEA_HOME}/modules

weblogic.jar will be in ${WLS_HOME}/server/lib

Deploying and Running the Application

1. RUN ant

D:\data\workspace\EJB-HelloWorld>ant

The following folders and class files will be created

Deploy the HelloWorld.ear created in dist folder in your Weblogic server. Ensure the state of the ear deployed was active

2. RUN ant run

D:\data\workspace\EJB-HelloWorld>ant run

The output will be in server console “Hello It’s Working!”

No comments:

Post a Comment