Discussions

EJB programming & troubleshooting: How to run EJB using J2EE server

  1. How to run EJB using J2EE server (1 messages)

    Hi all,

    I need a step by step procedure to deploy the EJB using J2EE server.
    I also need information about the portability of EJB/CORBA and what are the software used to interface EJB/CORBA.
    and steps used to interface EJB/CORBA.
  2. How to run EJB in j2ee server[ Go to top ]

    I have tried to get the step by step procedure to run a lab experiment. Even after exaustive search I got only a few-that too I could,nt implement the steps as such. I have tried to run the EJB combining the steps from 1 or 2 web resources and the steps from j2ee1.4 tutorial. It really works. I've aslo given the source code for you to test. I have installed j2sd1.4.2 in C drive and j2ee1.4 in c:\Sun2\AppServer Just follow the steps as given below. velumaniramesh at yahoo dot com Place the following java programs in c:\arith1\arith1 arith1.java arith1Home.java arith1Bean.java arith1Client.java Source code is given at the end. Steps for Execution C:\arith1\arith1>set path=c:\j2sdk1.4.2\bin; C:\arith1\arith1>set classpath=c:\Sun2\AppServer\lib\j2ee.jar;c:\Sun2\AppServer\bin;.; C:\arith1\arith1>set j2ee.home=c:\Sun2\AppServer; C:\arith1\arith1>set java.home= c:\j2sdk1.4.2; C:\arith1\arith1>javac *.java AllPrograms-.SunMicrosystems->ApplicationServerPE->Start default server Creating the J2EE Application In this section, you'll create a J2EE application named arith1App, storing it in the file arith1App.ear. 1. In deploytool, select File New Application. 2. Click Browse. 3. In the file chooser, navigate to this directory: C:\arith1\arith1 In the File Name field, enter arith1App.ear. 4. Click New Application. 5. Click OK. 6. Verify that the arith1App.ear file resides in the directory specified in step 3. Creating the Enterprise Bean Packaging the Enterprise Bean To package an enterprise bean, you run the Edit Enterprise Bean wizard of the deploytool utility. During this process, the wizard performs the following tasks: • Creates the bean's deployment descriptor • Packages the deployment descriptor and the bean's classes in an EJB JAR file • Inserts the EJB JAR file into the arith1App.ear file To start the Edit Enterprise Bean wizard, select File New Enterprise Bean. The wizard displays the following dialog boxes. 1. Introduction dialog box a. Read the explanatory text for an overview of the wizard's features. b. Click Next. 2. EJB JAR dialog box a. Select the button labeled Create New JAR Module in Application. b. In the combo box below this button, select arith1App. c. In the JAR Display Name field, enter arith1JAR. d. Click Edit Contents. e. In the tree under Available Files, locate the build/arith1 subdirectory. (If the target directory is many levels down in the tree, you can simplify the tree view by entering all or part of the directory's path name in the Starting Directory field.) f. In the Available Files tree select these classes: arith1.class, arith1Bean.class, and arith1Home.class. (You can also drag and drop these class files to the Contents text area.) g. Click Add. h. Click OK. i. Click Next. 3. General dialog box a. Under Bean Type, select the Stateless Session. b. In the Enterprise Bean Class combo box, select arith1.arith1Bean. c. In the Enterprise Bean Name field, enter arith1Bean. d. In the Remote Home Interface combo box, select arith1.arith1Home. e. In the Remote Interface combo box, select arith1.arith1. f. Click Next. 4. In the Expose as Web Service Endpoint dialog box, select No and click Next. 5. Click Finish. Creating the Application Client An application client is a program written in the Java programming language. At runtime, the client program executes in a different virtual machine than the Application Server Packaging the Application Client To start the New Application Client wizard, select File New Application Client. The wizard displays the following dialog boxes. 1. Introduction dialog box a. Read the explanatory text for an overview of the wizard's features. b. Click Next. 2. JAR File Contents dialog box a. Select the button labeled Create New AppClient Module in Application. b. In the combo box below this button, select arith1App. c. In the AppClient Display Name field, enter arith1Client. d. Click Edit Contents. e. In the tree under Available Files, locate this directory: C:\arith1\arith1 f. Select the arith1Client.class file. g. Click Add. h. Click OK. i. Click Next. 3. General dialog box a. In the Main Class combo box, select arith1Client. b. Click Next. c. Click Finish. Specifying the Application Client's Enterprise Bean Reference When it invokes the lookup method, the arith1Client refers to the home of an enterprise bean: Object objref = myEnv.lookup("ejb/Simplearith1"); You specify this reference in deploytool as follows. 1. In the tree, select arith1Client. 2. Select the EJB Ref's tab. 3. Click Add. 4. In the Coded Name field, enter ejb/Simplearith1 5. In the EJB Type field, select Session. 6. In the Interfaces field, select Remote. 7. In the Home Interface field enter, arith1.arith1Home. 8. In the Local/Remote Interface field, enter arith1. arith1 9. In the JNDI Name field, select arith1Bean. 10. Click OK. Mapping the Enterprise Bean References To map the enterprise bean references in the clients to the JNDI name of the bean, follow these steps. 1. In the tree, select arith1App. 2. Click the Sun-specific Settings button. 3. Select the JNDI Names in the View field. 4. In the Application table, note that the JNDI name for the enterprise bean is arith1Bean. 5. In the References table, enter arith1Bean in the JNDI Name column for each row. Figure 24-1 shows what the JNDI Names tab should look like after you've performed the preceding steps. Figure 24-1 ConverterApp JNDI Names Deploying the J2EE Application Now that the J2EE application contains the components, it is ready for deployment. 1. Select the arith1App application. 2. Select Tools Deploy. 3. Under Connection Settings, enter the user name and password for the Application Server. 4. Tell deploytool to create a JAR file that contains the client stubs. (For more information on client JAR files, see the description under Creating the Application Client.) a. Select the Return Client JAR checkbox. b. In the field below the checkbox, enter C:\arith1\arith1 5. Click OK. 6. In the Distribute Module dialog box, click Close when the deployment completes. 7. Verify the deployment. a. In the tree, expand the Servers node and select the host that is running the Application Server. b. In the Deployed Objects table, make sure that the arith1App is listed and its status is Running. 8. Verify that a stub client JAR named arith1AppClient.jar resides in C:\arith1\arith1 Running the Application Client To run the application client, perform the following steps. 1. In a terminal window, go to this directory: C:\arith1\arith1 2. C:\arith1\arith1>set APPCPATH=c:\arith1\arith1\arith1AppClient.jar 3. C:\arith1\arith1>set path=c:\Sun2\AppServer\bin; 4. C:\arith1\arith1>appclient -client arith1App.ear -name arith1Client -textauth In the terminal window, the client displays the result Product of 2& 3:6 //arith1Client.java import arith1.arith1; import arith1.arith1Home; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; public class arith1Client { public static void main(String[] args) { try { Context initial = new InitialContext(); Context myEnv = (Context) initial.lookup("java:comp/env"); Object objref = myEnv.lookup("ejb/Simplearith1"); arith1Home home = (arith1Home) PortableRemoteObject.narrow(objref, arith1Home.class); arith1 a = home.create(); int product = a.multiply(3,2); System.out.println("Product of 2& 3:"+product); System.exit(0); } catch (Exception ex) { System.err.println("Caught an unexpected exception!"); ex.printStackTrace(); } } } //arith1.java package arith1; /** * Remote interface for CalculatorBean. */ import javax.ejb.EJBObject; import java.rmi.RemoteException; import java.math.*; public interface arith1 extends javax.ejb.EJBObject { /** * The method that returns the multiplied value */ public int multiply( int val1,int val2 ) throws java.rmi.RemoteException; } //arith1Home.java package arith1; /** * Home interface for CalculatorBean. */ public interface arith1Home extends javax.ejb.EJBHome { public arith1 create() throws javax.ejb.CreateException,java.rmi.RemoteException; } //arith1Bean.java package arith1; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class arith1Bean implements SessionBean{ public void ejbCreate() throws CreateException { } 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 * */ public int multiply(int val1, int val2){ System.out.println("I am from multiply : " + val1 + " * " + val2); return val1*val2; } }