如何在另一个项目中使用我的Eclipse OSGi Java插件?

时间:2017-09-12 12:46:17

标签: java eclipse osgi

这对我来说太新了,有点复杂。我在Eclipse中使用标准的OSGi框架创建了一个插件项目。目的是使用此捆绑包连接到H2 DB。这是Activator.java:     包dbservice;

import java.sql.Connection;
import java.sql.DriverManager;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

private static BundleContext context;
private Connection conn=null;

static BundleContext getContext() {
    return context;
}


public void start(BundleContext bundleContext) throws Exception {
    Activator.context = bundleContext;
    Class.forName("org.h2.Driver");
    conn = DriverManager.getConnection("jdbc:h2:~/test","sa","123456");
    System.out.print("Connection opened...");
}

public void stop(BundleContext bundleContext) throws Exception {
    Activator.context = null;
    conn.close();
    System.out.print("Connection closed...");
}

}

我在OSGi框架内运行这个项目。它在那里工作。但我的问题是,如何在另一个项目中使用此捆绑包?

1 个答案:

答案 0 :(得分:0)

问题是您真正想要提供给其他捆绑包的内容。 如果要提供访问数据库的通用方法,则可以提供DataSource。在这种情况下,我建议使用pax-jdbc-config,因为它会为您完成所有繁重的工作。

另一个选项是Aries transactioncontrol,它甚至更容易使用,但会使您的用户代码依赖于事务控制API。

如果您想提供更高级别的服务,那么服务是最佳选择。为您的服务创建一个接口,并在您的捆绑中实现该接口。然后只需将对象导出为OSGi服务。然后其他捆绑包可以获取服务。我建议使用声明性服务来提供和使用服务。见this example