- public java.lang.AutoCloseable createInstance() {
- TimeUnit unit = TimeUnit.MILLISECONDS;
- if (getTickDuration() != null && getThreadFactoryDependency() == null && getTicksPerWheel() == null) {
- return new HashedWheelTimerCloseable(new HashedWheelTimer(getTickDuration(), unit));
- }
- if (getTickDuration() != null && getThreadFactoryDependency() == null && getTicksPerWheel() != null) {
- return new HashedWheelTimerCloseable(new HashedWheelTimer(getTickDuration(), unit, getTicksPerWheel()));
- }
- if (getTickDuration() == null && getThreadFactoryDependency() != null && getTicksPerWheel() == null) {
- return new HashedWheelTimerCloseable(new HashedWheelTimer(getThreadFactoryDependency()));
- }
- if (getTickDuration() != null && getThreadFactoryDependency() != null && getTicksPerWheel() == null) {
- return new HashedWheelTimerCloseable(new HashedWheelTimer(getThreadFactoryDependency(), getTickDuration(),
- unit));
- }
- if (getTickDuration() != null && getThreadFactoryDependency() != null && getTicksPerWheel() != null) {
- return new HashedWheelTimerCloseable(new HashedWheelTimer(getThreadFactoryDependency(), getTickDuration(),
- unit, getTicksPerWheel()));
- }
- return new HashedWheelTimerCloseable(new HashedWheelTimer());
+ public AutoCloseable createInstance() {
+ // The service is provided via blueprint so wait for and return it here for backwards compatibility.
+ final WaitingServiceTracker<Timer> tracker = WaitingServiceTracker.create(
+ Timer.class, bundleContext, "(type=global-timer)");
+ final Timer timer = tracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
+
+ return Reflection.newProxy(AutoCloseableTimerInterface.class, new AbstractInvocationHandler() {
+ @Override
+ protected Object handleInvocation(final Object proxy, final Method method, final Object[] args) throws Throwable {
+ if (method.getName().equals("close")) {
+ tracker.close();
+ return null;
+ } else {
+ return method.invoke(timer, args);
+ }
+ }
+ });