ENTERPRISE JAVA BEAN – TEMPERATURE CONVERSION
STEPS
- Create a new folder say EJB in C drive (This is only for this example. But you can create anywhere…)
- Create 4 classes namely TempHome.java , TempRemote.java ,TempEJB.java and TempClient.java inside the EJB folder and place the setenv batch file inside the EJB folder
// TempRemote.java
import javax.ejb.*;
import java.rmi.*;
public interface TempRemote extends EJBObject
{
public double FTOC(double degree) throws RemoteException;
}
//TempHome.java
import javax.ejb.*;
import java.io.Serializable;
import java.rmi.*;
public interface TempHome extends EJBHome
{
public TempRemote create() throws RemoteException,CreateException;
}
//TempEJB.java
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
public class TempEJB implements SessionBean
{
private SessionContext ctx;
public double FTOC(double f)
{
double c;
c=((f-32)*5)/9;
return c;
}
public TempEJB(){ }
public void ejbActivate()
{
System.out.println(“ejbActivate called.”);
}
public void ejbRemove()
{
System.out.println(“ejbRemove Called.”);
}
public void ejbPassivate()
{
System.out.println(“ejbPassivate called.”);
}
public void setSessionContext(SessionContext ctx)
{
System.out.println(“setSessionContext() called.”);
this.ctx = ctx;
}
public void ejbCreate() throws CreateException
{
System.out.println(“ejbCreate() called”);
}
}
//TempClient.java
import javax.naming.*;
import java.io.*;
import javax.rmi.*;
import java.rmi.*;
import java.util.*;
import java.lang.*;
public class TempClient
{
public static void main(String args[])
{
BufferedReader stdin;
try
{
System.out.println(“Before lookup”);
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,”weblogic.jndi.WLInitialContextFactory”);
prop.put(Context.PROVIDER_URL,”t3://localhost:7001″ );
InitialContext ctx = new InitialContext(prop);
System.out.println(“Got Initial Context”);
TempHome home = (TempHome) ctx.lookup(“TempEJB2″);
TempRemote rem = home.create();
System.out.println(“create called”);
try
{
double f=0;
stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“\n\n Enter the temperature in Fahrenheit(type ‘q’ to quit):”);
while(f!=’q')
{
f=Double.parseDouble(stdin.readLine());
double calc=rem.FTOC(f);
System.out.println(“The temperature in celcius:”+String.valueOf(calc));
}
}
catch(Exception e)
{
System.out.println(“error:”+e);
}
rem.remove();
System.out.println(“RemoveCalled”);
}
catch(Exception er)
{
System.out.println(“Called From Client Catch”);
er.printStackTrace();
}
}
}
3. Open the DOS prompt and set the environment
C:\EJB> setenv
4. Compile all the files
C:\EJB>javac *.java
5. Create Deployment descriptor
C:\EJB>java weblogic.marathon.ddinit.EJBInit .
(leave space between EJBInit and dot at the last)
(You will find a folder name META-INF in EJB folder inside which you will find two XML file)
6. Create the jar file
C:\EJB>jar –cvf Temp.jar *.class META-INF
(Temp.jar is the name of the jar file and you will find this jar file created in EJB folder)
7. Deploy the jar file into weblogic server
7.1 Start the weblogic server by clicking Start->Program->BEA Weblogic->User projects->mydomain->start server or if you find start server icon in desktop double click it.
7.2 Open the weblogic server admin console by clicking Start->Program->BEA Weblogic->Examples->Weblogic Server examples->Server Admin console.
7.3 Enter the username and password and sign in. In the right pane you will find Domain configurations->EJB modules.
7.4 Click EJB modules->Deploy new EJB module->Upload your file->Browse(go to EJB folder and select Temp.jar file)->Upload.
7.5 Select myserver->Upload->select Temp.jar->Target module->Deploy.
8. Open a new Dos prompt and run the client program
C:\EJB>setenv
C:\EJB>set classpath=%classpath%;.;
C:\EJB>java TempClient
Result
Before lookup
Got Initial Context
Create called
Enter the temperature in Fahrenheit(type ‘q’ to quit):
212
The temperature in celcius:100.0
Note:
Create setenv.bat with following text (Make necessary changes according to ur local settings)
@rem *************************************************************************
@rem This script is used to set up your environment for development with
@rem WebLogic Server. It simply calls the commEnv.cmd script under
@rem C:\bea\weblogic81\common\bin. Add domain specific configuration in this script below.
@rem *************************************************************************
set WL_HOME=C:\bea\weblogic81
@rem Set Production Mode. When this is set to true, the server starts up in
@rem production mode. When set to false, the server starts up in development
@rem mode. If it is not set, it will default to false.
set PRODUCTION_MODE=
@rem Set JAVA_VENDOR to java virtual machine you want to run on server side.
set JAVA_VENDOR=Sun
@rem Set JAVA_HOME to java virtual machine you want to run on server side.
set JAVA_HOME=C:\bea\jdk141_03
call “%WL_HOME%\common\bin\commEnv.cmd”
@rem set JAVA_VM=
set CLASSPATH=%WEBLOGIC_CLASSPATH%;%POINTBASE_CLASSPATH%;%JAVA_HOME%\jre\lib\rt.jar;%WL_HOME%\server\lib\webservices.jar