EJB lookup example

This page discusses - EJB lookup example

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.


package ejb;

import javax.ejb.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.


package ejb;

import javax.ejb.Stateless;

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").


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("EJB message is:" + br.getMessage());
  System.err.println("Company name is:" + br.getCompanyname());
  System.err.println("Address is:" + br.getAddress());

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

Share on Google+Share on Google+

EJB lookup example

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: EJB lookup example  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image