您的位置:首页 > 其它

OSGI服务注册、引用、以及跟踪

2015-12-29 17:52 246 查看
下载地址<a target=_blank href="http://download.csdn.net/detail/qq_28051649/9382494">http://download.csdn.net/detail/qq_28051649/9382494</a>
1、新建一个bundle名为BundleBase
package example.service;

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

public class Activator implements BundleActivator {

private static BundleContext context;

static BundleContext getContext() {
return context;
}

/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
}

/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}

}

package example.service;

public interface IHelloService {

String sayHello(String somebody);
}
2、新建一个名为BundleEnservice
<p>package bundleenservice;</p><p>import java.util.Dictionary;
import java.util.Hashtable;</p><p>import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;</p><p>import example.service.IHelloService;</p><p>public class Activator implements BundleActivator {</p><p> private static BundleContext context;</p><p> static BundleContext getContext() {
  return context;
 }</p><p> /*
  * (non-Javadoc)
  * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
  */
 public void start(BundleContext bundleContext) throws Exception {
  Activator.context = bundleContext;
  IHelloService hs = new EnHelloworld();
  Dictionary<String, Object> properties = new Hashtable<String, Object>();
  properties.put("service.language", "CN");
  context.registerService(IHelloService.class.getName(), hs, properties);
 }</p><p> 
 /*
  * (non-Javadoc)
  * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
  */
 public void stop(BundleContext bundleContext) throws Exception {
  Activator.context = null;
 }</p><p>}</p><p>package bundleenservice;</p><p>import example.service.IHelloService;</p><p>public class EnHelloworld implements IHelloService{</p><p> @Override
 public String sayHello(String somebody) {
  // TODO Auto-generated method stub
  return "how are you ?" + somebody;
 }</p><p>}
</p><p> </p><p> </p><p>
 </p>

 

3、新建一个名为BundleCnService

 

package bundlecnservice;

import java.util.Dictionary;
import java.util.Hashtable;

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

import example.service.IHelloService;

public class Activator implements BundleActivator {

private static BundleContext context;

static BundleContext getContext() {
return context;
}

/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
//通过服务工厂进行服务的获取
CnGreetingsServiceFactory cns = new CnGreetingsServiceFactory();

//		IHelloService hs = new CnHelloworld();
Dictionary<String, Object> properties = new Hashtable<String, Object>();
//		properties.put(Constants.SERVICE_RANKING,1 );
properties.put("service.language", "CN");
//服务的注册
context.registerService(IHelloService.class.getName(), cns, properties);
}

/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}

}
<p>package bundlecnservice;</p><p>import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;</p><p>public class CnGreetingsServiceFactory implements ServiceFactory{</p><p> @Override
 public CnHelloworld getService(Bundle arg0, ServiceRegistration arg1) {
  // TODO Auto-generated method stub
  System.out.println(arg0.getSymbolicName() + "获取了服务");
  return new CnHelloworld();
 }</p><p> @Override
 public void ungetService(Bundle arg0, ServiceRegistration arg1, Object arg2) {
  // TODO Auto-generated method stub
  System.out.println(arg0.getSymbolicName() + "释放了服务");
 }</p><p>}
</p>

package bundlecnservice;

import example.service.IHelloService;

public class CnHelloworld implements IHelloService{

 @Override
 public String sayHello(String somebody) {
  // TODO Auto-generated method stub
  return "你好," + somebody;
 }

}

4、新建一个bundleserviceUser

package bundleserviceuser;

import java.util.Collection;
import java.util.Iterator;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import example.service.IHelloService;
public class Activator implements BundleActivator {
 private static BundleContext context;
 static BundleContext getContext() {
  return context;
 } /*
  * (non-Javadoc)
  * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
  */
 public void start(BundleContext bundleContext) throws Exception {
  Activator.context = bundleContext;
  
  ServiceReference reference = context.getServiceReference(IHelloService.class.getName());
  String filter = "(service.language=CN)";
  ServiceReference[] references = context.getServiceReferences(IHelloService.class.getName(), filter);
  int iter = references.length;
  for(int i = 0; i  < iter; i++){
   IHelloService service  = (IHelloService) context.getService(references[i]);
   System.out.println(service.sayHello("zhangfy"));
  }   
  
 } /*
  * (non-Javadoc)
  * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
  */
 public void stop(BundleContext bundleContext) throws Exception {
  Activator.context = null;
 }}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: