Web applications 在战争中嵌入OSGI
我对在WAR中添加OSGI容器很感兴趣,但我找不到关于如何实现这一点的教程或文档。我发现有些东西根本没用。 我对Felix实现和Atlassian实现感兴趣 我愿意这样做,这样我的war就可以接受插件,我可以动态扩展我的Web应用程序,并将其部署到任何Web服务器上Web applications 在战争中嵌入OSGI,web-applications,osgi,war,osgi-bundle,embedded-osgi,Web Applications,Osgi,War,Osgi Bundle,Embedded Osgi,我对在WAR中添加OSGI容器很感兴趣,但我找不到关于如何实现这一点的教程或文档。我发现有些东西根本没用。 我对Felix实现和Atlassian实现感兴趣 我愿意这样做,这样我的war就可以接受插件,我可以动态扩展我的Web应用程序,并将其部署到任何Web服务器上 有文档或其他东西的链接吗?非常感谢您的帮助。向web应用程序添加OSGi框架启动器不是什么大问题 您需要添加一个侦听器来启动web.xml中的框架启动器 at.badgateway.StartupListener startup
有文档或其他东西的链接吗?非常感谢您的帮助。向web应用程序添加OSGi框架启动器不是什么大问题 您需要添加一个侦听器来启动web.xml中的框架启动器
at.badgateway.StartupListener
startuplistener可能是这样的
public class StartupListener implements ServletContextListener {
//vars
@Override
public void contextInitialized(ServletContextEvent event) {
// set props
Map<String, String> config = new HashMap<String, String>();
config.put(Constants.FRAMEWORK_STORAGE, "path to cache");
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
try {
// get framework and start it
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
framework = frameworkFactory.newFramework(config);
framework.start();
// start existing bundles
bundleContext = framework.getBundleContext();
starter = new MyBundleStarter(servletContext, bundleContext);
starter.launch();
} catch (Exception ex)
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// stop framework
}
}
公共类StartupListener实现ServletContextListener{
//瓦尔斯
@凌驾
公共void contextInitialized(ServletContextEvent事件){
//设置道具
Map config=newhashmap();
config.put(Constants.FRAMEWORK_存储,“缓存路径”);
config.put(Constants.FRAMEWORK_STORAGE_CLEAN,“true”);
试一试{
//获取框架并启动它
FrameworkFactory=ServiceLoader.load(FrameworkFactory.class).iterator().next();
frameworkFactory.newFramework(配置);
framework.start();
//启动现有捆绑包
bundleContext=framework.getBundleContext();
starter=新的MyBundleStarter(servletContext,bundleContext);
starter.launch();
}捕获(例外情况除外)
}
@凌驾
公共无效上下文已销毁(ServletContextEvent arg0){
//停止框架
}
}
注意上面引号中的MyBundlestarter类,它是激活war中包含的所有捆绑包的类。(例如/WEB-INF/Osgi捆绑包)
import org.osgi.framework.Bundle;
导入org.osgi.framework.BundleContext;
公共类MyBundleStarter{
私有BundleContext BundleContext=null;
public void launch()引发异常{
ArrayList availableBundles=新的ArrayList();
//获取并打开可用的捆绑包
对于(URL:getBundlesInWar()){
Bundle Bundle=bundleContext.installBundle(url.getFile(),url.openStream());
可用捆绑。添加(捆绑);
}
//开始打包
用于(捆绑包:可用捆绑包){
试一试{
bundle.start();
}捕获()
}
私有列表getBundlesInWar()引发异常{
//返回位于目标位置的URL列表
}
}
最后但并非最不重要的一点是,您必须向项目中添加osgi框架
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
</dependency>
org.apache.felix
org.apache.felix.framework
或
org.eclipse.osgi
org.eclipse.osgi
如果您使用WebLogic托管您的应用程序,您可以在WAR中嵌入OSGi捆绑包,并将其部署到系统定义的OSGi服务器。这很好,因为可以在WebLogic日志中自动看到来自OSGi日志服务的日志消息。此外,当您取消部署时,您的捆绑包将从目标OSGi服务器中删除r应用程序
有关更多信息,请参阅或此。我可以看出这是一篇旧文章,但可能对某些人有用: 这篇文章在这个主题中包含了很多有用的东西,至少对我来说是一个很大的帮助。
值得一看该页面上的其他帖子。Martin..你能给我一些文档吗?一些指向OSGI实现的链接,允许你这样做吗?等等此代码与equinox和felix一起工作。只需将org.eclipse.OSGI或org.apache.felix.framework添加到pom中,它就会被检测到,因为它们包含一个类impl正在修订FrameworkFactory-Interface。太好了。我回家后会试试。谢谢注意:添加felix文件安装为捆绑包可能会很有用,这增加了在测试环境中添加捆绑包的可能性。
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
public class MyBundleStarter{
private BundleContext bundleContext = null;
public void launch() throws Exception {
ArrayList<Bundle> availableBundles= new ArrayList<Bundle>();
//get and open available bundles
for (URL url : getBundlesInWar()) {
Bundle bundle = bundleContext.installBundle(url.getFile(), url.openStream());
availableBundles.add(bundle);
}
//start the bundles
for (Bundle bundle : availableBundles) {
try{
bundle.start();
}catch()
}
private List<URL> getBundlesInWar() throws Exception {
// returns a list of URLs located at destination
}
}
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>org.eclipse.osgi</artifactId>
</dependency>