Tuesday, 21 April 2009
9-Writing JAVA Clients for Calling BEA WebLogic EJBs
IX- The naming solution that works for WebLOGIC
File : CalcARSBean.java
package bean;
/**
* Remote interface for CalculatorBean.
*/
public interface CalcARSBean
extends javax.ejb.EJBObject
{
/**
* The method that returns the multiplied value
*/
public int multiply( int val1,int val2 )
throws java.rmi.RemoteException;
}
------------------------------------------------------------------
File : CalcARSBeanHome
package bean;
/**
* Home interface for CalculatorBean.
*/
public interface CalcARSBeanHome
extends javax.ejb.EJBHome
{
public static final String COMP_NAME="java:comp/env/ejb/CalculatorBean";
public static final String JNDI_NAME="CalculatorSessionBean";
public bean.CalcARSBean create()
throws javax.ejb.CreateException,java.rmi.RemoteException;
}
------------------------------------------------------------------
File : CalcARSBeanLocal
package bean;
/**
* Local interface for CalculatorBean.
*/
public interface CalcARSBeanLocal
extends javax.ejb.EJBLocalObject
{
}
------------------------------------------------------------------
File : CalcARSBeanLocalHome
package bean;
/*
* Local home interface for CalculatorBean.
*/
public interface CalcARSBeanLocalHome
extends javax.ejb.EJBLocalHome
{
public static final String COMP_NAME="java:comp/env/ejb/CalculatorBeanLocal";
public static final String JNDI_NAME="CalculatorBeanLocal";
public bean.CalcARSBeanLocal create()
throws javax.ejb.CreateException;
}
------------------------------------------------------------------
File : CalcARSSessionBean
package bean;
/*
* SessionBean.java
*
*/
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
/**
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email deepak@roseindia.net
*/
/**
* This is the Test Session Bean
*
* @ejb:bean type="Stateless"
* name="CalculatorBean"
* jndi-name="CalculatorSessionBean"
* display-name="EJB Calculator Session Bean"
*
* @ejb:env-entry name="CalculatorBean" type="java.lang.String" value="calculatorbean"
*/
public class CalcARSSessionBean implements SessionBean{
public void ejbCreate() throws CreateException {
System.out.println("ejbCreate remote");
}
public void setSessionContext( SessionContext aContext ) throws EJBException {
}
public void ejbActivate() throws EJBException {
}
public void ejbPassivate() throws EJBException {
}
public void ejbRemove() throws EJBException {
}
/**
* The method that returns the multiplied value
*
* @ejb:interface-method view-type="remote"
*/
public int multiply(int val1, int val2){
System.out.println("I am from multiply");
System.out.println("I am from multiply : " + val1 + " * " + val2);
return val1*val2;
}
}
------------------------------------------------------------------
File : Main.java
import bean.CountLocal;
import javax.naming.InitialContext;
import bean.CountRemote;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
public class Main {
public static void main(String[] a) throws Exception {
boolean debug = true;
String name = "java2s";
Context ctx1 = null;
System.out.println("ARSSSsssssssss try getting ctx");
try {
weblogic.jndi.Environment environment = new weblogic.jndi.Environment();
environment.setInitialContextFactory("weblogic.jndi.WLInitialContextFactory");
environment.setProviderUrl("t3://localhost:7101");
environment.setSecurityPrincipal("weblogic");
environment.setSecurityCredentials("weblogic");
environment.setRMIClientTimeout(7000L);
environment.setRequestTimeout(7000L);
ctx1 = environment.getInitialContext();
environment.setCreateIntermediateContexts(true);
System.out.println(environment.getRMIClientTimeout());
System.out.println(environment.getRequestTimeout());
System.out.println("Got Context1: " + ctx1);
}
catch ( NamingException ne ) {
ne.printStackTrace();
}
System.out.println("ARSSSsssssssss beg");
CountRemote service = (CountRemote) ctx1.lookup("ejb.CountBean/remote");
int countVal = 9;
service.set(countVal);
countVal = service.count();
System.out.println(countVal);
System.out.println("Calling count() on beans...");
countVal = service.count();
System.out.println(countVal);
service.remove();
}
}
----------------------------------------------------------------------------------
( prev ) ( next )