In this post I am going to show you how to get notified, when the server is starting up and when the server is about to shut down.
In OSGi, bundle start up sequence is random. So you can't rely on the bundle start up sequence.
There are real world scenarios where you have some dependencies amount bundles, hence need to perform some actions before other dependent bundles get deactivated in the server shutdown.
Eg. Let's say you have to send messages to a external system. Your message sending module use your authentication module to authenticate the request and send it to the external system and your message sending module try to send all the buffered messages before the server shutdown.
Bundle unloading sequence in OSGi not happened in a guaranteed sequence. So, what would happen if your authentication bundle get deactivated before your message sending bundle get deactivated. In this case message sending module can't send the messages
To help these type of scenarios WSO2 Carbon framework provide you with a special OSGi service which can be used to detect the server start up and server shutdown
1. How to get notified the server startup
Implement the interface org.wso2.carbon.core.ServerStartupObserver [3], and register it as a service via the bundle context.
When the server is starting you will receive notifications via completingServerStartup() and completedServerStartup()
2. How to get notified the server shutdown
Implement the interface org.wso2.carbon.core.ServerShutdownHandler [4], and register it as a service via the bundle context.
When the server is about to shutdown you will receive the notification via invoke()
eg:
Bundle unloading sequence in OSGi not happened in a guaranteed sequence. So, what would happen if your authentication bundle get deactivated before your message sending bundle get deactivated. In this case message sending module can't send the messages
To help these type of scenarios WSO2 Carbon framework provide you with a special OSGi service which can be used to detect the server start up and server shutdown
1. How to get notified the server startup
Implement the interface org.wso2.carbon.core.ServerStartupObserver [3], and register it as a service via the bundle context.
When the server is starting you will receive notifications via completingServerStartup() and completedServerStartup()
2. How to get notified the server shutdown
Implement the interface org.wso2.carbon.core.ServerShutdownHandler [4], and register it as a service via the bundle context.
When the server is about to shutdown you will receive the notification via invoke()
eg:
1 2 3 4 5 6 7 | protected void activate(ComponentContext componentContext) { try { componentContext.getBundleContext().registerService(ServerStartupObserver.class.getName(), new CustomServerStartupObserver(), null) ; } catch (Throwable e) { log.error("Failed to activate the bundle ", e); } } |
[3] https://github.com/wso2/carbon4-kernel/blob/master/core/org.wso2.carbon.core/src/main/java/org/wso2/carbon/core/ServerStartupObserver.java
[4] https://github.com/wso2/carbon4-kernel/blob/master/core/org.wso2.carbon.core/src/main/java/org/wso2/carbon/core/ServerShutdownHandler.java
[4] https://github.com/wso2/carbon4-kernel/blob/master/core/org.wso2.carbon.core/src/main/java/org/wso2/carbon/core/ServerShutdownHandler.java
No comments:
Post a Comment