I have a optional service dependency that looks something like this:
@Component
class TestComponent {
private AtomicReference<TestService> testServiceRef;
@Reference(type = '?')
protected void setTestService(TestService testService) {
testServiceRef.set(testService);
}
protected void unsetTestService(TestService testService) {
testServiceRef.set(null);
}
public void doStuff() {
TestService testService = testServiceRef.get();
if (testService != null)
testService.performSomeTask();
}
}
Now if the service becomes unbound while testService.performSomeTask() is running, I have a problem, correct? Do I need to add sychronization blocks in all these functions, or is there a better way to handle this sort of scenario?