OFBIZ:启动之StartupLoader
任意一个JAVA程序都是从main()开始启动的,OFBIZ也不例外。OFBIZ的main()位于framework/start/src/org/ofbiz/base/start/Start.java:
public final class Start {
private static final Start instance = new Start();
public static void main(String[] args) throws StartupException {
... ...
instance.init(args, command == Command.COMMAND);
try {
if (command == Command.STATUS) {
System.out.println("Current Status : " + instance.status());
} else if (command == Command.SHUTDOWN) {
System.out.println("Shutting down server : " + instance.shutdown());
} else {
// general start
instance.start();
}
} catch (Exception e) {
e.printStackTrace();
System.exit(99);
}
}
void init(String[] args, boolean fullInit) throws StartupException {
... ...
// initialize the startup loaders
initStartLoaders();
}
private void initStartLoaders() throws StartupException {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
synchronized (this.loaders) {
// initialize the loaders
for (Map<String, String> loaderMap : config.loaders) {
if (this.serverState.get() == ServerState.STOPPING) {
return;
}
try {
String loaderClassName = loaderMap.get("class");
Class<?> loaderClass = classloader.loadClass(loaderClassName);
StartupLoader loader = (StartupLoader) loaderClass.newInstance();
loader.load(config, loaderArgs.toArray(new String[loaderArgs.size()]));
loaders.add(loader);
} catch (ClassNotFoundException e) {
throw (StartupException) new StartupException(e.getMessage()).initCause(e);
} catch (InstantiationException e) {
throw (StartupException) new StartupException(e.getMessage()).initCause(e);
} catch (IllegalAccessException e) {
throw (StartupException) new StartupException(e.getMessage()).initCause(e);
}
}
this.loaders.trimToSize();
}
return;
}
void start() throws Exception {
if (!startStartLoaders()) {
if (this.serverState.get() == ServerState.STOPPING) {
return;
} else {
throw new Exception("Error during start.");
}
}
if (config.shutdownAfterLoad) {
stopServer();
}
}
boolean startStartLoaders() {
synchronized (this.loaders) {
// start the loaders
for (StartupLoader loader : this.loaders) {
if (this.serverState.get() == ServerState.STOPPING) {
return false;
}
try {
loader.start();
} catch (StartupException e) {
e.printStackTrace();
return false;
}
}
}
return this.serverState.compareAndSet(ServerState.STARTING, ServerState.RUNNING);
}
}
OFBIZ的main()过程可以简单理解为两个步骤:init()和start()。init()初始化StartupLoader,通过initStartLoaders()实现。start()启动StartupLoader,通过startStartLoaders()实现。
StartupLoader是一个接口,实现载入器的整个生命周期。
/**
* An object that loads server startup classes.
* <p>
* When OFBiz starts, the main thread will create the <code>StartupLoader</code> instance and
* then call the loader's <code>load</code> method. If the method returns without
* throwing an exception the loader will be added to a list of initialized loaders.
* After all instances have been created and initialized, the main thread will call the
* <code>start</code> method of each loader in the list. When OFBiz shuts down, a
* separate shutdown thread will call the <code>unload</code> method of each loader.
* Implementations should anticipate asynchronous calls to the methods by different
* threads.
* </p>
*
*/
public interface StartupLoader { /**
* Load a startup class.
*
* @param config Startup config.
* @param args Command-line arguments.
* @throws StartupException If an error was encountered. Throwing this exception
* will halt loader loading, so it should be thrown only when OFBiz can't
* operate without it.
*/
public void load(Config config, String args[]) throws StartupException; /**
* Start the startup class. This method must not block - implementations
* that require thread blocking must create a separate thread and then return.
*
* @throws StartupException If an error was encountered.
*/
public void start() throws StartupException; /**
* Stop the startup class. This method must not block.
*
* @throws StartupException If an error was encountered.
*/
public void unload() throws StartupException;
}
initStartLoaders()从config.loaders获取可用的载入器信息,然后创建StartupLoader实例,调用load()方法。
private void initStartLoaders() throws StartupException {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
synchronized (this.loaders) {
// initialize the loaders
for (Map<String, String> loaderMap : config.loaders) {
if (this.serverState.get() == ServerState.STOPPING) {
return;
}
try {
String loaderClassName = loaderMap.get("class");
Class<?> loaderClass = classloader.loadClass(loaderClassName);
StartupLoader loader = (StartupLoader) loaderClass.newInstance();
loader.load(config, loaderArgs.toArray(new String[loaderArgs.size()]));
loaders.add(loader);
} catch (ClassNotFoundException e) {
throw (StartupException) new StartupException(e.getMessage()).initCause(e);
} catch (InstantiationException e) {
throw (StartupException) new StartupException(e.getMessage()).initCause(e);
} catch (IllegalAccessException e) {
throw (StartupException) new StartupException(e.getMessage()).initCause(e);
}
}
this.loaders.trimToSize();
}
return;
}
config.loaders来源于配置文件framework/start/src/org/ofbiz/base/start/start.properties。
# --- StartupLoader implementations to load (in order)
ofbiz.start.loader1=org.ofbiz.base.container.ContainerLoader
ofbiz.start.loader1.loaders=main,rmi
config是framework/start/src/org/ofbiz/base/start/Config.java的一个实例。Config类调用readConfig()过程读取start.properties中的配置信息。
public class Config {
public List<Map<String, String>> loaders;
public static Config getInstance(String[] args) throws IOException {
String firstArg = args.length > 0 ? args[0] : "";
// Needed when portoffset is used with these commands, start.properties fits for all of them
if ("start-batch".equalsIgnoreCase(firstArg)
|| "start-debug".equalsIgnoreCase(firstArg)
|| "stop".equalsIgnoreCase(firstArg)
|| "-shutdown".equalsIgnoreCase(firstArg)
|| "-status".equalsIgnoreCase(firstArg)) {
firstArg = "start";
}
String configFileName = getConfigFileName(firstArg);
Config result = new Config();
result.readConfig(configFileName, args);
return result;
}
private static String getConfigFileName(String command) {
// default command is "start"
if (command == null || command.trim().length() == 0) {
command = "start";
}
return "org/ofbiz/base/start/" + command + ".properties";
}
public void readConfig(String config, String[] args) throws IOException {
... ...
// loader classes
loaders = new ArrayList<Map<String, String>>();
int currentPosition = 1;
Map<String, String> loader = null;
while (true) {
loader = new HashMap<String, String>();
String loaderClass = props.getProperty("ofbiz.start.loader" + currentPosition);
if (loaderClass == null || loaderClass.length() == 0) {
break;
} else {
loader.put("class", loaderClass);
loader.put("profiles", props.getProperty("ofbiz.start.loader" + currentPosition + ".loaders"));
loaders.add(loader);
currentPosition++;
}
}
}
}
OFBIZ:启动之StartupLoader的更多相关文章
- 【转】Ofbiz学习经验谈
不可否认,OFBiz这个开源的系统功能是非常强大的,涉及到的东西太多了,其实对我们现在而言,最有用的只有这么几个:实体引擎.服务引擎.WebTools.用户权限管理.最先要提醒各位的是,在配置一个OF ...
- ofbiz安装优化
一. 1.安装jdk 2.安装数据库 3.安装ant yum install ant 4.编译启动ofbiz cd /ofbiz目录下 ant run-install ./startofbiz.sh ...
- OFBiz:添加实体栏位
如何添加实体栏位?这里演示为PostalAddress添加planet栏位.打开applications/party/entitydef/entitymodel.xml,找到PostalAddress ...
- gradle ofbiz 16 开发环境搭建
原 gradle ofbiz 16 开发环境搭建 2017年02月13日 10:59:19 阅读数:2702 1.安装jdk 2.配置jdk环境变量 3.eclipse 安装svn 插件 4.svn下 ...
- ApacheOFBiz的相关介绍以及使用总结(一)
由于最近一段时间在给一个创业的公司做客户关系管理CRM系统,限于人力要求(其实是没有多少人力),只能看能否有稳定,开源的半成品进行改造,而且最好不需要前端(js)相关开发人员的支援就可以把事情做成,经 ...
- OFBIZ:启动之ContainerLoader
ContainerLoader类实现StartupLoader接口,目的是装入各种Container容器. /** * An OFBiz container. A container can be t ...
- ofbiz idea 启动
1.下载gradle并安装到本地 2.idea引入gradle 3.gradle右键选择refresh,项目会重新编译并加载gradle的task 4.可以再编译一下 5.没问题的话打开,jar ap ...
- [OFBiz]开发 三
1. Debug不要在Eclipse中使用Ant来启动ofbiz, 因为在Eclipse中无法kill掉Ant的进程,而ofbiz又没有提供stop的方法.(有一个hook shutdown的方法,但 ...
- OFBiz:配置过程
OFBiz使用了大量的配置文件,整个过程有点复杂.这里将配置过程大略整理了一下,方便后面查阅. 第一层:org.ofbiz.base.start.Start启动类.该类载入org/ofbiz/base ...
随机推荐
- LeetCode 176 Second Highest Salary mysql,select 嵌套 难度:1
https://leetcode.com/problems/second-highest-salary/ Write a SQL query to get the second highest sal ...
- bicycle
http://bj.ganji.com/zixingchemaimai/611076069x.htm
- (zz) 谷歌技术"三宝"之BigTable
006年的OSDI有两篇google的论文,分别是BigTable和Chubby.Chubby是一个分布式锁服务,基于Paxos算法:BigTable是一个用于管理结构化数据的分布式存储系统,构建在G ...
- 当一个activity中按钮过多时怎么办?
这几天看极客学院的视频,跟视频中的老师学到的一些小技巧~~ .setOnClickListener(this) 通过重写this(我猜的是重写),下面有onClicked() package exam ...
- Cordova android框架详解
一.Cordova 核心java类说明 CordovaActivity:Cordova Activity入口,已实现PluginManager.WebView的相关初始化工作, 只需继承Cordova ...
- Android应用增加计时器
昨天写的Sudoku游戏需要增加计时器功能,使用Chronometer实现如下,由于Chronometer自己在调用stop之后后台的计时器还会继续增加,所以暂停功能需要额外实现: 在StartAct ...
- Linux的yum源的配置
yum实际上是管理的rpm软件包 只要连接网络就可以使用在线的yum源,不用配置本地yum源 网络yum源/etc/yum.repos.d/fedora.repo(Fedora)[fedora]nam ...
- IOS7.0 Xcode5 AutoLayout 备忘录
Xcode5 xib设置autolayout方式: 列表1: (选择两个view时可设置) 左边对齐, 右边对齐, 顶部对齐, 底部对齐, x轴中心对齐, y轴中心对齐, 文本底标线对齐, (单选择一 ...
- hibernate内部测试题(附赠答案)
一.选择题(共25题,每题2.5分,选择一项或多项,漏选错选不得分) 1.在Hibernate中,以下关于主键生成器说法错误的是( ). A.increment可以用于类型为long.short或by ...
- springboot系列之-log
配置文件以application.yml为例说明: Spring Boot默认的日志组件为Logback. 一. 日志配置参数: logging: file: #日志文件,绝对路径或相对路径 path ...