如何在所有捆绑包启动后调用osgi应用程序的start方法?

Jee*_*tha 0 java osgi equinox

我有一个在equinox上运行的osgi应用程序.这就是bundle在bootstrap类中的启动方式.

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();
   }
Run Code Online (Sandbox Code Playgroud)

我的应用程序中有一个启动方法在其中一个包中.在启动所有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)
Run Code Online (Sandbox Code Playgroud)

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

Nei*_*ett 5

您的bundle install/start循环是一种反模式.问题是启动一个bundle立即强制它解决,但它可能无法解决,因为它的依赖关系尚未安装(可能是因为它们在列表的后面出现).

这可能会提示您事先确定正确的安装顺序,但这也是错误的答案.你应该让OSGi框架为你工作依赖项(这是OSGi的重点!).

所以,你需要安装所有包的第一个,然后再开始任何人.为此你需要两个循环,例如:

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