欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OSGI和Maven结合

程序员文章站 2022-04-28 09:53:54
...

1、创建项目

创建一个maven项目,并在该项目下创建两个maven module,一个为server,令一个为client

server为发布服务的bundle,client为使用服务的bundle

2、代码实现

与之前的helloworld一样,写代码

server中定义接口,接口的实现类,实现BundleActivator的类(发布服务)

client中,实现BundleActivator的类(使用服务)

结构如下:

OSGI和Maven结合

 

 

 

1、server模块

IHelloWorld接口

public interface IHelloWorld {
    String sayHello(String name);
}

实现接口HelloWorldImp

public class HelloWorldImp implements IHelloWorld {
    @Override
    public String sayHello(String name) {
        return "你好" + name;
    }
}

HelloWorldBundle(实现BundleActivator的类,为该bundle的启动类)

public class HelloWorldBundle implements BundleActivator{
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("HelloWorldBundle");
        IHelloWorld server = new HelloWorldImp();
        Dictionary<String, Object> properties = new Hashtable<>();
        bundleContext.registerService(IHelloWorld.class, server, properties);
        System.out.println("发布服务");
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {

    }
}

2、client模块

ClinetBundle(该bundle的启动类)

public class ClinetBundle implements BundleActivator {
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("Client");
        ServiceReference<IHelloWorld> reference = bundleContext.getServiceReference(IHelloWorld.class);
        System.out.println("reference-------" + reference);
        if (Objects.nonNull(reference)){
            IHelloWorld server = bundleContext.getService(reference);
            if (Objects.nonNull(server)){
                System.out.println("收到服务");
                System.out.println(server.sayHello("zsy"));
            }
            bundleContext.ungetService(reference);
        }
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {

    }
}

3、pom.xml配置

1、主pom

<?xml version="1.0" encoding="UTF-8"?>
<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>osgi_maven</groupId>
    <artifactId>osgi_maven</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0</version>
    <modules>
        <module>server</module>
        <module>client</module>
    </modules>

    <name>osgi_maven</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>osgi_maven</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>

2、server模块pom.xml

注意点:<packaging>bundle</packaging>这个配置一定要存在,否则maven-bundle-plugin打包时,configuration中的配置信息不会打到MANIFEST文件中,导致bundle安装后,start不会启动该bundle

<?xml version="1.0" encoding="UTF-8"?>
<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>osgi_maven</artifactId>
        <groupId>osgi_maven</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>
    </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>
                            oage.demo.server;version="${project.version}"
                        </Export-Package>
                        <Import-Package>
                            org.osgi.framework;version="${project.version}"
                        </Import-Package>
                        <Bundle-Activator>
                            oage.demo.server.HelloWorldBundle
                        </Bundle-Activator>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

3、client模块的pom.xml

注意点:client模块依赖了server模块中的IHelloWorld接口,在打包的时候在configuration中的instructions中要添加<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>,这样该依赖才会打入到包中,启动时才能找到依赖,否则后报错

<?xml version="1.0" encoding="UTF-8"?>
<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>osgi_maven</artifactId>
        <groupId>osgi_maven</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>org.eclipse</groupId>
            <artifactId>osgi</artifactId>
        </dependency>
        <dependency>
            <groupId>osgi_maven</groupId>
            <artifactId>server</artifactId>
        </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>
                        <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <Bundle-SymbolicName>$(replace;${project.artifactId};-;_)</Bundle-SymbolicName>
                        <Export-Package>
                            osgi.demo.client;version="${project.version}"
                        </Export-Package>
                        <Import-Package>
                            org.osgi.framework,oage.demo.server;version="${project.version}"
                        </Import-Package>
                        <Bundle-Activator>
                            osgi.demo.client.ClinetBundle
                        </Bundle-Activator>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

4、打包Bundle

使用命令mvn clean package,打包,看到如下输出,及打包成功。在模块下会生成一个target文件夹,该文件夹中会存在已打包好的jar包

OSGI和Maven结合

 

5、启动felix,并安装运行bundle

下载felix,并解压,目录结构如下(无plugins目录),新建plugins目录,将打好的两个包放到plugins目录下。

OSGI和Maven结合

 

在该层使用shell命令java -jar ./bin/felix.jar,将felix运行起来,看到如下情况,即运行成功。

OSGI和Maven结合

 

使用 install file:plugins/文件名 安装bundle

start启动bundle,要先启动server,后启动client,因为server为发布服务,如果后启动client找不到可用服务

 

OSGI和Maven结合