码迷,mamicode.com
首页 > 其他好文 > 详细

osgi + felix example1编写

时间:2016-06-22 09:25:07      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

在上篇博文中,我们搭建了osgi的整个运行环境,并将其他个别组件也整合了进来用于后续的开发,本播客系列将会将apache felix官网中example全部编写一遍,然后进行osgi后续的文章编写,如osgi command,blueprint,configAdmin等等,这些暂且放置一边,日后写博文的时候再谈。


example模块

在上回建立的maven工程中,新建文件夹命名为Application,日后编写的应用模块都将放在这个文件夹之中,新建maven module 命名为example,选择文件夹为Application,新建完成之后,改pom.xml文件中parent为Parent模块,不选择为root模块。


程序编写

首先确定pom文件中的依赖,将以下内容写入pom文件之中:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-blueprint</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.4.3</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-mybatis</artifactId>
            <version>${camel.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Testing & Camel Plugin -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-test-blueprint</artifactId>
        </dependency>
    </dependencies>

    <build>
        <defaultGoal>install</defaultGoal>

        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <configuration>
                    <instructions>
                        <Private-Package>cn.com.command.*</Private-Package>
                        <Import-Package>*</Import-Package>
                        <Bundle-Activator>cn.com.example5.Activator</Bundle-Activator>
                    </instructions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.karaf.tooling</groupId>
                <artifactId>karaf-maven-plugin</artifactId>
                <configuration>
                    <bootFeatures>
                        <bootFeature>${project.artifactId}</bootFeature>
                    </bootFeatures>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

以上,maven插件中,felix插件是我们所必须使用的,karaf插件是
提供我们运行容器的。其他一些依赖的问题暂时或用不上,后续将讲解这些。
接下来src文件夹下新建Class,命名为Activator,将Apache example1中源码编写进去,为以下:

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;

/**
 * Created by Administrator on 2016/6/18.
 */
public class Activator implements BundleActivator, ServiceListener {

    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("Starting to listen for service events.");
        bundleContext.addServiceListener(this);
    }

    public void stop(BundleContext bundleContext) throws Exception {
        bundleContext.removeServiceListener(this);
        System.out.println("Stopped listening for service events.");
    }

    public void serviceChanged(ServiceEvent serviceEvent) {
        String[] objectClass = (String[])serviceEvent.getServiceReference().getProperty("objectClass");
        if (serviceEvent.getType() == ServiceEvent.REGISTERED) {
            System.out.println("Ex1: Service of type " + objectClass[0] + " registered.");
        } else if (serviceEvent.getType() == ServiceEvent.UNREGISTERING) {
            System.out.println("Ex1: Service of type " + objectClass[0] + " unregistered.");
        } else if (serviceEvent.getType() == ServiceEvent.MODIFIED) {
            System.out.println("Ex1: Service of type " + objectClass[0] + " modified.");
        }
    }
}

然后改变pom.xml中felix插件中中类,启动类改写为当前编写的example类的所在包名与类名。


example feature

在上述完成之后,需要另外在配置feature,feature在上篇博客中已经提及,本文中不再讲述,在feature.xml中添加当前example feature,为以下:

<feature name="example" version="${project.version}">
        <feature>http</feature>
        <feature>cxf</feature>

        <feature>camel-core</feature>
        <feature>camel-blueprint</feature>
        <feature>camel-jackson</feature>
        <feature>camel-cxf</feature>

        <bundle>mvn:com.fasterxml.jackson.jaxrs/jackson-jaxrs-base/2.4.3</bundle>
        <bundle>mvn:com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider/2.4.3</bundle>

        <bundle start-level="100">mvn:${project.groupId}/example/${project.version}</bundle>
    </feature>

这里面这些配置的其他feature,都是应用其他做准备的,配置cxf是用来实现restful服务的,jackson是用来序列化使用的,最开始只需配置camel-core和最下的工程bundle即可。

<bundle start-level="100">mvn:${project.groupId}/example/${project.version}</bundle>

这一个是用来将当前工程同样打包为一个bundle使用的,在后续添加子模块时,每一个feature都会添加这行内容。


运行

在mvn 编译, install 之后,intellij中运行karaf插件,如以下:
技术分享

运行karaf-run,会得到以下结果:
技术分享

以上,Activator成功启动并正常运行,即该示例演示成功。


总结

本文对felix官网中example1进行了编写,并成功演示,但并没有讲解其种具体的原理,example的装卸载,还有karaf输出日志中,对各个bundle的install过程,这些将在下一篇博文中做具体的讲解。

osgi + felix example1编写

标签:

原文地址:http://blog.csdn.net/u012734441/article/details/51731557

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!