EJB lookup example
This examples describes you the lookup method used in EJB.
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[] args) throws 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
Ads