In OSGi, how do you gracefully handle initialization exceptions?

I am using Maven-SCR. Using the @Component and @Service , I can configure an instance of the class that it provides automatically.

However, sometimes an activation method (labeled @Activate ) or even a constructor can throw an exception and crash in a way that I cannot handle.

The problem is this: I want to catch this exception so that I can register it correctly, but at the same time I want the class to not publish its services if it has not activated or initialized.

How do you do this?

Thanks!

+4
source share
2 answers

The SCR specification requires exceptions to these methods in order to register with the OSGi LogService. Do you have the LogService implementation package installed? If so, you may find exceptions to these methods registered there. If you need to register this exception in some other log, you can check something like Pax Logging .

+2
source

First, I will review the logging configuration of your OSGi structure. He must record these exceptions.

If this does not work, you can simply enter this type of code in your annotated method

 catch (Exception ex) { // or RuntimeException if possible // log it // then rethrow throw ex; } 

Note that this is considered an anti-pattern for some due to the repeated stack traces that it tends to create.

It can also be carried over to the aspect, although it can cause pain in OSGi.

+1
source

Source: https://habr.com/ru/post/1436596/


All Articles