【发布时间】:2013-11-01 03:08:18
【问题描述】:
我现在正在研究 Opendaylight 项目(一个由 Cisco 领导的开源 SDN 控制器项目),发现该项目使用 apache.felix.dm 包中的资源来动态管理对 equinox(一个 OSGi 框架)的服务依赖) 在运行时。
为了了解dm的机制,我追踪了apache.felix.dm包下的ComponentImpl.java中的代码。我现在理解的是:
- DependencyManager 将在所有服务依赖项都满足后调用 init() 方法。
- start() 方法将在 init() 方法之后但在类提供的服务在 OSGi 框架上注册之前调用。
代码编写的调用机制如下(均位于ComponentImpl.java中):
调用init()的方法:
private void activateService(State state) {
String init;
synchronized (this) {
init = m_callbackInit;
}
// service activation logic, first we initialize the service instance itself
// meaning it is created if necessary and the bundle context is set
initService();
// now is the time to configure the service, meaning all required
// dependencies will be set and any callbacks called
configureService(state);
// flag that our instance has been created
m_isInstantiated = true;
// then we invoke the init callback so the service can further initialize
// itself
invoke(init);
// see if any of this caused further state changes
calculateStateChanges();
}
调用start()的方法:
private void bindService(State state) {
String start;
synchronized (this) {
start = m_callbackStart;
}
// configure service with extra-dependencies which might have been added from init() method.
configureServiceWithExtraDependencies(state);
// inform the state listeners we're starting
stateListenersStarting();
// invoke the start callback, since we're now ready to be used
invoke(start);
// start tracking optional services
startTrackingOptional(state);
// register the service in the framework's service registry
registerService();
// inform the state listeners we've started
stateListenersStarted();
}
现在我的问题是:init() 和 start() 方法有什么区别?既然它们都是在服务注册到 OSGi 框架之前被调用的,为什么需要将它们分开呢?感谢您的任何想法,如果我的任何理解不正确,请告诉我。
最好的问候,
杰
【问题讨论】:
-
按照init的概念将其加载到类中,在调用时准备好,start调用
-
对不起,你能解释一下你提到的“电话”吗?是不是类似于“回调”的功能?
标签: java osgi apache-felix