我正在尝试从 Java Web 应用程序调用 OSGi 包的方法。两者都应该在 Tomcat 7 上运行。
我已经编写了一个从 OSGi 包中调用方法的普通 Java 应用程序,如本网站所述:http://drupal.osgibook.org/node/37 .
为了获取 Equinox 环境的上下文,我从应用程序启动它并从内部安装 bundle 。此外,上下文用于检索正在运行的包的服务引用并获取其服务。
EquinoxRunner类的runEquinox方法:
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
public BundleContext runEquinox([...]) throws Exception {
[...]
BundleContext bundleContext = EclipseStarter.startup(new String[]{"-console"}, null);
bundleContext.installBundle("file:C:/.../plugins/myosgiclass.interface_1.0.0.201108301327.jar");
Bundle bundleTranslationImpl = bundleContext.installBundle("file:C:/.../plugins/myosgiclass.impl_1.0.0.201108301327.jar");
bundleTranslationImpl.start();
[...]
return bundleContext;
}
和 ServiceRunner 类的 invokeMethod:
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
[...]
public Object invokeMethod(BundleContext bundleContext, Object value, [...]){
ServiceReference serviceReference = bundleContext.getServiceReference(MyOSGiClass.class.getName());
Object result = null;
if (serviceReference != null) {
MyOSGiClass myOSGiClass = (MyOSGiClass) bundleContext.getService(serviceReference);
if (myOSGiClass != null) result = myOSGiClass.method(value);
bundleContext.ungetService(serviceReference);
}
return result;
}
现在,在 Tomcat 上使用 eclipse bridge ,我不知道如何检索 Equinox 环境的正确上下文。当我尝试使用 Equinox 在 Tomcat 上运行它时,出现 NoClassDefFound 异常。对于如何解决此问题的任何建议,我将不胜感激。
非常感谢。 干杯,尼克
最佳答案
当您嵌入这样的 OSGi 框架,然后想要从外部环境访问 OSGi 服务时,您需要确保服务接口(interface)在 OSGi 内部和外部是相同的。
因此,配置您的 OSGi 容器以将您的服务接口(interface)包从 Tomcat 导出到 OSGi。为此,请使用“FRAMEWORK_SYSTEMPACKAGES_EXTRA”属性配置您的 OSGi 框架。
有关 http://felix.apache.org/site/apache-felix-framework-launching-and-embedding.html#ApacheFelixFrameworkLaunchingandEmbedding-hostservices 的更多信息(即使是针对 Apache Felix,嵌入 API 也是标准化的)。
关于java - 在 Tomcat 中使用来自 Java Web 应用程序的 OSGi 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7257084/