设为首页 收藏本站
查看: 599|回复: 0

[经验分享] Writing a WebLogic Startup Class

[复制链接]

尚未签到

发表于 2017-2-15 07:30:28 | 显示全部楼层 |阅读模式
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 that be, the ones who write the specs, 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 purchased by BEA. It used to be Tengah by WebLogic, now its 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. These can be any String values. The String 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 here.

  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";
}
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 cannot be hot deployed. If a class changes then the server must be restarted. A startup class cannot 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. Lowest number goes first.
  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't start the server you can't run the console which makes it difficult to modify a poorly configured server.
  When you've created the registration don't 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 implement 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.
*
* @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";
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-342199-1-1.html 上篇帖子: weblogic Security:090310]Failed to create resource 下篇帖子: Eclipse中如何配置weblogic服务!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表