在Eciplse运行环境中,需要包含jSLP-OSGi这个Bundle。该Bundle提供 ch.ethz.iks.slp包。如果没有运行该Bundle,
那么就无法找到服务。
与P2P查找服务方式不同的是,在注册服务和查找服务。
注册服务的代码实例:
package radlab.r_osgi.slp.server;
import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; import java.util.Hashtable; //import org.osgi.framework.*;
import ch.ethz.iks.r_osgi.*; import radlab.r_osgi.slp.service.ServiceSLP;
import ch.ethz.iks.slp.*;
public class Activator implements BundleActivator {
/* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ private ServiceRegistration registration; public void start(BundleContext context) throws Exception { Hashtable<Object,Object> properties = new Hashtable<Object,Object>(); // this is the hint for R-OSGi that the service // ought to be made available for remote access properties.put(RemoteOSGiService.R_OSGi_REGISTRATION,Boolean.TRUE); registration=context.registerService(ServiceSLP.class.getName(),new ServiceImpl(), properties); ServiceReference advRef = context.getServiceReference(Advertiser.class.getName()); Advertiser advertiser=(Advertiser) context.getService(advRef); advertiser.register(new ServiceURL("service:osgi:r-osgi://localhost:9278", 20), null); System.out.println("I am server bundle, I provide a service which will print a message in your console."); }
/* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext context) throws Exception { registration.unregister(); }
}
获取服务的代码实例如下:
package radlab.r_osgi.slp.client;
import java.util.Vector; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleException; import org.osgi.framework.ServiceReference; import ch.ethz.iks.r_osgi.*; import radlab.r_osgi.slp.service.ServiceSLP;
import ch.ethz.iks.slp.*;
public class Activator implements BundleActivator { Vector<String> serviceLocation=new Vector<String>(); RemoteOSGiService remote; RemoteServiceReference[] srefs;
/* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ public void start(BundleContext context) throws Exception { System.out.println("I am client bundle"); final ServiceReference sref = context.getServiceReference(RemoteOSGiService.class.getName()); if (sref == null) { throw new BundleException("No R-OSGi found"); } ServiceReference locRef = context.getServiceReference(Locator.class.getName()); if (locRef != null) { System.out.println("Scanning Services..."); Locator locator = (Locator) context.getService(locRef); ServiceLocationEnumeration slenum = locator.findServices(new ServiceType("service:osgi"), null, null); System.out.println("Result:"); while (slenum.hasMoreElements()) { ServiceURL foundService = (ServiceURL) slenum.nextElement(); String url=foundService.toString(); System.out.println(url); String address=url.replaceFirst("service:osgi:", ""); serviceLocation.add(address); } } remote = (RemoteOSGiService) context.getService(sref); for(int i=0;i<serviceLocation.size();i++){ srefs = remote.getRemoteServiceReferences(new URI(serviceLocation.elementAt(i).toString()), ServiceSLP.class.getName(), null); if(srefs==null){ continue; }else{ break; } } if(srefs==null){ System.out.println("No services found.");System.exit(0); } System.out.println("Have a try..."); ServiceSLP serviceSLP = (ServiceSLP) remote.getRemoteService(srefs[0]); System.out.println(serviceSLP.getMessage()); }
/* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext context) throws Exception { }
}
转载本文请联系原作者获取授权,同时请注明本文来自陈俊清科学网博客。 链接地址: https://blog.sciencenet.cn/blog-458387-440317.html
上一篇:
R-OSGi 中的服务查找方式 下一篇:
博士毕业之后的路如何选呢?