首页 > 代码库 > Spring DM所提供的Bundle监听接口OsgiBundleApplicationContextListener

Spring DM所提供的Bundle监听接口OsgiBundleApplicationContextListener

通过使用Spring DM提供的监听接口,我们可以更好的对服务进行管控,其实该接口也非常简单就能使用上,只要自定义一个类实现OsgiBundleApplicationContextListener接口,实现onOsgiApplicationEvent方法,并将自定义类注册成OSGI服务,通过判断event的事件类型来执行相应逻辑处理,同时也可以获取到当前监听的Bundle,从而达到更深层次的监控,

 

public class ListenerDemo implements OsgiBundleApplicationContextListener{

    @Override
    public void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent event) {
        
        String bundleName = event.getBundle().getSymbolicName();
        
        if (event instanceof OsgiBundleContextRefreshedEvent) {//Spring上下文创建成功事件
            System.out.println("服务启动");
        }else if (event instanceof OsgiBundleContextClosedEvent) {//Spring上下文销毁事件
            System.out.println("服务关闭");
            
        }
    }
}

 

<bean id="ListenerDemo" class="com.sample.service.listener.ListenerDemo"></bean>
    
    <osgi:service ref="ListenerDemo" interface="org.springframework.osgi.context.event.OsgiBundleApplicationContextListener"/>

 

Spring DM所提供的Bundle监听接口OsgiBundleApplicationContextListener