javaosgiapache-felixdeclarative-services

In OSGi, how do you gracefully handle initialisation exceptions?


I am using Maven-SCR. By using the @Component and @Service tags, I can have my class instance register the interfaces it provides automatically.

On occasion, however, the activate method (marked as @Activate) or even the constructor, might throw an exception and fail in a way I can't handle.

The issue is: I want to catch that exception so I can properly log it, but at the same time, I want to prevent that the class publishes its services if it failed to activate or initialize.

How do you guys do this?

Thanks!


Solution

  • The SCR specification requires exceptions from these methods to be logged to the OSGi LogService. Do you have a LogService implementation bundle installed? If so, you can find the exceptions from these methods logged there. If you need to log these exception to some other log, the you might want to check out something like Pax Logging.