`

Service Event Listener Bundle

    博客分类:
  • OSGI
阅读更多

This example creates a simple bundle that listens for OSGi service events. This example does not do much at first, because it only prints out the details of registering and unregistering services. In the next example we will create a bundle that implements a service, which will cause this bundle to actually do something. For now, we will just use this example to help us understand the basics of creating a bundle.

A bundle gains access to the OSGi framework using a unique instance of BundleContext. In order for a bundle to get its unique bundle context, it must implement the BundleActivator interface; this interface has two methods, start() and stop(), that both receive the bundle's context and are called when the bundle is started and stopped, respectively. In the following source code, our bundle implements the BundleActivator interface and uses the context to add itself as a listener for service events (the file for our bundle is called Activator.java):

/*
 * Apache Felix OSGi tutorial.
**/

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

After implementing the Java source code for the bundle, we must also define a manifest file that contains meta-data needed by the OSGi framework for manipulating the bundle. The manifest is packaged into a JAR file along with the Java class file associated with our bundle; the whole JAR package is actually referred to as a bundle. We create a file called manifest.mf that contains the following:

Bundle-Name: Service listener example
Bundle-Description: A bundle that displays messages at startup and when service events occur
Bundle-Vendor: Apache Felix
Bundle-Version: 1.0.0
Bundle-Activator: tutorial.example1.Activator
Import-Package: org.osgi.framework
分享到:
评论

相关推荐

    EventListener,eventlistener是一个小型库,用于类之间的通信,如广播信号.zip

    EventListener是一个小型库,用于类之间的通信,如广播信号

    event_listener.7z

    event_listener.jsx PS事件监听脚本

    IE8-EventListener:IE8的Polyfill

    IE8-EventListener IE8的Polyfill 我怎么才能得到它? IE8-EventListener可通过npm获得,并可以使用以下命令进行安装: npm install ie8-eventlistener 它是什么? IE8-EventListener努力向IE8添加完全的...

    event_listener.jsx

    event_listener.jsx PS事件监听脚本 1、修改脚本监听数据本地化语言显示 ---0.02-01 2、函数增加注释当前历史记录名称 ---0.02-02 https://blog.csdn.net/greless/article/details/105811358

    js HTML DOM EventListener功能与用法实例分析

    DOM EventListener 用于向指定元素添加事件句柄。 在用户点击按钮时触发监听事件: document.getElementById('myBtn').addEventListener('click', displayDate); addEventListener() 方法添加的事件句柄不会覆盖...

    supervisor-event-listener:Supervisor事件通知, 支持邮件, Slack, WebHook

    supervisor-event-listener Supervisor事件通知, 支持邮件, Slack, WebHook 简介 Supervisor是*nix环境下的进程管理工具, 可以把前台进程转换为守护进程, 当进程异常退出时自动重启. supervisor-event-listener监听...

    Spring注解驱动开发第40讲——你晓得@EventListener这个注解的原理吗?

    Spring注解驱动开发第40讲——你晓得@EventListener这个注解的原理吗?

    C++ EventListener (C# style)-开源

    这是在 c++ 中实现的 EventListener(就像在 c# 中一样)。我正在使用一些 c++11 特性(比如 final),但是如果你愿意,你可以很容易地删除它们。 检查维基页面以查看使用情况。 需要做的:线程安全; 安全处置调用...

    SpringBoot整合Listener的两种方式.docx

    SpringBoot整合Listener的两种方式: 1.通过注解扫描完成Listener组件的注册 1.创建一个类实现 ServletContextListener (具体实现哪个Listener根据情况来判断) 2.在类上加入注解 @WebListener 3.重写 context...

    Passive Event Listeners - 被动事件监听器1

    Passive Event Listeners - 被动事件监听器在写webapp页面的时候,Chrome 提醒  [Violat

    PhotoShop(PS)脚本监听器Script event listener.jsx

    新手第一次了解的事件监听一定是 ScriptingListener.8bi插件,如果你对此插件有了基本运用,那么现在了解一款更方便的时间监听脚本 event_listener.jsx。不再像ScriptingListener.8bi插件记录的脚本那样晦涩难懂,...

    大图片所引起的内存问题

    在我们android开发中,一个应用使用的内存大小是有限制的.在应用中,如果大量的使用bitmap就很可能导致内存溢出的问题。比如我在曾经的一个项目中遇到的问题:要使用Gallery来显示多张不同的图片,在给Gallery的每个...

    event-listener:通知异步任务或线程

    事件监听器通知异步任务或线程。...use event_listener :: Event;let flag = Arc :: new (AtomicBool :: new ( false ));let event = Arc :: new (Event :: new ());// Spawn a thread that will se

    evt-listener:EventListener 作为对象

    off (其中listener是 EventListener 的一个实例)而不必担心保存和传递事件名称和函数。 请参阅下面的比较。 先决条件 使用 npm 安装它,将其保存为依赖项。 npm i evt-listener --save 两个示例共享相同的事件...

    Android代码-KDispatcher

    KDispatcher is a Kotlin EventDispatcher This is light-weight event dispatcher based on KOTLIN priority: Int?...eventListener - function listener for event priority - the priority to sort ca

    JavaScript HTML DOM EventListener

    JavaScript HTML DOM EventListener addEventListener() 方法 实例 在用户点击按钮时触发监听事件: document.getElementById(myBtn).addEventListener(click, displayDate); addEventListener() 方法用于向...

    kktix-event-listener

    $ npm install kktix-event-listener 用法 var kktixEventListener = require ( 'kktix-event-listener' ) ; kktixEventListener ( 'http://reactjs.kktix.cc/events/react9' ) ; //=> 命令行界面 $ npm install -g ...

    Event Listener-开源

    EventListener是一个简单高效的C ++框架,用于使用侦听器模式在图形界面或数字代码中传播事件。 可以过滤和阻止事件。 该软件包不使用任何其他库。

Global site tag (gtag.js) - Google Analytics