Posts Tagged ‘EJB3 & weblogic-10

18
Oct
09

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

Directory Structure

Directory Structure

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<String, String> env = new Hashtable<String, String>();
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

<?xml version=”1.0″ encoding=”iso-8859-1″?>
<project name=”Ejb_HelloWorld” basedir = “.” default = “buildEar”>

<property file=”${basedir}/developer.properties”/>
<property name=”jarname” value=”HelloWorld-ejb” />

<property name=”appname” value=”HelloWorld” />

<property name=”src”         value=”${basedir}/src”/>
<property name=”build”        value=”${basedir}/build”/>
<property name=”dist”        value=”${basedir}/dist”/>
<property name=”lib”        value=”${basedir}/lib” />

<property name=”src.server”         value=”${src}/server”/>
<property name=”src.client”         value=”${src}/client”/>
<property name=”build.server”        value=”${build}/server”/>
<property name=”build.client”        value=”${build}/client”/>
<property name=”dist.server”        value=”${dist}/server”/>
<property name=”dist.client”        value=”${dist}/client”/>

<path id=”dependencies”>

<pathelement location=”${lib}/javax.ejb_3.0.1.jar”/>
<pathelement location=”${lib}/weblogic.jar”/>

</path>

<target name = “clean” >

<echo> “Cleaning the directory ” </echo>
<delete dir=”${build}” />
<delete dir=”${dist}” />

</target>

<target name=”compile” depends=”clean”>

<echo> “Compiling EJB ” </echo>
<echo message=”BEA_HOME: ${BEA_HOME}”/>
<echo message=”WLS_HOME: ${WLS_HOME}”/>
<mkdir dir=”${build}/server/classes”/>
<javac srcdir=”${src.server}” destdir=”${build}/server/classes” debug=”on” >
<classpath refid=”dependencies”/>
</javac>

</target>

<target name=”compileClient“>

<echo> “Compiling Client class ” </echo>
<mkdir dir=”${build.client}/classes”/>
<javac srcdir=”${src.client}” destdir=”${build.client}/classes” debug=”on” >
<classpath refid=”dependencies”/>
<classpath location=”${build.server}/classes” />
</javac>

</target>

<target name = “buildEar” depends=”compile”>

<echo> “Building EJB EAR” </echo>
<mkdir dir=”${dist.server}”/>
<jar jarfile = “${dist.server}/${jarname}.jar” basedir = “${build}/server/classes”  />
<jar jarfile = “${dist.server}/${appname}.ear”  basedir=”${dist.server}” />

</target>

<target name=”run” depends=”compileClient”>

<echo message=”Executing client class “> </echo>
<java classname= “com.upog.demo.HelloWorldClient” fork=”yes”>
<classpath>
<pathelement location=”${build.client}/classes”/>
<pathelement location=”${dist.server}/HelloWorld-ejb.jar”/>
<pathelement location=”${lib}/wlfullclient.jar”/>
</classpath>
</java>

</target>

</project>

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

Ant build

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 Upog It’s Working!”




Follow

Get every new post delivered to your Inbox.