Home Ejb EJB lookup example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

EJB lookup example

Advertisement
This examples describes you the lookup method used in EJB.

EJB lookup example

     

This examples describes you the lookup method used in EJB.

Bean30Remote.java:-This  is the Remote interface which extends javax.ejb.EJBObject package. These are similar to RMI Remote interface.  The use of remote interface is particularly helpful in  providing business-specific functionality of an EJB. Here @Remote is the annotation used to declare the interface as Remote.

Bean30Remote.java


package ejb;

import javax.ejb.Remote;

@Remote
public interface Bean30Remote {

  String getMessage();

  String getAddress();

  String getCompanyname();
}

Bean30Bean.java:-This is the bean of type session in which we have defined  the body of the method which were declared in the  
interface named SessionBeanRemote.java.@Stateless is the annotation used to declare the bean as a session type.

@Stateless(mappedName="Bean30"):-It is used to change the mapped name from Bean30Bean to Bean30.After this we will use Bean30 to look up the bean in our application client.

Bean30Bean.java


package ejb;

import javax.ejb.Stateless;

@Stateless(mappedName="Bean30")
public class Bean30Bean implements Bean30Remote {

  public String getMessage() {
  return "Roseindia.net";
  }
  public String getAddress() {
  return "Sec-3,D-16/116,Rohini";
  }
  public String getCompanyname() {
  return "Roseindia.net Pvt.Ltd.";
  }
}

Main.java:-This is the client application from which  we can access the methods which are defined in the bean.

InitialContext ctx = new InitialContext():-InitialContext is the class which is used to find out  the starting context for performing naming operations. Initial Context is used to lookup home interfaces with inside an EJB. Here all the naming convention are relative to context. 

Bean30Remote br = (Bean30Remote) ctx.lookup("Bean30"):-This is the method that is used to retrieve the named object i.e. ("Bean30").

Main.java

package ejbmodule30;

import ejb.Bean30Remote;
import javax.naming.InitialContext;

public class Main {

  public static void main(String[] argsthrows Exception {
  InitialContext ctx = new InitialContext();
  Bean30Remote br = (Bean30Remote) ctx.lookup("Bean30");
  System.err.println("=================================");
  System.err.println("EJB message is:" + br.getMessage());
  System.err.println("Company name is:" + br.getCompanyname());
  System.err.println("Address is:" + br.getAddress());
  System.err.println("=================================");
  }
}

Output of the program

=================================
EJB message is:Roseindia.net
Company name is:Roseindia.net Pvt.Ltd.
Address is:Sec-3,D-16/116,Rohini
=================================

Download source code

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 12, 2009

Ask Questions?    Discuss: EJB lookup example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
pratap
September 12, 2012
EJB jar export

hie... I am using jBoss7 and eclipse Editor I made one EJB Session Bean and its remote, but where should I export its jar file... Do I need to export it in lib file of my web Project.And What about lookup path...?? I want to know the same thing for apache also...
DMCA.com