天天看點

OSGi系列 - 第一個OSGi Bundle

安裝好了Apache Felix,下面要做的是開發我的第一個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      

執行完成後會出現下面的目錄結構:

OSGi系列 - 第一個OSGi Bundle

切換到目錄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容器非常的重要。

OSGi系列 - 第一個OSGi Bundle

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

OSGi系列 - 第一個OSGi Bundle

為了測試我們的bundle,可以對它進行停止和啟動,相應的會輸出對應的資訊提示。注意:這裡啟動和停止指令後面的5,是特定于目前Felix執行個體的example bundle序号,需要根據情況相應進行改變。

OSGi系列 - 第一個OSGi Bundle