Oracle8i Enterprise JavaBeans Developer's Guide and Reference Release 3 (8.1.7) Part Number A83725-01 |
|
Overview ======== This EJB example shows how you can create a second EJB in the same server, but in a different session. The same username and password are used to create the second object, and it accesses the same published EJB. Source Files ============ Client.java ----------- You invoke the client program from a command prompt, and pass it four arguments, the - service URL (service ID, hostname, port, and SID if port is a listener) - name of the published bean to lookup and instantiate - username - password that authenticates the client to the Oracle8i database server For example: % java -classpath LIBs Client sess_iiop://localhost:2481:ORCL | /test/myHello scott tiger where LIBs is the classpath that must include $ORACLE_HOME/lib/aurora_client.jar #If using Java 2, use classes12.zip instead of classes111.zip $ORACLE_HOME/jdbc/lib/classes111.zip $ORACLE_HOME/lib/vbjorb.jar $ORACLE_HOME/lib/vbjapp.jar $JAVA_HOME/lib/classes.zip The client code performs the following steps: - gets the arguments passed on the command line - creates a new JNDI Context (InitialContext()) - looks up the published bean to find and activate its home interface - using the home interface, instantiates through its create() method a new bean object, hello - sets the hello bean's message to "Hello World!" - asks the first hello bean to create another bean, by invoking the getOtherHello() method, passing it the authentication, service URL, and bean name parameters - invokes otherHelloWorld() on the first bean, and printing its return value, which is derived from the second created bean The printed output is: Hello World! Hello from the Other HelloBean Object hello.ejb --------- The bean deployment descriptor. helloServer/HelloBean.java -------------------------- The EJB implementation. hello/Hello.java ---------------- The bean remote interface. hello/HelloHome.java -------------------- The bean's home interface. Compiling and Running the Example ================================= Before running this example, the user 'scott' needs to have javauserpriv. This can be enabled by doing: $ svrmgrl SVRMGRL> connect internal SVRMGRL> grant javauserpriv to scott; SVRMGRL> quit $ The configuration file INITSID.ORA must also specify that at least two MTS servers can be activated. That is, the parameters MTS_SERVERS and MTS_MAX_SERVERS must be set to at least the following: mts_servers=2 mts_max_servers=2 UNIX ---- Enter the command 'make all' or simply 'make' in the shell to compile, load, and deploy the objects, and run the client program. Other targets are 'run' and 'clean'. Make sure that a shell environment variable ORACLE_HOME is set to point to the home location of the Oracle installation. This is operating system dependent, so see the Installation documentation that came with your system for the location. Also, review the README file for the Oracle database, and the README file for the CORBA/EJB server (the Oracle8i ORB), for additional up-to-date information. Windows NT ---------- On Windows NT, run the batch file makeit.bat from a DOS command prompt to compile, load, and deploy the objects. Run the batch file runit.bat to run the client program, and see the results. Make sure that the environment variables %ORACLE_HOME%, %CLASSPATH%, and %SERVICE% are set appropriately for the DOS command window. You can set these as either user or system environment variables from the Control Panel. Double click on System in the Control Panel then on the Environment tab to set these variables. Start a new DOS window after setting environment variable values. See the Installation documentation that came with your Oracle8i system for the values of these variables. Also, review the README file for the Oracle database, and the README file for the CORBA/EJB server (the Oracle8i ORB), for additional up-to-date information. You can also set an environment variable %JAVA_HOME% to point to the root of your Java JDK. For example, SET JAVA_HOME=C:\JDK1.1.6.
import hello.Hello; import hello.HelloHome; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Hashtable; public class Client { public static void main (String[] args) throws Exception { if (args.length != 4) { System.out.println ("usage: Client serviceURL objectName user password"); System.exit (1); } String serviceURL = args [0]; String objectName = args [1]; String user = args [2]; String password = args [3]; Hashtable env = new Hashtable (); env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); env.put (Context.SECURITY_PRINCIPAL, user); env.put (Context.SECURITY_CREDENTIALS, password); env.put (Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN); Context ic = new InitialContext (env); // Activate a Hello in the 8i server // This creates a first session in the server HelloHome hello_home = (HelloHome)ic.lookup (serviceURL + objectName); Hello hello = hello_home.create (); hello.setMessage ("Hello World!"); System.out.println (hello.helloWorld ()); // Ask the first Hello to activate another Hello in the same server // This creates Another SESSION used by the first session hello.getOtherHello (user, password, serviceURL + objectName); System.out.println (hello.otherHelloWorld ()); } }
package hello; import javax.ejb.EJBHome; import javax.ejb.CreateException; import java.rmi.RemoteException; public interface HelloHome extends EJBHome { public Hello create () throws RemoteException, CreateException; }
package hello; import javax.ejb.EJBObject; import java.rmi.RemoteException; import javax.ejb.CreateException; public interface Hello extends EJBObject { public String helloWorld () throws RemoteException; public void setMessage (String message) throws RemoteException; public void getOtherHello (String user, String password, String otherBeanURL) throws RemoteException, CreateException; public String otherHelloWorld () throws RemoteException; }
package helloServer; import hello.*; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.ejb.CreateException; import java.rmi.RemoteException; import javax.naming.NamingException; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Hashtable; import oracle.aurora.jndi.sess_iiop.ServiceCtx; public class HelloBean implements SessionBean { String message; Hello otherHello; // Methods of the Hello interface public String helloWorld () throws RemoteException { return message; } public void setMessage (String message) throws RemoteException { this.message = message; } public void getOtherHello (String user, String password, String otherBeanURL) throws RemoteException, CreateException { try { // start a new session Hashtable env = new Hashtable (); env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); env.put (Context.SECURITY_PRINCIPAL, user); env.put (Context.SECURITY_CREDENTIALS, password); env.put (Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN); Context ic = new InitialContext (env); // create the other Bean instance HelloHome other_HelloHome = (HelloHome)ic.lookup (otherBeanURL); otherHello = other_HelloHome.create (); otherHello.setMessage ("Hello from the Other HelloBean Object"); } catch (NamingException e) { e.printStackTrace (); } } public String otherHelloWorld () throws RemoteException { if (otherHello != null) return otherHello.helloWorld (); else return "otherBean is not accessed yet"; } // Methods of the SessionBean public void ejbCreate () throws RemoteException, CreateException {} public void ejbRemove () {} public void setSessionContext (SessionContext ctx) {} public void ejbActivate () {} public void ejbPassivate () {} }
|
Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|