内嵌式--代码启动OSGI框架
前言
前面两篇文章中我们都是直接启动的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运行。