OSGI serviceChanged method

I'm writing a system that lets users plugin their own code to monitor sensors.

I want to write a basic listener that listens for changes in other bundles. I came across ServiceListener which seems handy for my cause. My question is: when does serviceChanged() from ServiceListener get called? I've been looking for it on the internet but can't find anything.

Many thanks!

package be.pxl.smartcampus.MonitorListener;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceEvent;
import be.pxl.smartcampus.util.Monitor;
import be.pxl.smartcampus.util.Data;

public class Activator implements BundleActivator, ServiceListener {

    public void start(BundleContext context) {
        context.addServiceListener(this);
    }

    public void stop(BundleContext context) {
        context.removeServiceListener(this);
    }

    public void serviceChanged(ServiceEvent event) {
        if (event.getType() == ServiceEvent.MODIFIED) {
            Monitor monitor = (Monitor) (event.getServiceReference());
            Data data = monitor.getData();
            System.out.println("SERVICE CHANGED CALLED");
            // TO DO: MAKE API CALL
        }
    }
}

import be.pxl.smartcampus.util.Data;
import be.pxl.smartcampus.util.Monitor;
import org.osgi.framework.BundleContext;

import java.util.Hashtable;

public class Activator extends Monitor {

    private Data data = new Data();
    private Hashtable<String, Data> hash = new Hashtable<String, Data>();

    public void start(BundleContext bundleContext) {
        hash.put("first", data);
        bundleContext.registerService(Activator.class.getName(), this, hash);
        changeData();
    }

    private void changeData() {
        hash.put("second", data);
    }

    public void stop(BundleContext bundleContext) {

    }
}

Upvotes: 0

Views: 381

Answers (2)

Neil Bartlett
Neil Bartlett

Reputation: 23948

It's not enough just to change the contents of some HashTable. You have to call setProperties on the ServiceRegistration object.

Upvotes: 1

Milen Dyankov
Milen Dyankov

Reputation: 3052

You are mixing 2 concepts here - bundles and services.

Bundles are Jar files that have lifecycle (can be dynamically installed, resolved, started, stopped and uninstalled). If you want to track bundle lifecycle you need to create a BundleListener which will be called when bundles lifecycle events occur.

Alternatively if you only need to do something with particular bundles that contain some special information (special file, special entry in MANIFEST.MF, ...) you can use the Extender Pattern

Services are components (classes) that live inside a bundle that are registered with OSGi service registry so other services can find them and use them. Services have own lifecycle (can be started, stopped, ...) If you want to track a service lifecycle event you need to implement and register ServiceListener It will be called when a service lifecycle event occurs (service is started, stopped, ...). The ServiceEvent contains information about the actual service and type of change that caused the event.

Alternatively, if you only care to do something with particular types of services, you can use Whiteboard Pattern.

Upvotes: 0

Related Questions