`

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
分享到:
评论

相关推荐

    OSGI bundle change listener

    **OSGI Bundle Change Listener** OSGi(Open Service Gateway Initiative)是一种Java模块化系统,它允许在运行时动态地发现、加载、卸载和更新服务。OSGi的核心是其框架,它管理一组称为bundle的模块。每个bundle...

    OSGI服务 DS EVENT

    4. `Service Listener`: 监听服务注册表,当服务发生变化时,触发相应的回调方法。 5. `Service Event`: 包含了服务变化的信息,如服务注册、更改或注销。 总的来说,OSGI服务DS事件是OSGI框架中实现组件间动态交互...

    基于gemini的blueprint(原生是Spring DM)实现对bundle生命周期的监听

    标题中的“基于gemini的blueprint(原生是Spring DM)实现对bundle生命周期的监听”涉及到的是OSGi(Open Service Gateway Initiative)框架下的一种服务管理机制。OSGi是一种模块化系统,它允许Java应用程序被分解为...

    OSGi传说beta1.1.pdf

    public class EventListener { @EventHandler public void handleEvent(Event event) { // 事件处理逻辑 } } ``` #### 九、OSGi与Spring框架的整合 OSGi可以与Spring框架整合,利用Spring的强大功能进行依赖...

    Android之GPS定位详解.pdf

    GpsStatus.Listener l = new GpsStatus.Listener() { public void onGpsStatusChanged(int event) { switch (event) { case GpsStatus.GPS_EVENT_STARTED: Log.i(TAG, "GPS 启动"); break; case GpsStatus.GPS...

    Android自定义软键盘

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); } return super.onTouchEvent(event); } } ``...

    GPS经纬度和卫星个数实现代码

    private final GpsStatus.Listener statusListener = new GpsStatus.Listener() { @Override public void onGpsStatusChanged(int event) { LocationManager locationManager = (LocationManager) ...

    Android重力感应简介

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE); sensor = sensorMgr.getDefaultSensor(Sensor....

    Android编程之光线传感器用法详解

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); light = new TextView(this); setContentView(light); sensorManager = (SensorManager) getSystemService...

    轻松实现Android指南针功能

    manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); } @Override protected void onResume() { Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION); manager....

    Android源码解析

    - `onTouchEvent(MotionEvent event)`: 处理触摸事件。 - **Android动画基础** - Android支持多种类型的动画,如属性动画、帧动画等。 - **关键类**: - `ValueAnimator`: 实现属性动画的核心类。 - `...

    Android 利用方向传感器实现指南针具体步骤

    manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); } @Override protected void onResume() { Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION); manager....

    android2.3自动接听实现

    telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // 注册监听器 telManager.listen(new TelListener(), PhoneStateListener.LISTEN_CALL_STATE); } private class TelListener...

Global site tag (gtag.js) - Google Analytics