Monday, January 11, 2010

Create a session bean from jdeveloper

Right click on application and create a new application.

Give the application name some logical name.Give some package name and choose the template as web application (jsf,ejb)


Now extend session bean you will get two folder Model and View Controller.

Select Model right click on it and say new


Choose Ejb and then session bean




A pop up window will come up like this.Say next



choose the Enterprise JavaBEans2.1(J2ee 1.4)




Give some logical name to the EJB




Say next

And let the default option be there




Ensure both local and remote interfaces are selected





Say next and then finish




Now check in the Jdev you will find a seesion bean is created just select that bean and in the structure pallete expand the source.

You will find two remote interfaces and two local interfaces.

If you are aware of EJB concepts these are the local interface,local home interface,remote interface and remote home interface along with that there is one java bean class.






Now our aim is to use a client to invoke a process deployed in the bean.

So for a client the remote interface will be exposed which in our case is

SessionEjb.java


So we will decalre one method in the remote interface



So I will just add a method declaration in my Remote method,so my code will look like this

package sr.model;

import javax.ejb.EJBObject;

public interface SessionEJB extends EJBObject {

public String getValue(String name) throws java.rmi.RemoteException;
}


here I have added a method decalaration

public String getValue(String name) throws java.rmi.RemoteException;

As we know the methods in a interface are abstract.

We will be defining our method in the bean class.

Here we have the bean class as SessionEJBBean.java

So my code will be like this


package sr.model;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public class SessionEJBBean implements SessionBean {
private SessionContext _context;

public void ejbCreate() {
}

public void setSessionContext(SessionContext context) throws EJBException {
_context = context;
}

public void ejbRemove() throws EJBException {
}

public void ejbActivate() throws EJBException {
}

public void ejbPassivate() throws EJBException {
}

public String getValue(String name) {
return (name+ “ ” + ”rahi”);
}
}

So here we have defined the method that we have declared in our remote interface and it is

public String getValue(String name) {
return (name+ “ ” + ”rahi”);
}

Now we will create a client to call this method

Jdeveloper provides a functionality to create a client.


Select the SessionEJB and right click on it and select new Sample Java Client






Following scree will come up




Say ok and you will find the following code generated for you


package sr.model;

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.rmi.PortableRemoteObject;

public class SessionEJBClient {
public static void main(String [] args) {
try {
final Context context = getInitialContext();
final SessionEJBHome sessionEJBHome =
(SessionEJBHome) PortableRemoteObject.narrow( context.lookup( "SessionEJB" ), SessionEJBHome.class );
SessionEJB sessionEJB;
sessionEJB = sessionEJBHome.create( );
// Call any of the Remote methods below to access the EJB
// sessionEJB.getValue( name );
} catch (Exception ex) {
ex.printStackTrace();
}
}

private static Context getInitialContext() throws NamingException {
// Get InitialContext for Embedded OC4J
// The embedded server must be running for lookups to succeed.
return new InitialContext();
}
}


This is automatically generated by the jdeveloper we will just make some changes to the code and try to invoke the method in our bean.

The changed code will be like this

package sr.model;

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.rmi.PortableRemoteObject;

public class SessionEJBClient {
public static void main(String [] args) {
try {
final Context context = getInitialContext();
final SessionEJBHome sessionEJBHome =
(SessionEJBHome) PortableRemoteObject.narrow( context.lookup( "SessionEJB" ), SessionEJBHome.class );
SessionEJB sessionEJB;
sessionEJB = sessionEJBHome.create( );
// Call any of the Remote methods below to access the EJB
System.out.println("name of code generator is " + sessionEJB.getValue("arpit"));
} catch (Exception ex) {
ex.printStackTrace();
}
}

private static Context getInitialContext() throws NamingException {
// Get InitialContext for Embedded OC4J
// The embedded server must be running for lookups to succeed.
return new InitialContext();
}
}


You can see the code we have changed here only one code as

System.out.println("name of code generator is " + sessionEJB.getValue("arpit"));

Now if you will run your client you may get some error.It may occurs like

javax.naming.NameNotFoundException: SessionEJB not found
at com.evermind.server.rmi.RMIClientContext.lookup(RMIClientContext.java:57)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at sr.model.SessionEJBClient.main(SessionEJBClient.java:14)
Process exited with exit code 0.


This is because your bean is not deployed to the server.You need to deploy your bean class to server for the client to access it.So just select your Session bean and say run.

You might get lot of codes running in the jdeveloper.Finally it shoould show

10/01/07 22:14:48 Oracle Containers for J2EE 10g (10.1.3.4.0) initialized 10/01/07 22:17:33 Oracle Containers for J2EE 10g (10.1.3.4.0) initialized
E:\jdeveloper\jdk\bin\javaw.exe -jar E:\jdeveloper\j2ee\home\admin.jar ormi://10.10.10.10:23891 oc4jadmin **** -updateConfig
Jan 7, 2010 10:21:24 PM com.oracle.corba.ee.impl.orb.ORBServerExtensionProviderImpl preInitApplicationServer
WARNING: ORB ignoring configuration changes. Restart OC4J to apply new ORB configuration.
Ready message received from Oc4jNotifier.
Embedded OC4J startup time: 6843 ms.

Once it is done you can try to run your client programme


Now if you will run your client programme you will get output like

name of code generator is arpit rahi
Process exited with exit code 0.

This is an small example of how to create a session bean from jdeveloper.You can add your own business logic to it as per your requirement.

Now We will see our same Session bean example in j2ee 5.0 versions .The jdeveloper IDE has made it look very easy.IN j2ee 5.0 version we do not have two interfaces for local and remote,it is replaced by one single method to hide the complexity of the code from the end user.I am not going much in to the details of beans I am just illustrating how to use jdeveloper to create a session bean.

Now again select the same steps as you have done earlier but now this time choose the version of j2ee as 5.0




In the last screen you can find how many methods are being created by the jdeveloper
And then say finish.



Now if you will check your jdeveloper and choose the bean you can find three methods
One local method one remote method and one bean class method.






Now again we will change our remote methods as follows

package sr.model;

import javax.ejb.Remote;

@Remote
public interface SessionEJB {
public String getValue(String name)throws java.rmi.RemoteException;

}

Again I will change my bean code as follows


package sr.model;

import javax.ejb.Stateless;

@Stateless(name="SessionEJB")
public class SessionEJBBean implements SessionEJB, SessionEJBLocal {
public SessionEJBBean() {
}

public String getValue(String name)

{
return ("Code generated by " + " " + name + " " + "rahi");
}

}



Now again go to jdeveloper select the bean and generate the client code and modify the code as follows

package sr.model;

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

public class SessionEJBClient {
public static void main(String [] args) {
try {
final Context context = getInitialContext();
SessionEJB sessionEJB = (SessionEJB)context.lookup("SessionEJB");
// Call any of the Remote methods below to access the EJB
System.out.println(sessionEJB.getValue("arpit"));
} catch (Exception ex) {
ex.printStackTrace();
}
}

private static Context getInitialContext() throws NamingException {
// Get InitialContext for Embedded OC4J
// The embedded server must be running for lookups to succeed.
return new InitialContext();
}
}

Again run the bean first and then run the client code.


You will get the output as

Code generated by arpit rahi
Process exited with exit code 0.

1 comment:

Anonymous said...

Hello
In your bean class, what if the methods have to be referenced from another bean class from a completely different application?
Any help on that?
Thanks
Hermione