package examples.workspace; import java.net.*; import java.util.*; import java.sql.*; import weblogic.db.jdbc.*; import weblogic.common.*; import weblogic.workspace.common.*; /** * This code example shows how a T3Client can cache arbitrary * data in its workspace on the WebLogic Server. This example uses * a very simple piece of data -- a cookie -- to show how to * set up data for caching on the WebLogic Server. Any data that you * can add to a ParamSet can be cached. *

*

To set up this example:

*
    *
  1. Set up your development shell as described in Setting up your environment. *

    *

  2. Compile this example by executing the following command in your development shell: * *

    * *$ javac -d %CLIENT_CLASSES% datacache.java * * *

    *

  3. Start WebLogic Server in a separate shell. * *

    *

  4. Run this example by executing the following command in your development shell: * *

    * *$ java examples.workspace.datacache t3://hostname:port *

    Where hostname:port is the host name and port number of your WebLogic Server. *

* * @author Copyright (c) 1996-1999 by BEA Systems. * All Rights Reserved. */ public class datacache { /** * In the main, the T3Client connects to the WebLogic Server and sets its * soft disconnect timeout to "never." Setting the soft disconnect * to "never" prevents the WebLogic Server from cleaning up (or destroying) * a T3Client's resource in the WebLogic Server even if the T3Client goes * away. We get the T3Client's Workspace ID, which we will use as a * token to identify our Workspace when reconnecting after a * disconnect. Then the T3Client is disconnected. *

* The ParamSet that we will save into the T3Client's Workspace is * created. We use a loop to create, connect, and disconnect the T3Client, * each time connecting to the original T3Client Workspace by * supplying the Workspace ID when the T3Client is created. * Within the loop, we set a different ParamValue * and store the ParamSet in the Workspace; then we disconnect and * connect again, and retrieve the data that we stored in the * previous session. *

* Finally, we connect one last time and set the soft disconnect * timeout to zero. This forces an immediate clean up of the * T3Client's resources in the WebLogic Server after we've finished with * this client's Workspace. */ public static void main(String argv[]) throws Exception { if (argv.length != 1) { System.out.println("Usage: java examples.workspace.datacache WebLogicURL"); System.out.println("Example: java examples.workspace datacache t3://localhost:7001"); return; } String weblogicURL = argv[0]; // Create T3Client T3Client t3 = null; String wsid = null; t3 = new T3Client(weblogicURL); // Connect and set the timeout to be unlimited t3.connect() .setSoftDisconnectTimeoutMins(T3Client.DISCONNECT_TIMEOUT_NEVER); System.out.println("Setting soft disconnect timeout to 'never'"); WorkspaceDef defaultWS = t3.services.workspace().getWorkspace(); wsid = defaultWS.getID(); System.out.println("Workspace ID for this client is " + wsid); // Disconnect the client. Since the soft disconnect timeout interval // is large, the server will preserve the session. t3.disconnect(); System.out.println("T3Client disconnected"); ParamSet cookie = new ParamSet(); System.out.println("Created a ParamSet"); for (int i = 0; i < 10; i++) { t3 = new T3Client(weblogicURL, wsid); t3.connect(); System.out.println("T3Client " + i + " connected at Workspace ID " + wsid); // Send the data to the WebLogic Server Workspace cookie.setParam("CookieValue", i); defaultWS = t3.services.workspace().getWorkspace(); defaultWS.store("mycookie", cookie); System.out.println("Stored the ParamSet: " + i); // Disconnect the client and reconnect. t3.disconnect(); System.out.println("Disconnected"); t3 = new T3Client(weblogicURL, wsid); t3.connect(); // Retreive the data from the WebLogic Server Workspace defaultWS = t3.services.workspace().getWorkspace(); int val = ((ParamSet)defaultWS.fetch("mycookie")) .getParam("CookieValue").asInt(); System.out.println("T3Client " + i + " retrieved value: " + val); t3.disconnect(); } // Sets the timeout interval to 0 to force an immediate // disconnect on the server. t3 = new T3Client(weblogicURL, wsid); t3.connect(); t3.setSoftDisconnectTimeoutMins(0); t3.disconnect(); System.out.println("Final disconnect -- Completed."); } }