首页 技术 正文
技术 2022年11月12日
0 收藏 606 点赞 2,122 浏览 3070 个字

OSGi 系列(十四)之 Event Admin Service

OSGi 的 Event Admin 服务规范提供了开发者基于发布/订阅模型,通过事件机制实现 Bundle 间协作的标准通讯方式。

事件发布者使用 Event Admin 服务发送基于主题 (Topic) 的事件,任何对某一主题感兴趣的事件订阅者都会收到该事件,并且做出相应的反应。

1. Event Admin Service 介绍

(1) Event Admin Service 规范

compendium 规范提供了 Event Admin Service 服务,具体规范见 org.osgi.service.event

(2) Topic 的规范

  • /不能用于开头或者结尾
  • 可以使用 A-Z,a-z,0-9,-,_

2. 快速入门

2.1 环境准备

<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>5.0.0</version>
</dependency>

2.2 新建 2 个 bundle,一个发布事件(osgi-event-publish),一个接收事件(osgi-event-subscribe)

2.3 发布事件(osgi-event-publish)

import java.util.HashMap;
import java.util.Map;import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;public class EventPublish { private EventAdmin eventAdmin; public void publish() {
Map<String, String> content = new HashMap<>();
content.put("phone", "10086");
content.put("content", "ye");
eventAdmin.postEvent(new Event("send/10086", content));
} public void setEventAdmin(EventAdmin eventAdmin) {
this.eventAdmin = eventAdmin;
}
}

blueprint.xml 配制

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <reference id="eventAdmin" interface="org.osgi.service.event.EventAdmin" /> <bean class="com.edu.osgi.event.publish.EventPublish" init-method="publish">
<property name="eventAdmin" ref="eventAdmin" />
</bean>
</blueprint>

2.4 接收事件(osgi-event-subscribe)

import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;public class SendEventHandler implements EventHandler { public void handleEvent(Event event) {
System.out.println("=======handleEvent=======");
System.out.println(event.getTopic()); for(String key : event.getPropertyNames()) {
System.out.println(key + "=" + event.getProperty(key));
}
}
}

blueprint.xml 配制

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <service interface="org.osgi.service.event.EventHandler">
<service-properties>
<entry key="event.topics" value="send/*"/>
</service-properties>
<bean class="com.edu.osgi.event.subscribe.SendEventHandler" />
</service>
</blueprint>

2.5 karaf 测试

# 查看 EventAdmin 服务是否发布
ls EventAdmin
# 安装 eventadmin
feature:list | grep eventadmin
feature:install eventadmin

注意: osgi-event-subscribe 只能处理启动后发布的事件,不能处理启动前的事件。

3. 事件过滤

3.1 基于主题的过滤

<service interface="org.osgi.service.event.EventHandler">
<service-properties>
<entry key="event.topics" value="send/*"/>
</service-properties>
<bean class="com.edu.osgi.event.subscribe.SendEventHandler" />
</service>

3.2 基于内容的过滤

<service interface="org.osgi.service.event.EventHandler">
<service-properties>
<entry key="event.topics" value="send/*"/>
<entry key="event.filter" value="(phone=10010)"/>
</service-properties>
<bean class="com.edu.osgi.event.subscribe.SendEventHandler" />
</service>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,816
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899