osgi框架_Intellij Idea 15中开发Maven+osgi项目(Apache felix环境)

eba78c2a0ed3091ef063197b8aa06e90.png

1.Intellij Idea创建Maven项目

利用maven模板快速创建maven项目,如图操作,填写g(groupid)a(artifactid)v(version)。

maven home directory 选择系统默认或者自己下载下来的maven主目录。

2.将项目模块化

利用maven的依赖以及集成的特性,将项目模块化。

在主module下创建module并继承主module。

这里我创建了server以及client,项目结构如下图,先忽略红圈之外的文件夹:

主pom配置如下:

4.0.0com.renming.osgi.helloworld helloworld 1.0.0serverclientpomhelloworldhttp://maven.apache.orgUTF-8com.renming.osgi.helloworld server ${project.version}org.eclipse osgi 3.9.1-v20130814-1242providedjunit junit 3.8.1test

server模块pom配置如下:

 helloworld com.renming.osgi.helloworld1.0.04.0.0  server bundleserverhttp://maven.apache.orgUTF-8org.eclipse osgi junit junit 3.8.1testorg.apache.felix maven-bundle-plugin 2.4.0true${project.version}$(replace;${project.artifactId};-;_) com.renming.osgi.helloworld.server.inter;version="${project.version}"  org.osgi.framework  com.renming.osgi.helloworld.Activator 

client模块pom配置如下:

 helloworld com.renming.osgi.helloworld1.0.04.0.0  client bundleclienthttp://maven.apache.orgUTF-8com.renming.osgi.helloworld server org.eclipse osgi junit junit 3.8.1testorg.apache.felix maven-bundle-plugin 2.4.0true${project.version}$(replace;${project.artifactId};-;_)  org.osgi.framework,com.renming.osgi.helloworld.server.inter;version="${project.version}"  com.renming.osgi.helloworld.Activator 

主pom和子pom拥有继承关系,同时进行了模块化。

3.编写简单的测试用例

通过实现BundleActivator接口,可以实现与osgi框架环境的通信。

这里需要实现start以及stop,分别在bundle启动以及终止的时候被调用。

这里让它在server bundle启动的时候注册一个简单的服务:

package com.renming.osgi.helloworld; import java.util.ArrayList;import java.util.List; import com.renming.osgi.helloworld.server.impl.HelloImpl;import com.renming.osgi.helloworld.server.inter.Hello;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceRegistration; public class Activator implements BundleActivator {  private List registrations = new ArrayList();  private static BundleContext context;  static BundleContext getContext() { return context; }  public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; System.out.println("----------------hello start---------------------"); //注册hello接口中的服务 registrations.add(bundleContext .registerService(Hello.class.getName(), new HelloImpl("Hello, OSGi"), null)); System.out.println("----------------hello start---------------------"); }  public void stop(BundleContext bundleContext) throws Exception { Activator.context = null;  for (ServiceRegistration registration : registrations) { System.out.println("unregistering: " + registration); registration.unregister(); }  } }

需要注意的是编写完代码后,需要配置server模块pom中打包插件,完整的上面已经列出了。

然后是cilent模块中测试代码,需要从bundleContext中获取到对应的服务,只需要提供服务名就可以了。具体代码如下:

package com.renming.osgi.helloworld;  import com.renming.osgi.helloworld.server.inter.Hello;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceReference; public class Activator implements BundleActivator { public void start(BundleContext ctx) {System.out.println("----------------hello client start---------------------"); ServiceReference ref = ctx.getServiceReference(Hello.class.getName()); if (ref != null) { Hello hello = null; try { hello = (Hello) ctx.getService(ref); if (hello != null) hello.sayHello(); else System.out.println("Service:Hello---object null"); } catch (RuntimeException e) { e.printStackTrace(); } finally { ctx.ungetService(ref); hello = null; } } else { System.out.println("Service:Hello---not exists"); }System.out.println("----------------hello client start---------------------");} public void stop(BundleContext ctx) throws Exception { } }

4.配置Apache felix环境

下载:http://felix.apache.org/downloads.cgi

解压之后拷贝bin、conf、bundle目录到主目录下方便执行以及修改配置,同时创建plugins文件夹用于存放我们编写的bundle。

最后目录结构如下:

---执行bin中felix.jar

---控制台显示g!无报错信息则执行成功

---执行命令lb,可以查看已启动bundle

---然后打包client模块以及server模块,并将打包的jar包拷贝到plugins中

---执行命令 install file:plugins/XXX.jar,会显示ID,然后执行start ID,最后执行lb查看是否启动成功,并打印对应的信息

由于client以及server都是在本地调用,如果需要远程调用可以参考这篇文章:

http://www.cnblogs.com/lw900320/archive/2012/06/26/2563221.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值