Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

Saturday, March 10, 2007

How to enable load balance stateful session bean ?

Hi..recently one of my customer need to setup load balance for their ejb java applicationbuilt using JDeveloper,ADF technology ( ADF JClient ).They're looking for how to setup load balance between 2 ejb containers.

Finally i've found out that you have to set

'LoadBalanceOnLookup=true' property.

and to really load balance the method call, you have to re-create the InitialContexteach time when calling the ejb method.I know it may quite impact on performance,hopefully OC4J team in next release can enhance this.
This is the implementation with ADF Business Component.You can create your customize strategy class like this :

public class Test {
private static final String EJB_BEAN_NAME = "AppModule2Bean";
public static ApplicationModule getAppModule()
{
try
{
Context ctx = getContext();
AppModule2Home home = (AppModule2Home) ctx.lookup(EJB_BEAN_NAME);
ApplicationModule am = ApplicationModuleProxy.create(home,null /* or some client props */);
am.getTransaction().connectToDataSource(null, "jdbc/Scott1DS", false); return am; }
catch (NamingException nex) { return null; } }

private static InitialContext getContext() {
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.evermind.server.rmi.RMIInitialContextFactory"); env.put(Context.SECURITY_PRINCIPAL, "admin");
env.put(Context.SECURITY_CREDENTIALS, "welcome");
env.put("LoadBalanceOnLookup","true");
env.put("dedicated.rmicontext","true");
/* * PUT YOUR CUSTOM lookup: URL HERE */
env.put(Context.PROVIDER_URL, "ormi://rputra-id:23791/ProjectEJB1,ormi://rputra-id:23792/ProjectEJB1");
return new InitialContext(env); }
catch (NamingException e) { e.printStackTrace(); return null; } }}

public class CustomConnectionStrategy extends DefaultConnectionStrategy{
public CustomConnectionStrategy(){ }
public ApplicationModule createApplicationModule(Hashtable p0) {
JUApplication app = new JUApplication(Test.getAppModule());
AppModule2 appMod = (AppModule2)app.getApplicationModule();
return appMod;
}
}

and then in your ADF Application Module Configuration Manager (right click Application Module, choose Configuration),pick one of your configuration name and set this

jbo.ampool.connectionstrategyclass = 'your CustomConnectionStrategy class name'

that's it your j2ee application now is load balance enabled.

No comments: