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!”

About these ads

29 Responses to “EJB3 – HelloWorld example Using Weblogic 10.3”


    • 2 durga
      January 15, 2010 at 9:56 am

      Hi Jay,

      Nice example for ejb3.0.

      Can you please send one sample application, which is in ejb2.0 same as the application which you have developed in ejb3.0?

      Regards,
      Durga

  1. 3 GS
    January 24, 2010 at 10:03 pm

    Thanks for the simple example.

  2. 4 FVAkcium
    February 12, 2010 at 7:37 pm

    Thanks a lot. It’s sample really helpful.

  3. 5 Rahi Akela
    May 18, 2010 at 5:38 am

    Thank you very much for this nice EJB3 sample example.
    I am kindly request.Please mail me or write a blog on a java web services sample example with Weblogic 10.3 server in the same way.For this,I am obliged to you.
    ———Rahi Akela———
    ———Date:18.05.2010—-

  4. 6 Fernanda Castelo
    September 14, 2010 at 10:55 pm

    Hello.

    I’ve followed this tutorial but I have problems in the topic Deploying and Running the Application.

    Dist folder have not been created. I have not found it. Could you help me?

    PS.: I’m not using wlfullclient.jar. Then I removed some build’s lines.

  5. 8 Hanan Hermon
    September 19, 2010 at 1:33 pm

    Hello,

    It seems like a very simple one. can you add an example for Weblogic 9.2 with Java 1.5?

    Thanks.

  6. 10 Steve
    January 29, 2011 at 8:54 pm

    Very nice. Thanks

  7. 11 visitor
    January 31, 2011 at 6:00 pm

    Everytime I run ‘ant run':
    javax.naming.NameNotFoundException is what I get.

  8. 12 visitor
    February 1, 2011 at 9:31 am

    I thought there is autodeployment in the background. But you must copy the HelloWorld.ear in the autodeploy folder.

  9. 13 Anonymous
    March 19, 2011 at 9:51 pm

    Thanks buddy…after 2 days R&D, i was not able to run this remotely…but i after reading your article i was able to do that…great help…thanks man…

  10. 14 Gulzar Singh
    March 19, 2011 at 9:53 pm

    Thanks buddy…after 2 days R&D, i was not able to run this remotely…but i after reading your article i was able to do that…great help…thanks man…

  11. 15 Anonymous
    June 5, 2011 at 11:43 am

    Very good tutorial. Thanks. Keep it up.

  12. 16 Anonymous
    October 18, 2011 at 6:06 am

    hi, its not working for me

  13. 17 Akhil Kumar
    October 18, 2011 at 10:27 am

    Nice Example.

  14. 18 Anonymous
    November 25, 2011 at 2:14 pm

    very nice demo application.
    Very easy to developing procedure

  15. June 25, 2012 at 10:56 am

    // An alternative client program.

    package com.upog.demo;

    import acme.ejb.session.HelloWorldSession;

    import java.util.Hashtable;

    import javax.naming.CommunicationException;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;

    public class HelloWorldClient {

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

    {
    try

    {
    final Context context = getInitialContext();
    HelloWorld helloWorld =
    (HelloWorld)context.lookup(“SessionEJB-Project1-HelloWorldBean#com.upog.demo.HelloWorld”);
    System.out.println(helloWorld.sayHello(“anisur rehman” ));

    } catch (CommunicationException ex) {
    System.out.println(ex.getClass().getName());
    System.out.println(ex.getRootCause().getLocalizedMessage());
    System.out.println(“\n*** A CommunicationException was raised. This typically\n*** occurs when the target WebLogic server is not running.\n”);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }

    private static Context getInitialContext() throws NamingException {
    Hashtable env = new Hashtable();
    // WebLogic Server 10.x connection details
    env.put( Context.INITIAL_CONTEXT_FACTORY, “weblogic.jndi.WLInitialContextFactory” );
    env.put(Context.PROVIDER_URL, “t3://127.0.0.1:7101″);
    return new InitialContext( env );
    }
    }

  16. 20 Mayank Shrivastava
    June 29, 2012 at 9:50 am

    Thanks a lot for wonderful tutorial and step to do so .
    -Mayank

  17. 22 Anonymous
    December 3, 2012 at 1:16 am

    I get the below exception after running ant run. I have wlclient.jar and weblogic.jar set in my classpath
    run:
    [echo] Executing client class
    [java] javax.naming.NoInitialContextException: Cannot instantiate class: we
    blogic.jndi.WLInitialContextFactory [Root exception is java.lang.ClassNotFoundEx
    ception: weblogic.jndi.WLInitialContextFactory]
    [java] at javax.naming.spi.NamingManager.getInitialContext(Unknown Sour
    ce)
    [java] at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)

    [java] at javax.naming.InitialContext.init(Unknown Source)

  18. 23 Anonymous
    December 3, 2012 at 1:19 am

    I get below exception when I run the command ant run. I have weblogic 12c, and jdk1.6 and have weblogic.jar and wlclient.jar in my classpath
    run:
    [echo] Executing client class
    [java] javax.naming.NoInitialContextException: Cannot instantiate class: we
    blogic.jndi.WLInitialContextFactory [Root exception is java.lang.ClassNotFoundEx
    ception: weblogic.jndi.WLInitialContextFactory]
    [java] at javax.naming.spi.NamingManager.getInitialContext(Unknown Sour
    ce)
    [java] at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)

    [java] at javax.naming.InitialContext.init(Unknown Source)

  19. 24 Anonymous
    January 24, 2013 at 4:20 pm

    Thank you very much!

  20. 25 rahul
    March 6, 2013 at 7:31 pm

    plz help me ………..when i running ejb3 with weblogic 10.3 …….server side deployment no any problem ……..server started successfully……….but when i run HelloClient then below excepion found…….

    Exception in thread “main” java.lang.AssertionError: java.lang.ClassNotFoundException: ravi.ejb3.weblogic10.HelloRemote
    at weblogic.ejb.container.internal.RemoteBusinessIntfGenerator.generateRemoteInterface(RemoteBusinessIntfGenerator.java:57)
    at weblogic.ejb.container.internal.RemoteBusinessIntfProxy.readObject(RemoteBusinessIntfProxy.java:205)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1871)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
    at weblogic.utils.io.ChunkedObjectInputStream.readObject(ChunkedObjectInputStream.java:197)
    at weblogic.rjvm.MsgAbbrevInputStream.readObject(MsgAbbrevInputStream.java:564)
    at weblogic.utils.io.ChunkedObjectInputStream.readObject(ChunkedObjectInputStream.java:193)
    at weblogic.rmi.internal.ObjectIO.readObject(ObjectIO.java:62)
    at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:240)
    at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:348)
    at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:259)
    at weblogic.jndi.internal.ServerNamingNode_1030_WLStub.lookup(Unknown Source)
    at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:392)
    at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:380)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at ravi.ejb3.client.HelloClient.main(HelloClient.java:17)
    Caused by: java.lang.ClassNotFoundException: ravi.ejb3.weblogic10.HelloRemote
    at weblogic.ejb.container.deployer.RemoteBizIntfClassLoader.getClassBytes(RemoteBizIntfClassLoader.java:151)
    at weblogic.ejb.container.deployer.RemoteBizIntfClassLoader.loadClass(RemoteBizIntfClassLoader.java:96)
    at weblogic.ejb.container.internal.RemoteBusinessIntfGenerator.generateRemoteInterface(RemoteBusinessIntfGenerator.java:54)
    … 26 more
    How to resolve this problem ………..plz help me any one…………

  21. 26 Greg
    May 23, 2013 at 2:53 pm

    Thank you for your tutorial. But when I run the script build.xml from Eclipse it seems to be ok :[echo] Executing client class
    [java] Initial Context created
    [java] lookup successful
    [java] Calling EJB method . . .
    [java] Output will be in Managed server console
    BUILD SUCCESSFUL
    Total time: 1 second

    You say that normally I should see the output in server console but where ? I access to the console of weblogic but I don”t where to see the output
    Thanks in advance for your help

    Greg

  22. 27 Anonymous
    March 29, 2014 at 12:45 pm

    nice tutorial… Do you have a StateFull tutorial also?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: