java - How do I make a HK2 ServiceLocator use the Singleton service instances from the ServiceLocator that it's bridged from? -
we using using extrasutilities.bridgeservicelocator()
inject existing singleton application services created in 1 servicelocator jersey restful web services bridging app servicelocator jersey servicelocator.
however singletons exist in 'outer' locator aren't being used - each of services being created once again when injected jersey services. seems singleton visible within scope of servicelocator, if it's bridged.
is intended behaviour? , if there way change behaviour , have true singleton across bridged servicelocators?
i have extracted issue out set of test classes illustrate point below:
public class bridgedservicetest { private servicelocator _outerservicelocator; private servicelocator _innerservicelocator; @test public void testbridgedinnerserviceok() throws exception { servicelocatorfactory servicelocatorfactory = servicelocatorfactory.getinstance(); _outerservicelocator = servicelocatorfactory.create("outer"); servicelocatorutilities.addclasses(_outerservicelocator, singletonserviceimpl.class); _innerservicelocator = servicelocatorfactory.create("inner"); extrasutilities.bridgeservicelocator(_innerservicelocator, _outerservicelocator); final client client1 = new client(); _outerservicelocator.inject(client1); assertthat(singletonserviceimpl.instancecount.get(), matchers.is(1)); client1.test(); final client client2 = new client(); _innerservicelocator.inject(client2); // next line fails instance count 2 assertthat(singletonserviceimpl.instancecount.get(), matchers.is(1)); client2.test(); } @after public void teardown() throws exception { _innerservicelocator.shutdown(); _outerservicelocator.shutdown(); } } @contract public interface singletonservice { void fulfil(); } @service public class singletonserviceimpl implements singletonservice { public static atomicinteger instancecount = new atomicinteger(); @postconstruct public void postconstruct() { instancecount.incrementandget(); } @override public void fulfil() { system.out.println("contract fulfilled."); } } public class client { @inject private singletonservice _singletonservice; public void test() { _singletonservice.fulfil(); } public singletonservice getsingletonservice() { return _singletonservice; } }
there bug in servicelocator bridge has been fixed. fix in hk2 2.5.0-b07 or later!
Comments
Post a Comment