前言
前面两篇文章中我们都是直接启动的OSGI框架(equinox),然后把所有bundle放到框架中运行。在平时开发中,由于历史原因,把整个应用都迁移到OSGI框架中 是一件很困难的事,但我们可以尝试把需要热更新的部分剥离出来 制作成bundle。然后在我们现有的web框架中嵌入一个OSGI框架,并使用这个嵌入的OSGI框架加载bundle,从而实现对程序的部分模块化。
本次demo是在一个典型的spring mvc框架下的web应用中,通过代码启动一个内嵌OSGI框架。
代码启动OSGI框架
由于是使用spring,可以直接定义一个spring bean来启动一个OSGI框架:
/** * osgi容器管理 */ @Component public class OsgiInit { /** * 初始化容器 */ @PostConstruct public void init() { ServiceLoader<FrameworkFactory> serviceLoader = ServiceLoader.load(FrameworkFactory.class); FrameworkFactory frameworkFactory = serviceLoader.iterator().next(); //一些启动参数 Map<String, String> properties = new HashMap<>(); properties.put("osgi.console", ""); properties.put("osgi.clean", "true"); properties.put("org.osgi.framework.bootdelegation", "sun.*,com.sun.*,javax.*"); properties.put("org.osgi.framework.system.capabilities","osgi.ee; osgi.ee=\"JavaSE\";version:List=\"1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8\""); Framework framework = frameworkFactory.newFramework(properties); try { framework.init();//初始化框架 } catch (BundleException e) { e.printStackTrace(); } try { framework.start();//启动框架 Thread thread = new Thread(() -> { try { framework.waitForStop(0); } catch (InterruptedException e) { e.printStackTrace(); } }); thread.start(); Constents.getInstence().setThread(thread); Constents.getInstence().setFramework(framework); } catch (BundleException e) { e.printStackTrace(); } System.out.println("osgi容器启动成功"); } /** * 销毁容器 */ @PreDestroy public void destroy(){ System.out.println("开始销毁容器"); try { Constents.getInstence().getFramework().stop(); } catch (BundleException e) { e.printStackTrace(); } Constents.getInstence().getThread().interrupt(); } }
这边类是由@Component注释,spring容器会在初始化这个bean之前执行@PostConstruct注释的init方法 启动OSGI框架;spring 容器在销毁之前会执行@PreDestroy注释的destroy方法,销毁OSGI框架。
OSGI框架启动成功后,会通过调用Constents.getInstence().setFramework(framework);方法把这个framework对象存起来。看下Constents的实现:
public class Constents { private static final Constents constents = new Constents(); private Thread thread; private Framework framework;//OSGI框架对象 private Map<String,Bundle> bundleMap = new HashMap<>();//OSGI框架中加载的Bundle private Constents(){} public static Constents getInstence(){ return constents; } public Thread getThread() { return thread; } public void setThread(Thread thread) { this.thread = thread; } public Framework getFramework() { return framework; } public void setFramework(Framework framework) { this.framework = framework; } public void setBundle(Bundle bundle){ this.bundleMap.put(bundle.getSymbolicName(),bundle); } public Bundle getBundle(String name){ return this.bundleMap.get(name); } }
Constents使用了单例模式生成一个唯一的Constents对象,上述代码在OSGI容器启动成功后,会把framework对象赋值给Constents对象。在web框架中可以直接调用这个对象的相关方法动态的加载Bundle。
这里我们在一个Spring mvc 的Controller中,模拟对Bundle的加载和启动:
@RequestMapping("/start") public String start(){ Framework framework = Constents.getInstence().getFramework();// try { File bundleRoot = new File("D:\\test\\bundles"); File[] files = bundleRoot.listFiles(); for (File jar :files){ Bundle temp = framework.getBundleContext().installBundle(jar.toURL().toExternalForm()); Constents.getInstence().setBundle(temp); } //启动manager bundle Constents.getInstence().getBundle("com.sky.osgi.manager").start(); } catch (Exception e) { System.out.println("安装bundle失败"); e.printStackTrace(); } return "start"; }
当然也可以在Controller中定义其他修改bundle、删除bundle等方法,以控制内嵌的OSGI容器的内部bundle运行。
相关推荐
httpcomponents-client-4.1.2-osgi-bin.zip
带有嵌入式OSGI的Spring Boot 这是一个嵌入了Felix OSGI框架的示例Spring Boot项目。 其他项目是API(接口和模型类)及其实现。 Spring Boot应用程序将这些程序包作为OSGI框架的额外程序包公开(以便能够使用公开的...
httpcomponents-client-4.1.2-osgi-bin.tar.gz
com.springsource.org.apache.commons.dbcp-sources-1.2.2.osgi.jar源码 jar包
如何解决htmlunit关于https访问证书问题,最新htmlunit,需用java8以上版本编译
赠送源代码:osgi-resource-locator-1.0.1-sources.jar; 赠送Maven依赖信息文件:osgi-resource-locator-1.0.1.pom; 包含翻译后的API文档:osgi-resource-locator-1.0.1-javadoc-API文档-中文(简体)版.zip; Maven...
标签:acegi-security-1.0.7-osgi.jar.zip,acegi,security,1.0.7,osgi,jar.zip包下载,依赖包
httpcomponents-client-4.5.4-osgi-bin.zip 内含: org.apache.httpcomponents.httpclient_4.5.4.jar 官方说明: The Apache HttpComponents™ project is responsible for creating and maintaining a toolset of...
httpcomponents-client-4.5.4-osgi-bin.tar.gz 内含: org.apache.httpcomponents.httpclient_4.5.4.jar 官方说明: The Apache HttpComponents™ project is responsible for creating and maintaining a toolset ...
行业分类-设备装置-基于OSGi的Web应用开发平台权限管理自适应设计方法.zip
spring-osgi-1.2.1-with-dependencies.zip spring-osgi-1.2.1-with-dependencies.zip spring-osgi-1.2.1-with-dependencies.zip
官网上有,方便被内网屏蔽的同事下载需求。 这些资料在官网或者其他网站都能找到,为了方便一些内网屏蔽的同事。
简要介绍talend-esb osgi etl 等知识,综合介绍了talend-ESB是什么,osgi是什么,以及TOS-ESB相关组件介绍。具体的内容,请下载仔细查看。
电子书: 9781934356401-1934356409_Modular Java - Creating Flexible Applications with OSGi and Spring .pdf 可以去Amazon上看介绍。
基于java的开发源码-OSGi 分布式通讯组件 R-OSGi.zip 基于java的开发源码-OSGi 分布式通讯组件 R-OSGi.zip 基于java的开发源码-OSGi 分布式通讯组件 R-OSGi.zip 基于java的开发源码-OSGi 分布式通讯组件 R-OSGi.zip ...
java-goinstant-auth.zip,Java应用程序的GoInstant身份验证。Java应用程序的GoInstant身份验证
spring-osgi-1.2.1-with-dependencies完整包
httpcomponents-client-4.2-alpha1-osgi-bin.zip
jar包,官方版本,自测可用
jar包,官方版本,自测可用