`

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

 
阅读更多

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>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics