首页 > 代码库 > osgi实战学习之路:7. Service-2之ServiceListener

osgi实战学习之路:7. Service-2之ServiceListener

ServiceListener三种状态:


ServiceEvent.REGISTERED
ServiceEvent.MODIFIED
ServiceEvent.UNREGISTERING



基于ServiceListener实现服务查找的demo


Provider

student-manage/Activator.java

package com.demo.service;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import com.demo.service.impl.StudentManageA;
import com.demo.service.impl.StudentManageB;

public class Activator implements BundleActivator {

	public void start(BundleContext context) throws Exception {
		System.out.println("注册服务开始....");
		//注册B
		System.out.println("注册服务B....");
		Hashtable<String, String>  dict=new Hashtable<String, String>();
		dict.put("name", "b");
		context.registerService(IStudentManage.class.getName(),
				new StudentManageB(), dict);
		//注册A
		System.out.println("注册服务A....");
		dict=new Hashtable<String, String>();
		dict.put("name", "a");
		context.registerService(IStudentManage.class.getName(),
				new StudentManageA(), dict);
		
		System.out.println("注册服务结束....");
	}

	public void stop(BundleContext context) throws Exception {

	}

}


Consumer

student-action/Activator.java

package com.demo.action;

import java.util.SortedSet;
import java.util.TreeSet;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;

import com.demo.service.IStudentManage;


public class Activator implements BundleActivator{
	BundleContext context;
	UserManagerListener listener;
	public void start(BundleContext context) throws Exception {
		System.out.println("action listener begin...");
		this.context=context;
		listener=new UserManagerListener();
		System.out.println("服务调用------------------");
		synchronized (listener) {
		      String filter = "(" + Constants.OBJECTCLASS + "=" + IStudentManage.class.getName() + ")";
		      context.addServiceListener(listener, filter);
		      ServiceReference[] refs = context.getServiceReferences(null, filter);
		      if (refs != null) {
		        for (ServiceReference r : refs) {
		        	listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, r));
		        }
		      }
		 }
		//查找Service
		IStudentManage studentManage=listener.getStudentManageService();
		//调用服务
		studentManage.add();
		System.out.println("action listener end...");
	}

	public void stop(BundleContext context) throws Exception {
	}
	//监听类
	class UserManagerListener implements ServiceListener {
		SortedSet<ServiceReference> studentManageRefs = new TreeSet<ServiceReference>();
		public synchronized void serviceChanged(ServiceEvent event) {
			switch (event.getType()) {
	        case ServiceEvent.REGISTERED:
	        	studentManageRefs.add(event.getServiceReference());
	          break;
	        case ServiceEvent.MODIFIED:
	          break;
	        case ServiceEvent.UNREGISTERING:
	        	studentManageRefs.remove(event.getServiceReference());
	          break;
	        default:
	          break;
	      }
		}
		 /**查找服务
		 * @return IStudentManage
		 */
		public synchronized IStudentManage getStudentManageService() {
		      if (studentManageRefs.size() > 0) {
		    	  System.out.println("size:"+studentManageRefs.size());
		        return (IStudentManage) context.getService(studentManageRefs.last());
		      }
		      return null;
		    }
	}
}


结果: