|||
在客户端:
只需发现服务和使用服务
package radlab.r_osgi.sample.client;
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.RemoteOSGiService;
import ch.ethz.iks.r_osgi.RemoteServiceReference;
import ch.ethz.iks.r_osgi.URI;
import radlab.r_osgi.sample.api.ServiceInterface;
public class Activator implements BundleActivator {
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
private ServiceReference sref;
private static RemoteOSGiService remote;
private static ServiceInterface service;
private static final URI uri = new URI(System.getProperty(
"ch.ethz.iks.r_osgi.service.uri", "r-osgi://192.168.0.228:9278")); // IP地址根据服务所在的机器作相应调整
public void start(BundleContext context) throws Exception {
System.out.println("starting sample client");
sref = context.getServiceReference(RemoteOSGiService.class.getName());
if (sref != null) {
remote = (RemoteOSGiService) context.getService(sref);
} else {
throw new BundleException("OSGi remote service is not present.");
}
final RemoteServiceReference[] refs = remote
.getRemoteServiceReferences(uri, ServiceInterface.class.getName(), null);
if (refs==null) {
throw new BundleException("The service is not present.");
}
System.out.println("I am client bundlen");
System.out.println("Wait...n");
service = (ServiceInterface) remote.getRemoteService(refs[0]);
System.out.println(service.getMessage());
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
}
}
MENIFEST.MF文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Client
Bundle-SymbolicName: radlab.r_osgi.sample.client
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: radlab.r_osgi.sample.client.Activator
Bundle-Vendor: jqqq
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: ch.ethz.iks.r_osgi,
org.osgi.framework;version="1.3.0",
radlab.r_osgi.sample.api
在测试环境下:
先运行服务端bundle,然后再运行客户端bundle。客户端bundle会得到这样的结果:
osgi> starting sample client
I am client bundle
Wait...
This is service message!
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-27 10:09
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社