|||
R-OSGi工程的网址是:http://r-osgi.sourceforge.net/
在本文中,开发R-OSGi的环境是在Eclipse3.4版本上;本实验的目的是体会一下:OSGi与R-OSGi之间有何区别?是否是透明的,是否无缝衔接?因此,不关注复杂的应用逻辑,只是设计一个简单的服务(service)和客户端(client)之间的通讯访问。
在Eclipse开发环境下,开发两个bundles,一个是radlab.r_osgi.sample.service,其功能包括定义服务接口,服务实现以及注册服务,当然根据OSGi规范,将服务接口发布出来;另一个是radlab.r_osgi.sample.client,其功能是发现服务并使用该服务,根据OSGi规范,需要引入服务接口。上述的bundles都必须依赖于r_osgi bundle,本实验采用版本是remote-1.0.0.RC4.jar;在测试和运行环境,还需要其他的plug in。
定义服务接口:
package radlab.r_osgi.sample.api;
public interface ServiceInterface {
public String getMessage(); // very simple, just return a message
}
定义服务实现:
package radlab.r_osgi.sample.service;
import radlab.r_osgi.sample.api.ServiceInterface;;
public class ServiceImpl implements ServiceInterface{
public String getMessage(){
String message="This is service message!";
return message;
}
}
注册服务:
package radlab.r_osgi.sample.service;
import java.util.Hashtable;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import ch.ethz.iks.r_osgi.RemoteOSGiService;
import radlab.r_osgi.sample.api.ServiceInterface;
import radlab.r_osgi.sample.service.ServiceImpl;
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 {
final Hashtable properties = new Hashtable();
properties.put(RemoteOSGiService.R_OSGi_REGISTRATION, Boolean.TRUE);
registration = context.registerService(
ServiceInterface.class.getName(), new ServiceImpl(), properties);
System.out.println("The service has been registed!");
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
registration.unregister();
}
}
MANIFEST.MF文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Service
Bundle-SymbolicName: radlab.r_osgi.sample.service
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: radlab.r_osgi.sample.service.Activator
Bundle-Vendor: jqqq
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: ch.ethz.iks.r_osgi,
org.osgi.framework;version="1.3.0"
Export-Package: radlab.r_osgi.sample.api
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-10-20 04:16
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社