Engine startup

This commit is contained in:
Fabio Sinibaldi 2022-04-07 16:14:52 +02:00
parent bb47c90c9e
commit 5d3e56754f
7 changed files with 97 additions and 20 deletions

View File

@ -2,14 +2,17 @@ package org.gcube.application.cms.implementations;
import lombok.Synchronized; import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.gcube.application.cms.caches.Cache; import org.gcube.application.cms.caches.Cache;
import org.gcube.application.cms.caches.Engine; import org.gcube.application.cms.caches.Engine;
import org.gcube.application.geoportal.common.model.rest.ConfigurationException; import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@Slf4j
public class ImplementationProvider { public class ImplementationProvider {
private static ImplementationProvider instance=null; private static ImplementationProvider instance=null;
@ -45,4 +48,50 @@ public class ImplementationProvider {
); );
return toReturn; return toReturn;
} }
private boolean isInit=false;
@Synchronized
public void initEngines(){
if(!isInit) {
log.info("INITIALIZING ENGINES. Size : {} ", implementationsRegistry.size());
HashSet<Class> failed = new HashSet<>();
implementationsRegistry.forEach((aClass, engine) -> {
log.info("Init : {} -> {}", engine.getClass().toGenericString(), aClass.getCanonicalName());
try {
engine.init();
} catch (Throwable t) {
failed.add(engine.getClass());
log.error("Unable to start engine {} ", engine.getClass(), t);
}
});
if (failed.isEmpty())
log.info("INIT OK");
else {
log.warn("!!!!! Following Engines FAILED INIT :");
failed.forEach(aClass -> log.warn(String.valueOf(aClass)));
}
isInit = true;
}else log.info("Received Init request but Engines already started");
}
public void shutdownEngines(){
log.info("SHUTTING DOWN ENGINES. Size : {} ",implementationsRegistry.size());
HashSet<Class> failed= new HashSet<>();
implementationsRegistry.forEach((aClass, engine) -> {
log.info("ShotDown : {} -> {}",engine.getClass().toGenericString(),aClass.getCanonicalName());
try{
engine.shutdown();
}catch (Throwable t){
failed.add(engine.getClass());
log.error("Unable to shutdown engine {} ",engine.getClass(),t);
}
});
if(failed.isEmpty())
log.info("SHUTDOWN OK");
else {
log.warn("!!!!! Following Engines FAILED SHUTDOWN :");
failed.forEach(aClass->log.warn(String.valueOf(aClass)));
}
}
} }

View File

@ -14,7 +14,7 @@ public class GCubeTest {
// testContext = "/pred4s/preprod/preVRE"; // testContext = "/pred4s/preprod/preVRE";
//testContext = "/gcube/devsec/devVRE"; testContext = "/gcube/devsec/devVRE";
System.out.println("TEST CONTEXT = "+testContext); System.out.println("TEST CONTEXT = "+testContext);

View File

@ -49,6 +49,7 @@
<artifactId>common-smartgears</artifactId> <artifactId>common-smartgears</artifactId>
</dependency> </dependency>
<!-- INTERNAL LOGIC --> <!-- INTERNAL LOGIC -->
<dependency> <dependency>
<groupId>org.gcube.application.cms</groupId> <groupId>org.gcube.application.cms</groupId>

View File

@ -1,18 +1,22 @@
package org.gcube.application.geoportal.service; package org.gcube.application.geoportal.service;
public class AppManager { import lombok.extern.slf4j.Slf4j;
import org.gcube.application.cms.implementations.ImplementationProvider;
import org.gcube.smartgears.ApplicationManager;
// implements ApplicationManager{ @Slf4j
//} public class AppManager implements ApplicationManager {
//
// @Override
// public void onInit() {
// ImplementationProvider.get().shutdown(); @Override
// } public void onInit() {
// ImplementationProvider.get().initEngines();
// @Override }
// public void onShutdown() {
// ImplementationProvider.get().shutdown(); @Override
// } public void onShutdown() {
ImplementationProvider.get().shutdownEngines();
}
} }

View File

@ -66,6 +66,7 @@ public class GeoPortalService extends ResourceConfig{
ImplementationProvider.get().getManagerList().forEach( ImplementationProvider.get().getManagerList().forEach(
(aClass, s) -> log.debug("{} serving {} ",aClass,s)); (aClass, s) -> log.debug("{} serving {} ",aClass,s));
ImplementationProvider.get().initEngines();
log.info("Initializing serialization"); log.info("Initializing serialization");
JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider(); JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
@ -75,4 +76,6 @@ public class GeoPortalService extends ResourceConfig{
} }
} }

View File

@ -1,5 +1,6 @@
package org.gcube.application.geoportal.service; package org.gcube.application.geoportal.service;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.gcube.application.cms.caches.Engine; import org.gcube.application.cms.caches.Engine;
import org.gcube.application.cms.implementations.ImplementationProvider; import org.gcube.application.cms.implementations.ImplementationProvider;
@ -11,6 +12,7 @@ import org.gcube.application.geoportal.service.engine.providers.ucd.ProfileMap;
import org.gcube.application.geoportal.service.rest.GuardedMethod; import org.gcube.application.geoportal.service.rest.GuardedMethod;
import org.gcube.application.cms.serialization.Serialization; import org.gcube.application.cms.serialization.Serialization;
import org.glassfish.jersey.test.JerseyTest; import org.glassfish.jersey.test.JerseyTest;
import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.opengis.annotation.Profile; import org.opengis.annotation.Profile;
@ -25,7 +27,7 @@ import static org.junit.Assume.assumeTrue;
@Slf4j @Slf4j
public class BasicServiceTestUnit extends JerseyTest { public class BasicServiceTestUnit extends JerseyTest {
protected static Boolean USE_LOCAL_FOLDER=false;
@Override @Override
@ -33,9 +35,12 @@ public class BasicServiceTestUnit extends JerseyTest {
return new GeoPortalService(){ return new GeoPortalService(){
@Override @Override
public Map<Engine<?>, Class<?>> customImplementations() { public Map<Engine<?>, Class<?>> customImplementations() {
if(USE_LOCAL_FOLDER)
return Collections.singletonMap((Engine<ProfileMap>) return Collections.singletonMap((Engine<ProfileMap>)
new LocalFolderProfileMapCache(TestProfiles.BASE_FOLDER.getAbsolutePath()),ProfileMap.class); new LocalFolderProfileMapCache(TestProfiles.BASE_FOLDER.getAbsolutePath()),ProfileMap.class);
else return Collections.emptyMap();
} }
}; };
} }
@ -55,7 +60,10 @@ public class BasicServiceTestUnit extends JerseyTest {
}); });
} }
@AfterClass
public static void shutdown(){
ImplementationProvider.get().shutdownEngines();
}
protected static<T> T check(Response resp, Class<T> clazz) throws Exception { protected static<T> T check(Response resp, Class<T> clazz) throws Exception {
String resString=resp.readEntity(String.class); String resString=resp.readEntity(String.class);

View File

@ -19,6 +19,7 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.List; import java.util.List;
import static junit.framework.Assert.fail;
import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue; import static junit.framework.TestCase.assertTrue;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
@ -47,8 +48,19 @@ public class UCDCalls extends BasicServiceTestUnit {
@Test @Test
public void getById() throws Exception { public void getById() throws Exception {
String testProfileId="profiledConcessioni"; List result=check(baseTarget().path(InterfaceConstants.Methods.QUERY_PATH).request(MediaType.APPLICATION_JSON_TYPE).
check(baseTarget().path(testProfileId).request(MediaType.APPLICATION_JSON_TYPE).get(), UseCaseDescriptor.class); post(Entity.entity(Serialization.write(new QueryRequest()),MediaType.APPLICATION_JSON)), List.class);
assertTrue("Existing UCDs : ",result.size()>0);
result.forEach(c->{
try{
UseCaseDescriptor d= Serialization.convert(c,UseCaseDescriptor.class);
check(baseTarget().path(d.getId()).request(MediaType.APPLICATION_JSON_TYPE).get(), UseCaseDescriptor.class);
} catch (Exception e) {
e.printStackTrace(System.err);
fail(e.getMessage());
}
});
} }
@Test @Test