unsupported type java.util.concurrent.atomic.AtomicReference

380 Views Asked by At

I am writing OSGI services .After Starting the service,I got Below Message.Kindly suggest to overcome this Issue.

@Component (service = Service.class, immediate = true)
public class ServiceImpl implements Service{

@Reference (name = "CounterService", policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MANDATORY,
            bind = "bindCounter", unbind = "unbindCounter", service = CounterService.class)
    public static final AtomicReference<CounterService> myCounterService = new AtomicReference<>();

public void bindCService(CounterService counterService)
{     
    this.cacheService= cacheService;
}

public void unbindService(CounterService cacheService)
{     
    this.cacheService= null;
}

}

And i got this Message after Starting Service

-!MESSAGE [com.xxx.xxx.xxx.CounterImpl(29)] Field counterService in component class com.xxx.xxx.xxx.CounterImpl has unsupported type java.util.concurrent.atomic.AtomicReference

2

There are 2 best solutions below

0
Peter Kriens On

Well, why the AtomicReference? You also don't need bind and unbind services. You seem confused :-) You might want to take a look at the videos or the OSGi starter

For the dynamic case, this is what would work:

@Component(immediate = true)
public class ServiceImpl implements Service {

    @Reference    
    volatile CounterService myCounterService;
    
    @Activate
   void activate() {
    ...
    }
    
}

If the CounterService is not dynamic, this is a very nice pattern:

@Component(immediate = true)
public class ServiceImpl implements Service {

    final CounterService myCounterService;
    
    @Activate
    public ServiceImpl( @Reference  CounterService  c ) {
        this.myCounterService = c;
    }
    
}
0
Saurav Suman On

Another way to fix this is to remove the @Reference annotation from the field and add it to your bind method. Something like this:

@Component (service = Service.class, immediate = true)
public class ServiceImpl implements Service{

    public static final AtomicReference<CounterService> myCounterService = new AtomicReference<>();

@Reference (name = "CounterService", policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MANDATORY,
            bind = "bindCounterService", unbind = "unbindCounterService", service = CounterService.class)
public void bindCounterService(CounterService counterService)
{     
     myCounterService = counterService;
}

public void unbindCounterService(CounterService counterService)
{     
    myCounterService = null;
}

}