Apache Felix Event Admin在ServiceMix容器下使用

1、首先在pom文件中引用

<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.event</artifactId>
<version>1.3.1</version>
<type>jar</type>
</dependency>

2、在blueprint文件下引入Event服务,才可以将消息推送到Event里面

<reference id="EventAdmin" interface="org.osgi.service.event.EventAdmin" availability="mandatory"/>

3、将服务注入到Bean里面使用

public class EventAdminUtils{
    private EventAdmin eventAdmin;

    public EventAdmin getEventAdmin() {
        return eventAdmin;
    }

    public void setEventAdmin(EventAdmin eventAdmin) {
        this.eventAdmin = eventAdmin;
    }

    public boolean SyncEvent(String topic, Dictionary props) {
        Event reportGeneratedEvent = new Event(topic,props);
        eventAdmin.sendEvent(reportGeneratedEvent);
        return true;
    }

    public boolean AsyncEvent(String topic, Dictionary props) {
        Event reportGeneratedEvent = new Event(topic,props);
        eventAdmin.postEvent(reportGeneratedEvent);
        return true;
    }

}

blueprint注入服务

<bean id="adminEvent" class="com.test.event.impl.utils">
     <property name="eventAdmin" ref="EventAdmin"/>
</bean>

4、当做一个服务发布出去,需要用到的地方,引用服务就可以

创建一个接口类:

public interface IEventAdminUtils {
    public boolean SyncEvent(String topic,Dictionary props);
    public boolean AsyncEvent(String topic,Dictionary props);
}

创建一个实现类:

public class EventAdminUtils implements IEventAdminUtils{
    private EventAdmin eventAdmin;

    public EventAdmin getEventAdmin() {
        return eventAdmin;
    }

    public void setEventAdmin(EventAdmin eventAdmin) {
        this.eventAdmin = eventAdmin;
    }

    public boolean SyncEvent(String topic, Dictionary props) {
        Event reportGeneratedEvent = new Event(topic,props);
        eventAdmin.sendEvent(reportGeneratedEvent);
        return true;
    }

    public boolean AsyncEvent(String topic, Dictionary props) {
        Event reportGeneratedEvent = new Event(topic,props);
        eventAdmin.postEvent(reportGeneratedEvent);
        return true;
    }

}

blueprint中发布服务

<service ref="Sender" interface="com.test.event.utils.IEventAdminUtils">
    </service>

5、从Event里面拿出内容

public class Handler implements EventHandler {

    @Override
    public void handleEvent(Event event) {
        String reportMsg = (String) event.getProperty("message");
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值