7.31 OSGI入门

参考: http://gaotao3037.blog.163.com/blog/static/1401386552011074059786/

看了上面这个博文并按照例子才算是对osgi有了一个了解。

1.第一个helloworld没什么可说的注意下这个文件的配置就好了。

Manifest-Version: 1.0       
Bundle-ManifestVersion: 2  数值为2意味着本bundle支持OSGI规范第四版;如果是1那就是支持OSGI规范第三版。
Bundle-Name: HelloWorld Plug-in  给bundle定义一个短名,方便人员阅读
Bundle-SymbolicName: com.howard.sample.HelloWorld   给bundle定义一个唯一的非局部名。方便分辨。
Bundle-Version: 1.0.0  
Bundle-Activator: com.howard.sample.helloworld.Activator  声明在start和stop事件发生时会被通知的监听类的名字。
Bundle-Vendor: HOWARD  
Bundle-RequiredExecutionEnvironment: JavaSE-1.6  
Import-Package: org.osgi.framework;version="1.3.0"   定义bundle的导入包。
Export-Package: com.howard.sample.service     表示osgi暴露了这个包内的classes  可以被外部引用  但是并未暴露他的实现

2.osgi服务

实现bundles暴露服务接口给其他bundles而不是细节暴露,消费bundles甚至不知道提供服务的bundles

package com.huy.helloservice;

public interface HelloService {
	public String sayHello();
}
----------------------------------------
package com.huy.helloservice;

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

public class HelloServiceActivator implements BundleActivator {
	private ServiceRegistration helloServiceRegistration;
	@Override
	public void start(BundleContext context) throws Exception {
		// TODO Auto-generated method stub
		HelloService helloService = new HelloServiceImpl();
		helloServiceRegistration = context.registerService(HelloService.class.getName(),helloService,null);
		/*三个参数
		 * 1,service的接口名。如果service实现了多个接口,那样你需要传入一个包含所有接口名的String数组。在这里我们传入的是HelloService这个接口。
		 * 2,真正的service实现。在例子中我们传了一个HelloServiceImpl实现。
         * 3,service属性。这个参数可以在有多个service实现同一个接口的情况下,消费者用来区分真正感兴趣的service。
		 */
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		// TODO Auto-generated method stub
		helloServiceRegistration.unregister();
	}

}
------------------------------------
package com.huy.helloservice;

public class HelloServiceImpl implements HelloService {

	@Override
	public String sayHello() {
		// TODO Auto-generated method stub
		System.out.println("inside helloserviceImpl.sayHello()");
		return "Say Hello";
	}

}
---------------------------------------
package com.huy.helloworld;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import com.huy.helloservice.HelloService;

public class Activator implements BundleActivator {
	private ServiceReference helloServiceReference;
	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		System.out.println("Hello World!!");
		helloServiceReference = context.getServiceReference(HelloService.class.getName());
		HelloService hs = (HelloService)context.getService(helloServiceReference);
		hs.sayHello();
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		System.out.println("Goodbye World!!");
		context.ungetService(helloServiceReference);
	}

}

注意修改HelloService的MANIFEST.MF文件中Bundle-Activator


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值