OSGi + Felix Example1 Writing

Source: Internet
Author: User

In our previous blog post, we built the entire operating environment of OSGI and integrated the other individual components into the subsequent development, and this podcast series will write all of example in Apache Felix's official website and then write the following OSGi articles, such as OSGi command , Blueprint,configadmin and so on, these aside for the time being, later on to write blog post.

Example Module

In the last Maven project, the new folder was named Application, and the application modules that were written later are placed in this folder, the new Maven module is named example, the folder is application, and after the new creation is completed, The parent module in the Pom.xml file is not selected as the root module.

Program writing

First determine the dependencies in the Pom file, and write the following into the Pom file:

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

Above, the Maven plugin, the Felix plugin is what we must use, the Karaf plugin is
Provide us with a running container. Other dependent issues are temporarily or not available, and will be explained later.
Next src folder under the new class, named Activator, the Apache example1 in the source code into the following:

ImportOrg.osgi.framework.BundleActivator;ImportOrg.osgi.framework.BundleContext;ImportOrg.osgi.framework.ServiceEvent;ImportOrg.osgi.framework.ServiceListener;/** * Created by Administrator on 2016/6/18. * * Public  class Activator implements bundleactivator, servicelistener {     Public void Start(Bundlecontext Bundlecontext)throwsException {System.out.println ("Starting to listen for service events."); Bundlecontext.addservicelistener ( This); } Public void Stop(Bundlecontext Bundlecontext)throwsException {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."); }    }}

Then change the pom.xml in the Felix plugin in the class, the startup class is rewritten to the currently written example class the package name and the class name.

Example feature

After the completion of the above, you need to configure feature,feature in the previous blog has been mentioned, this article is no longer described in the Feature.xml, add the current example feature, for the following:

<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="+">Mvn:${project.groupid}/example/${project.version}</Bundle>    </feature>

The other feature of these configurations, which are used for the rest of the configuration, are configured CXF for restful services, and Jackson is used for serialization, starting with the configuration of Camel-core and the bottom engineering bundle.

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

This one is used to package the current project as a bundle, and each feature will add this line when the Submodule is added later.

Run

After MVN compiles, install, run the Karaf plugin in IntelliJ, as follows:

Running Karaf-run, you get the following results:

Above, Activator successfully starts and runs correctly, that is, the example demonstrates success.

Summarize

This article on the Felix official website Example1 has been written, and successfully demonstrated, but did not explain its specific principles, example loading and unloading, there are KARAF output logs, the installation process for each bundle, these will be in the next blog post to do a specific explanation.

OSGi + Felix Example1 Writing

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.