EJB 3.0 – EJB calling EJB same package using local
Scenario: Session EJB1 calls Session EJB2 via local interface
Session EJB1
package project1;
import java.util.Hashtable;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
@Stateless(name = "SessionEJB1", mappedName = "MyEJB-SessionEJB1")
@Remote
@Local
public class SessionEJB1Bean implements SessionEJB1, SessionEJB1Local {
public SessionEJB1Bean() {
}
public String sayHi(){
return "Hi";
}
public String callEJB2(){
try {
final Context context = getInitialContext();
SessionEJB2Local sessionEJB2 = (SessionEJB2Local)context.lookup("java:comp/env/App1EAR/SessionEJB2/local");
} catch (Exception ex) {
ex.printStackTrace();
}
return "called";
}
private static Context getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
// WebLogic Server 10.x connection details
env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );
env.put(Context.PROVIDER_URL, "t3://localhost:7001");
return new InitialContext( env );
}
}
Session EJB2
package project1;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless(name = "SessionEJB2", mappedName = "MyEJB-SessionEJB2")
@Remote
@Local
public class SessionEJB2Bean implements SessionEJB2, SessionEJB2Local {
public SessionEJB2Bean() {
}
public String sayHello(){
return "Hello";
}
}
ejb-jar.xml
weblogic-ejb-jar.xml
EJB1 Test Client
package project1;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class SessionEJB1Client {
public static void main(String [] args) {
try {
final Context context = getInitialContext();
SessionEJB1 sessionEJB1 = (SessionEJB1)context.lookup("App1EAR/SessionEJB1/remote");
System.out.println(sessionEJB1.sayHi());
System.out.println(sessionEJB1.callEJB2());
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static Context getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
// WebLogic Server 10.x connection details
env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );
env.put(Context.PROVIDER_URL, "t3://localhost:7001");
return new InitialContext( env );
}
}
No comments:
Post a Comment