osgi实战学习之路:6. Service-1

什么是Service?

它是注册到osgi的一个java对象

Service注册:

通过BundleContext::registerService(java.lang.String[] clazzes, java.lang.Object service, java.util.Dictionary properties) 

Service查找及使用:

通过BundleContext::getServiceReference(java.lang.String clazz),返回ServiceReference
通过BundleContext::getService(ServiceReference reference) ,返回注册的服务对象

Service释放:

通过BundleContext::ungetService(ServiceReference reference) 

LADP:

轻量级目录访问协议(Lightweight Directory Access Protocol)

1个Service对应一个实现类的注册与使用demo:

服务提供者:

student-manage/IStudentManage.java

package com.demo.service;

public interface IStudentManage {
	void add();
}

student-manage/Activator.java

package com.demo.service;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

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

import com.demo.service.impl.StudentManage;

public class Activator implements BundleActivator {

	public void start(BundleContext context) throws Exception {
		System.out.println("注册服务开始....");
		context.registerService(IStudentManage.class.getName(),
				new StudentManage(), null);
		System.out.println("注册服务结束....");
	}

	public void stop(BundleContext context) throws Exception {

	}

}

服务使用者:

student-action/Activator.java

package com.demo.action;

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

import com.demo.service.IStudentManage;

public class Activator implements BundleActivator{
	public void start(BundleContext context) throws Exception {
		System.out.println("action  begin...");
		ServiceReference sf=null;
		try {
			//查找Service
			sf=context.getServiceReference(IStudentManage.class.getName());
			//调用服务
			IStudentManage studentManage = (IStudentManage)context.getService(sf);
			studentManage.add();
		} finally {
			context.ungetService(sf);
		}
		System.out.println("action  end...");
	}

	public void stop(BundleContext context) throws Exception {
	}

}

部署至karaf并查看结果:

一个Service对应多个实现(基于LDAP)demo2

服务提供者

student-manage/Activator.java

package com.demo.service;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

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

import com.demo.service.impl.StudentManageA;
import com.demo.service.impl.StudentManageB;

public class Activator implements BundleActivator {

	public void start(BundleContext context) throws Exception {
		System.out.println("注册服务开始....");
		//注册A
		Hashtable<String, String> dict=new Hashtable<String, String>();
		dict.put("name", "a");
		context.registerService(IStudentManage.class.getName(),
				new StudentManageA(), dict);
		//注册B
		dict=new Hashtable<String, String>();
		dict.put("name", "b");
		context.registerService(IStudentManage.class.getName(),
				new StudentManageB(), dict);
		System.out.println("注册服务结束....");
	}

	public void stop(BundleContext context) throws Exception {

	}

}

服务使用者

student-action/Activator.java

package com.demo.action;

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

import com.demo.service.IStudentManage;

public class Activator implements BundleActivator{
	public void start(BundleContext context) throws Exception {
		System.out.println("action  begin...");
		ServiceReference[] references=null;
		try {
			System.out.println("服务调用------------------");
			//查找Service
			String filter="(name=b)";
			references=context.getServiceReferences(IStudentManage.class.getName(), filter);
			//调用服务
			if(references.length==1){
				IStudentManage studentManage = (IStudentManage)context.getService(references[0]);
				studentManage.add();
			}else {
				throw new IllegalArgumentException("IStudentManage查找到多个实现类");
			}

		} finally {
			for(ServiceReference sf:references){
				context.ungetService(sf);
			}

		}
		System.out.println("action  end...");
	}

	public void stop(BundleContext context) throws Exception {
	}

}

部署到karaf及查看结果:

osgi实战学习之路:6. Service-1,布布扣,bubuko.com

时间: 2024-10-24 22:08:47

osgi实战学习之路:6. Service-1的相关文章

osgi实战学习之路:8. Service-3之ServiceTracker

通过ServiceTracker可以对查找的Service进行扩展 下面的demo引入装饰器模式对Service进行日志的扩展 demo: Provider student-manage/Activator.java package com.demo.service; import java.util.Dictionary; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import org

osgi实战学习之路:7. Service-2之ServiceListener

ServiceListener三种状态: ServiceEvent.REGISTERED ServiceEvent.MODIFIED ServiceEvent.UNREGISTERING 基于ServiceListener实现服务查找的demo Provider student-manage/Activator.java package com.demo.service; import java.util.Dictionary; import java.util.HashMap; import

osgi实战学习之路:3. osgi分层概念及相互合作demo

源代码下载 分层: modual: 主要作用于包级管理与共享代码 lifecycle: 主要作用于运行期间的模块管理与访问osgi底层框架 service: 主要作用于多模块之间的相互通信 demo: hello-provider/pom.xml <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.or

osgi实战学习之路:5.生命周期及利用命令、装饰者模式实现基于socket交互Bundle命令demo

生命周期中关键3个类: BundleActivator 入口点,类似main方法 BundleContext Bundle上下文对象,在运行期间,为应用程序提供操作osgi框架的方法 Bundle 代表一个已安装的Bundle 接口说明: BundleActivator: public interface BundleActivator { public void start(BundleContext context) throws Exception; public void stop(Bu

osgi实战学习之路:2. maven+maven-bundle-plugin+karaf搭建osgi之HelloWorld

环境准备: jdk版本 jdk:1.7 karaf: 版本:apache-karaf-3.0.1 下载地址: http://pan.baidu.com/s/1qWM4Y1u http://karaf.apache.org/ 配置本地仓库: 参考:http://blog.csdn.net/wobendiankun/article/details/25333113 启动karaf: karaf_home/bin/karaf.bat 启动成功如下: 安装 mvn-hello-provider 到本地仓

osgi实战学习之路:4.Bundle

源代码下载 Bundle是什么? Bundle是一个标准的jar,只是在META-INF/MANIFEST.MF中加入Bundle元数据的描述 Bundle元数据 标识 Bundle-ManifestVersion: 2 Bundle-Name: student-model Bundle-SymbolicName: com.demo.student-model Bundle-Version: 0.0.1.SNAPSHOT Export-Package 导出Bundle的某些包中的代码让其它Bun

JavaEE学习之路-Writing Service Methods

The service provided by a servlet is implemented in the service method of a GenericServlet, in the doMethod methods (where Method can take the value Get,Delete, Options, Post, Put, or Trace) of an HttpServlet object, or in any other protocol-specific

Android学习笔记五之Service

Android学习笔记五之Service 1.什么是Service? 什么是Service?Service是Android系统的四大组件之一,官方文档是这样描述Service的: A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application comp

Azure云平台学习之路(三)——Cloud Services

1.什么是云服务? 能够部署高度可用的且可无限缩放的应用程序和API.简而言之,就是你写的CMD程序按照一定的框架进行少量修改就能运行在Azure云平台上. 2.Azure云服务有什么特点? (1)专注应用程序而不是硬件,PaaS的一种. (2)支持多种框架和语言. (3)集成了运行状况监视和负载平衡. (4)自动缩放优化成本和性能 3.建立云服务之前,我们需要建立一个云存储,来记录我们的程序的日志信息(当然,这不是必须的) (1)选择左边导航栏的"存储".主面板上显示的是所有已有的存