How to manage the lifecycle of application service?
Answered
I've tried to implement ApplicationComponent, org.picocontainer.{Startable, Disposible}
Please sign in to leave a comment.
I'm not sure what you want to achieve. If you implement the ApplicationComponent interface and register your class as a component, it will be created at application startup time and stopped at its shutdown. If you want your class to be created on demand but need to perform some handling on its shutdown, you need to register it as a service, perform the initialization in its constructor and implement the com.intellij.openapi.Disposable interface. The Startable and Disposable interfaces from picocontainer aren't used by our plugin framework.
com.intellij.openapi.Disposable works.
Thank you so much.
btw. There should be a unified api for componet and service.
IL
The Disposable API does work for both components and services.
How about start/init? more clear lifecycle?
Service is just a Component, so it should implement ComponentBase
Maybe jetbrains has thinked about to change the whole container. It's not easy.
IL
The initialization of a service is performed in the constructor. There is no need to provide any additional initialization methods.
Implementing the ComponentBase interface is optional for application or project components, and is very often not needed.
We do not have any plans to change the component model of IntelliJ IDEA.
The same question bothers me, I want to open a counter when the plugin startup and do a callback before the IDE close, so I think Application Component is a good solution to my problem(with initComponent and disposeComponent methods).
However, Component is marked @deprecated now, so I try to do the same things with Application Service, but I found it is not easy to use as Component, the Disposable#dispose() method does not work and I do not know when the initialization of a service will be performed.
So, please tell me how to use Application Service to take the place of ApplicationComponent#initComponent and ApplicationComponent#disposeComponent methods.
Besides, My intelliJ version is: id 'org.jetbrains.intellij' version '0.4.21', is it too old to use these features in Service?
Thanks!
Sorry to bother you, I've figured out how to use ApplicationService whose aim is to improve the performance when IDE startup by lazy instantiation. I move the init method to an appropriate place and use dispose() method instead of ApplicationComponent#disposeComponent method. It works well.
However, I found one difference with the official documents: I need to register the application service in plugin.xml, otherwise I will get 'null' of this service, but the official document says the 'Light-Service' can be declared with @Service annotation.
Please tell me if I don't understand this in a correct way.
Thanks!
Chentao Zhou, please upgrade your Gradle IntelliJ Plugin to the latest version, which is 1.2.0 for now (https://github.com/JetBrains/gradle-intellij-plugin/releases).
Components are deprecated now, check out IntelliJ SDK Documentation for more details regarding migration to services: https://plugins.jetbrains.com/docs/intellij/plugin-components.html