• OSGi系列 第一个OSGi Bundle


    安装好了Apache Felix,下面要做的是开发我的第一个Bundle。整个过程是参考Apache Felix Tutorial Example 1 - Service Event Listener Bundle实现的。

    打开一个DOS窗口,切换到要存放Bundle的目录,这里以D:\develop\eclipse-jee-indigo-SR2-win32\workspace\felix-analysis为例。然后使用Maven创建一个空的项目。

    mvn archetype:create -DgroupId=felix.tutorial -DartifactId=example1 -DpackageName=felix.tutorial.example1 -Dversion=1.0.0

    执行完成后会出现下面的目录结构:

    切换到目录src\main\java\felix\tutorial\example1,删除Maven创建的App.java文件,新建文件Activator.java,文件内容如下:

    package felix.tutorial.example1;
    
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.ServiceListener;
    import org.osgi.framework.ServiceEvent;
    
    /**
     * This class implements a simple bundle that utilizes the OSGi
     * framework's event mechanism to listen for service events. Upon
     * receiving a service event, it prints out the event's details.
    **/
    public class Activator implements BundleActivator, ServiceListener
    {
        /**
         * Implements BundleActivator.start(). Prints
         * a message and adds itself to the bundle context as a service
         * listener.
         * @param context the framework context for the bundle.
        **/
        public void start(BundleContext context)
        {
            System.out.println("Starting to listen for service events.");
            context.addServiceListener(this);
        }
    
        /**
         * Implements BundleActivator.stop(). Prints
         * a message and removes itself from the bundle context as a
         * service listener.
         * @param context the framework context for the bundle.
        **/
        public void stop(BundleContext context)
        {
            context.removeServiceListener(this);
            System.out.println("Stopped listening for service events.");
    
            // Note: It is not required that we remove the listener here,
            // since the framework will do it automatically anyway.
        }
    
        /**
         * Implements ServiceListener.serviceChanged().
         * Prints the details of any service event from the framework.
         * @param event the fired service event.
        **/
        public void serviceChanged(ServiceEvent event)
        {
            String[] objectClass = (String[])
                event.getServiceReference().getProperty("objectClass");
    
            if (event.getType() == ServiceEvent.REGISTERED)
            {
                System.out.println(
                    "Ex1: Service of type " + objectClass[0] + " registered.");
            }
            else if (event.getType() == ServiceEvent.UNREGISTERING)
            {
                System.out.println(
                    "Ex1: Service of type " + objectClass[0] + " unregistered.");
            }
            else if (event.getType() == ServiceEvent.MODIFIED)
            {
                System.out.println(
                    "Ex1: Service of type " + objectClass[0] + " modified.");
            }
        }
    }

    用文字编辑软件打开pom.xml文件,修改成如下内容(注意这里的packaging变成了bundle):

    <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>felix.tutorial</groupId>
      <artifactId>example1</artifactId>
      <version>1.0.0</version>
      <packaging>bundle</packaging>
    
      <name>Service listener example</name>
      <description>A bundle that displays messages at startup and when service events occur</description>
      <organization>
        <name>Apache Felix</name>
      </organization>
      
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
              <instructions>
                <Bundle-Activator>felix.tutorial.example1.Activator</Bundle-Activator>
                <Import-Package>org.osgi.framework</Import-Package>
              </instructions>
            </configuration>
          </plugin>
        </plugins>
      </build>
    
      <dependencies>
        <dependency>
          <groupId>org.apache.felix</groupId>
          <artifactId>org.osgi.core</artifactId>
          <version>1.0.0</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>

    打包bundle,在DOS窗口下执行mvn package。执行完成后target目录下有一个example1-1.0.0.jar文件,这就是最终需要发布的bundle文件。使用WinRar打开example1-1.0.0.jar,找到META-INFI/MANIFEST.MF,查看其内容,会发现maven-bundle-plugin产生了很多的条目,这些内容对OSGi容器非常的重要。

    启动Felix,使用start file:/D:/develop/eclipse-jee-indigo-SR2-win32/workspace/felix-analysis/example1/target/example1-1.0.0.jar命令启动bundle。

    为了测试我们的bundle,可以对它进行停止和启动,相应的会输出对应的信息提示。注意:这里启动和停止命令后面的5,是特定于当前Felix实例的example bundle序号,需要根据情况相应进行改变。

    到这一步,我们的第一个bundle也就大功告成了。

    使用Maven开发Bundle的文章:

    Bundle Plugin for Maven

  • 相关阅读:
    Lync二次开发
    Socket 一对多通信
    DBImport V3.1 数据互导工具及文档生成器更新发布
    Sql学习第六天——SQL 巩固练习(用到了前几天几个知识点)
    文件监控
    Google Protocol Buffers 入门
    [原]常用Linux命令总结[Thx for commandlinefu]
    清理SQL Server日志释放文件空间
    ASP.NET MVC动态二级域名及ASP.NET管道机制
    搜索引擎——JobSearch简介
  • 原文地址:https://www.cnblogs.com/eastson/p/2499357.html
Copyright © 2020-2023  润新知