SpringDM笔记16-处理OSGi服务的动态性:事件

本文介绍了SpringDM中两种实现服务监听的方式:通过实现特定接口或配置服务监听器。包括服务注册与注销事件监听及服务绑定与解绑事件监听的具体配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Service registration and unregistration events

    Spring DM provides two ways to implement and configure listeners:

    ■ Make the service implement the OsgiServiceRegistrationListener interface. When this interface

       is present, Spring DM will automatically register the bean as a listener of the service’s lifecycle.

       This is a reasonable approach, but it ties your implementation to the Spring DM API.
    ■ Specify a service listener configuration in the service tag. This approach is preferred because it

       allows you to use a POJO as a listener. The association between events and callback methods is

       specified through the service listener configuration.

    Spring DM provides a registration-listener element to be used as an inner tag of the service tag.

    Attributes of the registration-listener element:

    (1)Attribute:ref

    Description:Specifies the identifier of the bean to be used as a service listener.

    (2)Attribute: registrationmethod 

    Description:Specifies the method of the listener bean to be called when the service is registered.
    The method must have exactly two parameters: One of the type of the  service and corresponds

    to the service that’s being listened to. A second which corresponds to the service’s properties

    and can be of type Map or Dictionary.

    (3)Attribute:  unregistrationmethod 

    Description:Specifies the method of the listener bean to be called when the service  is
    unregistered. The method signature follows the same rules as for registration-method.

    示例:

    Contents of a registration service listener:

    public class TestRegistrationServiceListener {
         public void onServiceRegistered(TestService service, Map properties) {
               (...)
         }
         public void onServiceUnregistered(TestService service, Map properties) {
               (...)
         }
    }

    <bean id="testListener" 

           class="com.manning.sdmia.springdm.service.impl.TestRegistrationServiceListener"/>
    <osgi:service id="testService" ref="testServiceBean"

           interface="com.manning.sdmia.springdm.service.TestService">
           <registration-listener ref="testListener" registration-method="onServiceRegistered"
                  unregistration-method="onServiceUnregistered"/>
    </osgi:service>

2. Service bind and unbind events

    Spring DM provides two different methods for receiving notifications when the configured service

    is bound or unbound by Spring DM.

    ■ Make the listener implement the OsgiServiceLifecyleListener interface. When this interface is

       present, Spring DM will automatically use the interface’s methods when binding and unbinding

       occur.
    ■ Specify a service reference listener configuration in the reference tag. This approach is

       preferred because it allows you to use a POJO as a listener. The association between events

       and callback methods is specified through the service reference listener’s configuration.

       Attributes of the listener tag:

       (1)ref:Specifies the identifier of the bean to be used as a service listener.

       (2)bind-method:Specifies the method of the listener to be called when a reference to the

           service is bound. The method must have two parameters: The first is of the type of the
           service and corresponds to the service. The second corresponds to the service properties

           and can be of type Map or Dictionary.

       (3)unbind-method:Specifies the method of the listener to be called when a reference to the

           service is unbound. The method signature follows the same rules as for the bindmethod
           attribute.

       示例:

       public interface OsgiServiceLifecycleListener {
             void bind(Object service, Map properties);
             void unbind(Object service, Map properties);
       }

      public class TestServiceListener {
            public void onServiceBound (TestService service, Map properties) {
                   (...)
            }
            public void onServiceUnbound (TestService service, Map properties) {
                   (...)
            }
      }

     <bean id="testListener"

             class="com.manning.sdmia.springdm.service.impl.TestRegistrationServiceListener"/>
     <osgi:reference id="testService"
             interface="com.manning.sdmia.springdm.service.TestService">
             <listener ref="testListener" bind-method="onServiceBound" unbind-

                    method="onServiceUnbound"/>
     </osgi:service>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值