Oracle8i Enterprise JavaBeans and CORBA Developer's Guide Release 8.1.5 A64683-01 |
|
Overview ======== The timeout example shows how you can control session timeout from an EJB. A first client program invokes a bean method to set the session timeout value, and a second client program tests the timeout, by first calling a method on the bean in the session within the timeout interval, and then after the timeout has expired. In the second case, the method invocation should fail. In order for the second client to be able to invoke a method on the same bean in the same session, the first client saves both the bean handle and a login object reference to disk, to be read by the scond client. Source Files ============ Client1.java ----------- You invoke the first client program from a command prompt, and pass it seven arguments, which are the - service URL (service ID, hostname, port, and SID if port is a listener) - name of the published bean home interface - username - password that authenticates the client to the Oracle8i database server - a file name to which to write the login IOR - a file name to which to write the object handle - a time out value in seconds For example: % java -classpath LIBs Client1 sess_iiop://localhost:2481:ORCL \ /test/myHello scott tiger login.dat handle.dat 30 where LIBs is the classpath that must include $ORACLE_HOME/lib/aurora_client.jar $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()) - gets a login server and authenticates the client - looks up and activates a Hello object - sets the object's message to "As created by Client1" - prints "Client1: " plus the massage, the message got by invoking helloWorld() on the Hello object - sets the session timeout by invoking setTimeout() on hello - writes the login IOR and the bean handle to files The printed output is: Client2.java ----------- You invoke the second client program from a command prompt, and pass it four arguments, the - username - password - a file name from which to read the login IOR, which must be the same as passed to Client1 - a file name from which to read the object handle, the same as that passed to Client1 For example: % java -classpath LIBs Client2 sess_iiop://localhost:2481:ORCL \ scott tiger login.dat handle.dat The client code performs the following steps: - reads the login object from the disk - reads the bean handle from disk - hello.ejb --------- The bean deployment descriptor. helloServer/HelloBean.java -------------------------- The bean implementation. Implements the methods helloWorld(), setMessage(), and setTimeout(). Note that the call to Presentation.sessionTimeout() requires that following import statement: import oracle.aurora.net.Presentation; hello/Hello.java ---------------- The bean remote interface. hello/HelloHome.java -------------------- The bean's home interface. Compiling and Running the Example ================================= 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.
SessionBean helloServer.HelloBean { BeanHomeName = "test/myHello"; RemoteInterfaceClassName = hello.Hello; HomeInterfaceClassName = hello.HelloHome; SessionTimeout = 30; AllowedIdentities = { PUBLIC }; RunAsMode = CLIENT_IDENTITY; TransactionAttribute = TX_NOT_SUPPORTED; // TransactionAttribute = TX_REQIRES_NEW; // TransactionAttribute = TX_BEAN_SUPPORTED; }
import hello.Hello; import hello.HelloHome; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.ejb.Handle; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import oracle.aurora.client.*; import oracle.aurora.AuroraServices.LoginServer; public class Client1 { public static void main (String[] args) throws Exception { if (args.length != 6) { System.out.println ("usage: Client serviceURL objectName username password " + "loginIORfile objHandlefile"); System.exit(1); } String serviceURL = args [0]; String objectName = args [1]; String username = args [2]; String password = args [3]; String loginIORfile = args [4]; String objHandlefile = args [5]; // int timeout = Integer.parseInt(args [6]); Hashtable env = new Hashtable(); env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); Context ic = new InitialContext (env); LoginServer lserver = (LoginServer)ic.lookup (serviceURL + "/etc/login"); new Login (lserver).authenticate (username, password, null); // 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 ("As created by Client1"); System.out.println ("Client1: " + hello.helloWorld ()); // Make the session survive timeout seconds after its last connection // is dropped. // hello.setTimeout (timeout); // System.out.println ("Set session timeout to " + timeout + " seconds"); writeIOR (lserver, loginIORfile); // writeIOR (hello, objHandleFile); // Save the bean handle to a file for Client2 to access our session FileOutputStream fostream = new FileOutputStream (objHandlefile); ObjectOutputStream ostream = new ObjectOutputStream (fostream); ostream.writeObject (hello.getHandle ()); ostream.flush (); fostream.close (); System.out.println ("Client1: exiting..."); } static public void writeIOR (org.omg.CORBA.Object object, String iorFile) throws Exception { org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init (); String ior = orb.object_to_string (object); OutputStream os = new FileOutputStream (iorFile); os.write (ior.getBytes ()); os.close (); } }
import hello.Hello; import hello.HelloHome; import java.io.InputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.ObjectInputStream; import javax.ejb.Handle; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import oracle.aurora.client.Login; import oracle.aurora.AuroraServices.LoginServer; import oracle.aurora.AuroraServices.LoginServerHelper; public class Client2 { public static void main (String[] args) throws Exception { boolean ssl = true; if (args.length != 4) { System.out.println ("usage: Client2 username password loginIORfile objHandlefile"); System.exit (1); } String username = args [0]; String password = args [1]; String loginIORfile = args [2]; String objHandlefile = args [3]; // Initialize the ORB for accessing objects in 8i // You have to initialize the ORB that way. // You will be authenticated using the login IOR read // from the file. org.omg.CORBA.ORB orb = ServiceCtx.init (null, null, null, false, null); // Read the IORs from the IOR files String loginIOR = getIOR (loginIORfile); // String helloIOR = getIOR (objHandlefile); // Get a ref to the bean, by reading the file. FileInputStream finstream = new FileInputStream (objHandlefile); ObjectInputStream istream = new ObjectInputStream (finstream); Handle helloHandle = (Handle)istream.readObject (); finstream.close (); Hello hello = (Hello)helloHandle.getEJBObject (); System.out.println ("Client2: read the bean handle from " + objHandlefile); // Authenticate with the login Object LoginServer lserver = LoginServerHelper.narrow (orb.string_to_object (loginIOR)); lserver._bind_options (new org.omg.CORBA.BindOptions (false, false)); Login login = new Login (lserver); login.authenticate (username, password, null); System.out.println("Client2: authenticated."); // Access the object from the ior and print its message System.out.println ("Client2: " + hello.helloWorld ()); // Disconnect from the object by exiting System.out.println ("Client2: exiting..."); } // Read an IOR from an IOR file. static String getIOR (String iorFile) throws Exception { // Loop until the ior file is available InputStream is = null; int i; for (i = 0; i < 10; i++) { try { is = new FileInputStream (iorFile); } catch (FileNotFoundException e) {} Thread.sleep (1000); } if (is == null){ System.out.println ("Client2 timed out before finding " + iorFile); System.exit (1); } byte[] iorbytes = new byte [is.available ()]; is.read (iorbytes); is.close (); String ior = new String (iorbytes); System.out.println ("Client2: got the IOR from " + iorFile); return ior; } }
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 setTimeout (int seconds) throws RemoteException; }
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 helloServer; import hello.*; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.ejb.CreateException; import java.rmi.RemoteException; import oracle.aurora.net.Presentation; public class HelloBean implements SessionBean { String message; // Methods of the Hello interface public String helloWorld () throws RemoteException { return message; } public void setMessage (String message) throws RemoteException { this.message = message; } public void setTimeout (int seconds) throws RemoteException { Presentation.sessionTimeout (seconds); } // Methods of the SessionBean public void ejbCreate () throws RemoteException, CreateException {} public void ejbRemove () {} public void setSessionContext (SessionContext ctx) {} public void ejbActivate () {} public void ejbPassivate () {} }
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 $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! xxxx 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 ================================= 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 ()); } }
SessionBean helloServer.HelloBean { BeanHomeName = "test/myHello"; RemoteInterfaceClassName = hello.Hello; HomeInterfaceClassName = hello.HelloHome; AllowedIdentities = { PUBLIC }; RunAsMode = CLIENT_IDENTITY; TransactionAttribute = TX_NOT_SUPPORTED; // TransactionAttribute = TX_REQIRES_NEW; // TransactionAttribute = TX_BEAN_SUPPORTED; }
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 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 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 () {} }