java - 如何在所有包启动后调用 osgi 应用程序的启动方法?

标签 java osgi equinox

我有一个在春分运行的 osgi 应用程序。这就是在引导类中启动 bundle 的方式。

String[] equinoxArgs = new String[]{"-console"};
EclipseStarter.setInitialProperties(getInitialProperties());
BundleContext context = EclipseStarter.startup(equinoxArgs, null);
List<URL> urls = getListOfBundleUrls();
   for(URL url: urls) {
       Bundle bundle = context.installBundle(url.toString());
       bundle.start();
   }

我的应用程序中有一个启动方法,位于其中一个包中。应在所有 bundle 启动以运行应用程序后调用此方法。当在引导类中调用该方法时,它会给出错误,指出在类路径中找不到某些类。 这是堆栈跟踪。

Initial SessionFactory creation failed.org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver com.mysql.jdbc.Driver class not found
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.cc.erp.platform.dbutils.services.BasicDBManager.buildSessionFactory(BasicDBManager.java:26)
at com.cc.erp.platform.dbutils.services.BasicDBManager.<clinit>(BasicDBManager.java:12)
at com.cc.erp.platform.dbutils.DBAgent.getNewCRUDService(DBAgent.java:19)
at com.cc.erp.reload.core.WebService.forQuery(WebService.java:51)
at com.cc.erp.reload.ui.CommandLineUserInterface.<init>(CommandLineUserInterface.java:27)
at com.cc.erp.helius.bootstrap.Bootstrap.launchHelius(Bootstrap.java:41)
at com.cc.erp.helius.bootstrap.Bootstrap.main(Bootstrap.java:22)
Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver com.mysql.jdbc.Driver class not found
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:107)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89)

包 com.mysql.jdbc 在 osgi 运行时导出。但它并非不在引导类路径中。我相信应该从框架本身调用此方法。请告诉我最好的方法。

最佳答案

您的 bundle 安装/启动循环是一种反模式。问题是,立即启动 bundle 会强制它解析,但它可能无法解析,因为它的依赖项尚未安装(可能是因为它们在列表中较晚)。

这可能会提示您提前计算出正确的安装顺序,但这也是错误的答案。您应该让 OSGi 框架为您处理依赖项(这就是 OSGi 的意义!)。

因此,您需要首先安装所有 bundle ,然后再启动任何 bundle 。为此,您需要两个循环,例如:

List<Bundle> installedBundles = new ArrayList<Bundle>();
for (URL url : urls) {
    installedBundles.add(context.installBundle(url.toString()));
}
for (Bundle bundle : installedBundles) {
    bundle.start();
}

关于java - 如何在所有包启动后调用 osgi 应用程序的启动方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15964061/

相关文章:

从SVN到服务器: How to compile automatically?上的Tomcat的Java类文件

java - OSGi getServiceReference 返回 null

eclipse - JavaFx8 + OSGi + Gradle 多项目的工作模块化示例,无需额外工具?

java - 使用 DS 多次实例化 Bundle

java - 启用 Java 安全管理器 OSGi Equinox 并限制 bundle 权限

java - 在Java中直接访问ArrayList中的列

java - SQLite数据库创建openOrCreateDatabase,什么是应用程序数据库的正确路径

java - 使用 java 7 编译 Equinox 3.8.2 项目

java - 为什么我的 InputStream 保留旧值?

osgi - 如何在osgi蓝图xml中指定枚举?