zoukankan      html  css  js  c++  java
  • OSGi 系列(十四)之 Event Admin Service

    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

    图14.1 EventAdmin

    (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
    

    图14.2 测试结果

    注意: 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>
    
  • 相关阅读:
    【科普杂谈】计算机按下电源后发生了什么
    【VS开发】使用WinPcap编程(1)——获取网络设备信息
    【VS开发】使用WinPcap编程(1)——获取网络设备信息
    微信公众平台消息接口PHP版
    编码gbk ajax的提交
    mysql 查询
    js cookie
    js同域名下不同文件下使用coookie
    去掉A标签的虚线框
    jquery切换class
  • 原文地址:https://www.cnblogs.com/binarylei/p/8564458.html
Copyright © 2011-2022 走看看