Create an OSGi project-helloword level with Maven

Source: Internet
Author: User

What is OSGi? OSGi is a Java modular specification, translated by the Open Service Gateway Protocol (open Services gateways Initiative) OSGi is not an application-level framework, but a specification at the software design level

What does it bring to us?

1 reusable component accumulation and reuse, functional decoupling. (multiplexing, cohesion, and coupling can all be well represented in OSGi)

2 runs in a specific OSGi container and starts fast.

3 OSGI also allows you to dynamically increase or decrease the service, or dynamically load unload classes and other resources (hot swap effect).

OSGi Framework Selection We use Felix to create a demo

Understand several concepts before creating

Bundle: is actually a jar file with a description. OSGi a component is a bundle

Take a look at the declaration period for bundles:

Bundles need to be mounted on the OSGi framework, which simply allows the framework to use a ClassLoader to load classes and resources in bundles.

Next is the resolve, which checks if the bundle needs to be "referenced" for the package to be available, which is described in detail later.

Then, the bundle will start, that is, running the Start method in Activator, the method runs, that is, into the "active" state, when the bundle is officially available.

Build a Helloword of OSGi

First create a MAVEN project

Add Maven Bundle Plugin to the Maven Pom file

<plugins><plugin> <groupid>org.apache.felix</groupId> <artifactId> Maven-bundle-plugin</artifactId> <extensions>true</extensions> < configuration> <instructions> <export-package>org.foo.myproject.api</ export-package> <private-package>org.foo.myproject.*</Private-Package> < Bundle-activator>org.foo.myproject.impl1.activator</Bundle-Activator> </instructions> </configuration> </plugin></plugins>        
This plugin can be found on the Felix website. This plugin is required to create bundles that depend on him

There is also the need to add a Osgi--core package dependency
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
<scope>provided</scope>
</dependency>



Collated Pom File
<?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>cn.bean.demo</groupId>
<artifactId>osgi</artifactId>
<version>0.0.1-SNAPSHOT</version>
&LT;NAME&GT;CN:: Bean:: Sun:: osgi_demo</name>
<packaging>bundle</packaging>

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

</properties>
<dependencies>

<!--OSGi--
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Description>${project.description}</Bundle-Description>
<Import-Package>org.osgi.framework</Import-Package>
<Export-Package></Export-Package>
<Embed-Transitive>true</Embed-Transitive>
<Embed-Directory>dependency</Embed-Directory>
<Bundle-Activator>cn.gzydt.sun.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>

It is now also necessary to implement a Java class that writes a bundleactivator interface that can be implemented when a bundle is started or stopped.
Import Org.osgi.framework.BundleActivator;
Import Org.osgi.framework.BundleContext;

public class Activator implements Bundleactivator {

public void Start (Bundlecontext context) throws Exception {
System.out.println ("Cn.bean.demo components--start .....");
}

public void Stop (Bundlecontext context) throws Exception {
System.out.println ("Cn.bean.demo components--stop ....");
}

}
When the OSGi container loads this component, it triggers the Start method, which stops when the component is unloaded or updated, and start (provided that the activation is added to the Maven-bundle plugin of the pom file).
<Bundle-Activator>cn.gzydt.sun.Activator</Bundle-Activator>
)

Then use the MAVEN command MVN compile: Compile source code MVN package Generate a bundle file (the package type in the Pom file is bundle)
And then throw the bundle in the OSGi Runtime environment container I use the apache-servicemix-6.0.3 official website version of the latest edition has Apache ServiceMix 7.0.0.m1 I tried to feel not very useful,
Start container

Create an OSGi project-helloword level with 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.