Writing a Weblogic Startup Class

zhaozj2021-02-08  287

Writing a Weblogic Startup Class

October 4, 2001 CopyRight © 2001 Stephen Davies

Introduction

This paper presents a tutorial on writing a startup class for WebLogic 6.x. It is targeted at developers who would like to use this WebLogic proprietary feature. It covers code creation and deployment into the WebLogic runtime system.

The topic of application specific data is frequently discussed in the J2EE and WebLogic newsgroups. All applications have a requirement to place frequently accessed, read-mostly global data somewhere that it can be easily used. The usual answer to these kinds of questions is to place The Global Data Into Jndi. Why? Because The Powers, Have Decided That This Is The Accepted Way of Doing IT.

So? If the accepted way of of making global application data available is to place it into the naming service (JNDI) then how can this be done? Easy, implement a startup class and have WebLogic invoke it as part of its runtime initialization. This Can Be Summarized As:

Create and Compile The Startup Class Place The Startup Class on The Servers ClassPath Register The Startup Class Via The Console Start The Server

Creating The Startup Class

It all begins with the creation of the startup class. BEA have made this quite straight forward, simply create a class that implements the interface weblogic.common.T3StartupDef. If you're wondering about the 'T3', the unofficial word is that it Stands for tengah 3, Which I believe Was the old name for the product before the weblogic company was purchasd by bea. It used to be tengah by weblogic, count. Weblogic by bea.

Create the class and implement the two callback methods. The first of these is the setServices method. The T3ServicesDef parameter is an object that gives you access to WebLogic proprietary internals, things such as the connection pools, workspaces, time and logging services. Much of this has now become obsolete under J2EE. For example, you use DataSources instead of directly accessing the connection pools. For this example the method can be left as a no-op. If you wanted to access the T3Services as part of your startup logic then you could store the services object in an instance variable.The bulk of the startup logic is placed into the startup method. The startup method takes two parameters, the registered name and a hashtable. The name is set during deployment and can be used to differentiate Between Two Registered Instances of The Same Startup Class. The Hashtable Contains The Arguments, Key / Value Pairs Which Are Specified During Configuration. Thase Can Be Any String Values. TRING RETURNED by The Startup Method Is Placed INTO The LOG. The Class Skeleton Looks Like this:

Public Class StartupSample Implements T3StartUpdef {

Public void setservices (T3ServicesDef Services) {}

Public String Startup (String Name, Hashtable Args) Throws Exception {

// do Startup Actions Here

Return "Message to Appear in Log";

}

}

The coding of the startup method is quite simple, in this example a context is created within the JNDI tree and any arguments are bound into the new context. It was done this way to prevent name clashes, having a separate context prevents this from happening. Binding the arguments into the JNDI tree demonstrates what is possible and makes it easy to change. You can use the console to modify the actual data being registered into the tree. The full startup method is shown below. The complete startup class source code can be Found he.public string startup (string name, havehtable args) throws exception {

System.out.Println ("Startup Class Invoked -" Name);

InitialContext IC = New InitialContext ();

Context StartupContext = Ic.createSubContext ("startup");

// Bind Each Argument Using ITS Name

Enumeration Keys = args.keys ();

While (keys.hasmoreElements ()) {

String key = (string) keys.nextelement ();

String value = (string) args.get (key);

StartupContext.bind (key, value);

}

Return "OK";

}

Deploying the Startup Class

Setting the classpath

There is a small trick to getting the startup class to run. It must be placed on the servers classpath. That means that you need to modify the WebLogic startup script. You will need to create a directory to store your classes and place this into the . CLASSPATH environment variable For example you could create the directory path x: /bea/wlserver6.1/custom_classes and modify the startWeblogic.bat file to include it on the classpath you must be careful which classes you place into this directory, classes loaded. from the servers CLASSPATH can not be hot deployed If a class changes then the server must be restarted A startup class can not be part of a JAR, EAR or WAR file;.. startup classes are invoked before component classloaders are created.Registering the Startup class

Registering the startup class involves starting the WebLogic Server and opening the administration console in a browser. In the Deployments folder you will find the Startup & Shutdown option. From here you can elect to Configure a new Startup class. The configuration values ​​are fairly self explanatory , the name is the String that is passed as the first parameter to the startup method. It can be set to whatever you like. The classname is the fully qualified name of the startup class. The deployment order is a number used to determine startup order When there is more than one startup class.

Arguments allows you specify a comma separated list of key / value pairs. For example, 'abc = 1, def = 2' is a valid argument string. It identifies the argument 'abc' as having the value '1' and a second key of 'def' having the value '2'. Note that both the key and value are Java Strings. These arguments are passed to the startup method as the hashtable. If for some reason you need to specify a String that contains embedded blanks you must use the double quote character around the space delimited text. The abort startup on failure option will abort the server startup if an exception is thrown by the startup method. Best not to turn this on until you have the class working, if you can not start the server you can not run the console which makes it difficult to modify a poorly configured server.When you've created the registration do not forget to target it to the server (s) that you want the startup class to run on . When you've finished you will need to restart the WebLogic Server.

Summary

NOW you know how easy it is to ustement a weblogic startup class. All it takes is 4 Easy Steps:

Create and Compile The Startup Class Place The Startup Class on The Servers Classpath Start The Server Register THE Startup Class Via The Console Restart The Server

Enjoy!

Appendix - Code Sample

Package sjdavies.wls61.startup;

Import javax.naming. *;

Import WebLogic.common. *;

Import java.util.hashtable;

Import java.util.enumeration;

/ **

* An Example of a Weblogic Startup Class.

* Binds Some Sample Values ​​Into The Naming Service.

*

* The top level context "Startup" is created and then the startup

* Classes Arguments Are Bound As Key / Value Pairs Into this context.

*

* CopyRight (C) 2001, Davies Consulting Pty. Ltd. Ltd.

*

* @Author Stephen Davies

* /

Public Class StartupSample Implements T3StartUpdef {

Public void setservices (T3ServicesDef Services) {}

/ **

* Bind Selected Values ​​Into The Naming Service.

* /

Public String Startup (String Name, Hashtable Args) Throws Exception {

System.out.Println ("Startup Class Invoked -" Name);

InitialContext IC = New InitialContext ();

Context StartupContext = Ic.createSubContext ("startup");

// Bind Each Argument Using ITS Name

Enumeration Keys = args.keys ();

While (keys.hasmoreElements ()) {

String key = (string) keys.nextelement ();

String value = (string) args.get (key);

StartupContext.bind (key, value);

}

Return "OK";

}

}

转载请注明原文地址:https://www.9cbs.com/read-1204.html

New Post(0)