百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

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

ccwgpt 2024-10-01 08:00 24 浏览 0 评论

1.Intellij Idea创建Maven项目

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

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

2.将项目模块化

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

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

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

主pom配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 
 <groupId>com.renming.osgi.helloworld</groupId>
 <artifactId>helloworld</artifactId>
 <version>1.0.0</version>
 <modules>
 <module>server</module>
 <module>client</module>
 </modules>
 <packaging>pom</packaging>
 
 <name>helloworld</name>
 <url>http://maven.apache.org</url>
 
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 
 <dependencyManagement>
 <dependencies>
 <dependency>
 <groupId>com.renming.osgi.helloworld</groupId>
 <artifactId>server</artifactId>
 <version>${project.version}</version>
 </dependency>
 <dependency>
 <groupId>org.eclipse</groupId>
 <artifactId>osgi</artifactId>
 <version>3.9.1-v20130814-1242</version>
 <scope>provided</scope>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 </dependencies>
 </dependencyManagement>
</project>

server模块pom配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <parent>
 <artifactId>helloworld</artifactId>
 <groupId>com.renming.osgi.helloworld</groupId>
 <version>1.0.0</version>
 </parent>
 <modelVersion>4.0.0</modelVersion>
 
 <artifactId>server</artifactId>
 <packaging>bundle</packaging>
 
 <name>server</name>
 <url>http://maven.apache.org</url>
 
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 
 <dependencies>
 <dependency>
 <groupId>org.eclipse</groupId>
 <artifactId>osgi</artifactId>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 </dependencies>
 
 <build>
 <plugins>
 <plugin>
 <groupId>org.apache.felix</groupId>
 <artifactId>maven-bundle-plugin</artifactId>
 <version>2.4.0</version>
 <extensions>true</extensions>
 <configuration>
 <instructions>
 <Bundle-Version>${project.version}</Bundle-Version>
 <Bundle-SymbolicName>$(replace;${project.artifactId};-;_)</Bundle-SymbolicName>
 <Export-Package>
 com.renming.osgi.helloworld.server.inter;version="${project.version}"
 </Export-Package>
 <Import-Package>
 org.osgi.framework
 </Import-Package>
 <Bundle-Activator>
 com.renming.osgi.helloworld.Activator
 </Bundle-Activator>
 </instructions>
 </configuration>
 </plugin>
 </plugins>
 </build>
</project>

client模块pom配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <parent>
 <artifactId>helloworld</artifactId>
 <groupId>com.renming.osgi.helloworld</groupId>
 <version>1.0.0</version>
 </parent>
 <modelVersion>4.0.0</modelVersion>
 
 <artifactId>client</artifactId>
 <packaging>bundle</packaging>
 
 <name>client</name>
 <url>http://maven.apache.org</url>
 
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 
 <dependencies>
 <dependency>
 <groupId>com.renming.osgi.helloworld</groupId>
 <artifactId>server</artifactId>
 </dependency>
 <dependency>
 <groupId>org.eclipse</groupId>
 <artifactId>osgi</artifactId>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 </dependencies>
 
 
 <build>
 <plugins>
 <plugin>
 <groupId>org.apache.felix</groupId>
 <artifactId>maven-bundle-plugin</artifactId>
 <version>2.4.0</version>
 <extensions>true</extensions>
 <configuration>
 <instructions>
 <Bundle-Version>${project.version}</Bundle-Version>
 <Bundle-SymbolicName>$(replace;${project.artifactId};-;_)
 </Bundle-SymbolicName>
 <Import-Package>
 org.osgi.framework,com.renming.osgi.helloworld.server.inter;version="${project.version}"
 </Import-Package>
 <Bundle-Activator>
 com.renming.osgi.helloworld.Activator
 </Bundle-Activator>
 </instructions>
 </configuration>
 </plugin>
 </plugins>
 </build>
</project>

主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<ServiceRegistration> registrations = new ArrayList<ServiceRegistration>();
 
 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

相关推荐

netty系列之:使用Jboss Marshalling来序列化java对象

简介在JAVA程序中经常会用到序列化的场景,除了JDK自身提供的Serializable之外,还有一些第三方的产品可以实现对JAVA对象的序列化。其中比较有名的就是Googleprotobuf。当然...

6款可替代dreamweaver的工具

dreamweaver对一个web前端工作者来说,再熟悉不过了,像我07年接触web前端开发就是用的dreamweaver,一直用到现在,身边的朋友有跟我推荐过各种更好用的可替代dreamweaver...

Java—类加载的基本机制和过程

类加载的基本机制和过程运行Java程序,就是执行java这个命令,指定包含main方法的完整类名,以及一个classpath,即类路径。类路径可以有多个,对于直接的class文件,路径是class文件...

什么是双亲委派机制?(转载)

原文章地址:https://www.cnblogs.com/hollischuang/p/14260801.html什么是双亲委派机制首先,我们知道,虚拟机在加载类的过程中需要使用类加载器进行加载,而...

[架构师必看]我在系统设计上犯过的14个错

在上篇《架构师画像》的文章中提到了自己在系统设计上犯过的一些错,觉得还挺有意义的,这篇文章就来回顾下自己近八年来所做的一些系统设计,看看犯的一些比较大的血淋淋的错误(很多都是推倒重来),这八年来主要做...

ONOS架构之子系统介绍

前言:为了方便灵活性,ONOS采取的是一种模块化结构,一方面能灵活地组织各种模块,容易让开发者扩展出新的模块,同时通过隔离令系统的模块各司其职而不会互相干扰。实际上ONOS是由多个子系统组成,本文将对...

基于微信小程序的在线课堂系统设计与实现-计算机毕业设计源码

摘要随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱,在线课堂微信小程序被用户普遍使用,为方便用户能够...

微信小程序云开发教室预约系统的前后端交互与数据库设计

需求描述:需要申请使用教室时,可点击教室申请查看教室的使用状况及相关设备。确定好需要的教室后,按学期、校区、教学楼、周次、星期、节次、等维度筛选,并备注用途。例如:当我点击该教室申请占用后,该教室状态...

微信小程序开发准备材料以及方式

这里讲述小程序注册类型为企业类型时所需要的资料,首先需要一个新的邮箱号,作为登陆账号,需要管理员或者法人的身份信息、已绑定银行卡的微信号、手机号、营业执照、开户银行信息,或者一些特殊行业所需要的办理的...

webman 事务回滚失效问题记录

大家好,我是yangyang.最近有用到webman这个框架写业务,写代码的过程中,遇到了一个奇葩的问题:基于webman下使用laravel的orm组件事务回滚不生效简单介绍下webmanwebma...

PHP实时通信:Workerman篇

一般做Web开发,用的是HTTP协议进行通信,是一个简单的请求-响应协议。做PHP开发的都很清楚这一点。只能由浏览器发起请求,服务器响应内容。服务器不能主动向浏览器推送消息。多个浏览器之间也不能互相发...

PHP培训课程内容都有哪些?PHP培训哪些内容?

作为一门经久不衰的开发语言,php开发工程师一直是很多年轻人选择学习和就业的职业方向,那么PHP培训课程主要学习哪些内容呢?一、企业级开发专题:深入剖析企业实际开发过程,教授最实用的企业级技术PHP7...

go 和 php 性能如何进行对比?

PHP性能很差吗?每次讲到PHP和其他语言间的性能对比,似乎都会发现这样一个声音:单纯的性能对比没有意义,主要瓶颈首先是数据库,其次是业务代码等等。好像PHP的性能真的不能单独拿出来讨论似的。但其实一...

突然发现php工作变少了

突然发现php工作变少了。好像不大行了,被go取代了魔法涂鸦python和php对比如下:1.python依赖管理需然简单,但依赖本身做的比较宽松,一但版本更新,或修改,就有一堆问题;2.传统py...

php高并发的瓶颈到底在哪

php高并发的瓶颈到底在哪?是同步阻塞?还是nginx+fpm不断创建-销毁进程资源过度消耗?高并发到底是什么问题,是语言问题嘛,为什么说php不适合高并发?求大佬指点从2009年后一直用lnmp,从...

取消回复欢迎 发表评论: