Wednesday, 24 September 2014

EJB 3.0 + websphere 8.5

 
EJB:

EJB stands for Enterprise Java Bean. EJB provides an architecure to develop and deploy component based enterprised applications. Bean will run  in EJB container supported by any application server compliant with J2ee 1.3 standard specification.

Advantages of EJB:


1. Componets can be clustered for high availability of the application.
2. Scalability.
3. transaction management.
4. Security.

All life cyce of EJB managed by container and developer has to focus on business logic.

Simple basic application to show how to deploy the EJB application in websphere 8.5.

1. Creat project called MyComponent in eclipse.

POJO bean class to serve simple request. This bean will return the name as a response.
Annotations to define some EJB features.

@Stateless: This bean is stateless. Dont maintain conversation state.
@Local(NameServiceLocal.class). Local view
@Remote(NameServiceRemote.class).Remote View.
@LocalBean

NameService .java
package com.service.com;
import com.service.com.view.NameServiceLocal;
import com.service.com.view.NameServiceRemote;
import javax.ejb.Local;
import javax.ejb.LocalBean;
import javax.ejb.Remote;
import javax.ejb.Stateless;
/**
 * Session Bean implementation class NameService
 */
@Stateless
@Local(NameServiceLocal.class)
@Remote(NameServiceRemote.class)
@LocalBean
public class NameService implements NameServiceRemote, NameServiceLocal
{
    /**
     * Default constructor.
     */
    public NameService()
 {
        // TODO Auto-generated constructor stub
    }
   
public String getName()
{

  return "Prashant Savadi";
 }
}

2.  Create local view for the bean.

package com.service.com.view;
public interface NameServiceLocal
 {
   public String getName();
}
3. Create remote view of the bean

package com.service.com.view;
public interface NameServiceRemote
 {
     public String getName();
}

4. ejb-jar.xml file

<?xml version="1.0" encoding="UTF-8"?><ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
  <display-name>MyComponent</display-name>
 </ejb-jar>



5. Export the the project as JAR file and deploy in server as shown below.

 




6. Generating stub class.

Go to server bin directory and run below command to generate stub class.

Stub will be generated and placed into the JAR file. Keep this JAR file in client project classpath and run the client code.



7.Accessing EJB from client code.

Before running client its required to place below jar in class path.

com.ibm.ws.ejb.thinclient_8.5.0

Below is the client code to look up the EJB which is deployed in the server. After running below code EJB should rerurn the name. Here "compremote" is the JNDI name whcih is configured from server admin console and same can be looked up from the client code.

package com.client;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.service.com.view.NameServiceLocal;
import com.service.com.view.NameServiceRemote;
public class Client {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Properties p = new Properties();
  p.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
  //p.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
  p.put(Context.PROVIDER_URL, "corbaloc:iiop:localhost:2813");
  //iiop://localhost:2813/
  //p.put(Context.PROVIDER_URL, "iiop://localhost:2809/");
  //String jndi = "ejblocal:enterprise_app_name/ejb_web_project_name.jar/ejb_name#name.of.local.impl.interface";
  NameServiceRemote ejb=null;
  try {
   InitialContext ctx = new InitialContext(p);
    ctx
    .lookup("compremote");
  //java:global/MyComponent/NameService!com.service.com.view.NameServiceRemote
   Object rejb = (Object) ctx
     .lookup("compremote");

   ejb=(NameServiceRemote)javax.rmi.PortableRemoteObject.narrow(rejb,com.service.com.view.NameServiceRemote.class);
//<terminated>com.client.Client at localhost:57599
  } catch (NamingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println(ejb.getName());

 }
}

8. Response

"Prashant Savadi"     :)

 

 

 




No comments:

Post a Comment