diff --git a/src/org/gcube/informationsystem/registry/impl/core/ServiceContext.java b/src/org/gcube/informationsystem/registry/impl/core/ServiceContext.java index dc6c8ed..c2cba3b 100644 --- a/src/org/gcube/informationsystem/registry/impl/core/ServiceContext.java +++ b/src/org/gcube/informationsystem/registry/impl/core/ServiceContext.java @@ -11,6 +11,8 @@ import org.gcube.common.core.resources.GCUBEHostingNode; import org.gcube.common.core.resources.GCUBEResource; import org.gcube.common.core.resources.GCUBERunningInstance; import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.common.core.utils.handlers.GCUBEHandler; +import org.gcube.common.core.utils.handlers.GCUBEScheduledHandler; import org.gcube.informationsystem.registry.impl.core.RegistryConfiguration.ROOT_SERVICES; /** @@ -35,6 +37,41 @@ public class ServiceContext extends GCUBEServiceContext { protected boolean isICCodeployed = false; + + protected class NotificationResourceScheduler extends GCUBEScheduledHandler { + + public NotificationResourceScheduler(long interval, Mode mode) { + super(interval, mode); + } + + protected boolean repeat(Exception exception, int exceptionCount) { + if (exception!=null) { + logger.warn("Failed to create the notification resource (attempt "+exceptionCount+" out of 20)",exception); + if (exceptionCount >= 20) { + logger.error("Max attempts reached, no more chance to register the notification resource"); + return false; + } + } + return true; + } + + } + + /** Tries to create the resource */ + protected class NotificationResource extends GCUBEHandler { + @SuppressWarnings("unchecked") + public void run() throws Exception { + try { + ServiceContext.getContext().setScope(ServiceContext.getContext().getInstance().getScopes().values().toArray(new GCUBEScope[0])[0]); + RegistryFactoryResource resource = (RegistryFactoryResource) FactoryContext.getContext().getWSHome().create( + FactoryContext.getContext().makeKey(RegistryFactory.NOTIFICATOR_RESOURCE_KEY)); + resource.getPersistenceDelegate().store(resource ); + } catch (Exception e) { + + throw e; + } + } + } private ServiceContext() {} @@ -54,7 +91,7 @@ public class ServiceContext extends GCUBEServiceContext { //creates the single RegistryResource used to raise notifications about profiles' changes logger.info("Creating IS-Registry notification resource..."); - new Thread() { + /*new Thread() { public void run() { try { Thread.sleep(5000); @@ -65,7 +102,11 @@ public class ServiceContext extends GCUBEServiceContext { } catch (Exception e) { ServiceContext.this.logger.error("Failed to create the notification resource", e); } - }}.start(); + }}.start();*/ + + NotificationResourceScheduler scheduler = new NotificationResourceScheduler(3000, GCUBEScheduledHandler.Mode.LAZY); + scheduler.setHandled(new NotificationResource()); + scheduler.run(); logger.info("IS-Registry notification resource registered");