Osgi series-the first osgi bundle

Source: Internet
Author: User

After installing Apache Felix, the following is how to develop my first bundle. The entire process is a referenceApache Felix Tutorial example 1-service event listener bundle.

 

Open a DOS window and switch to the directory where the bundle is to be stored.D: \ develop \ eclipse-jee-indigo-SR2-win32 \ workspace \ Felix-AnalysisFor example. Create an empty project using Maven.

 
MVN archetype: Create-dgroupid = Felix. Tutorial-dartifactid = example1-dpackagename = Felix. Tutorial. example1-dversion = 1.0.0

 

After the execution is complete, the following directory structure will appear:

 

Switch to directorySRC \ main \ Java \ Felix \ tutorial \ example1,Delete the app. Java File Created by Maven and create a new fileActivator. JavaThe file content is as follows:

 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 * processing 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 ." );}}} 

 

 

Open the Pom. xml file with the text editing software and modify it to the following content (note that packaging is changed to bundle here ):

 <  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 > 

 

Package bundle and execute it in DOS windowMVN package. After the execution is complete, the target directory containsExample1-1.0.0.jarFile, which is the bundle file to be released. Open with WinRARExample1-1.0.0.jar, FindMETA-INFI/manifest. MF, View its content, you will findMaven-bundle-pluginMany entries are generated, which are very important to osgi containers.

 

Start Felix and useStart file:/D:/develop/eclipse-jee-indigo-SR2-win32/workspace/Felix-analysis/example1/target/example1-1.0.0.jarCommand to start bundle.

 

To test the bundle, you can stop and start it, and the corresponding information is displayed.Note: The 5 next to the Start and Stop commands is the example bundle number specific to the current Felix instance, which needs to be changed as appropriate.

 

In this step, our first bundle is complete.

 

Use Maven to develop bundleArticle:

Bundle plugin for Maven

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.