113 lines
4.3 KiB
Java
113 lines
4.3 KiB
Java
package eu.openaire.urls_controller;
|
|
|
|
import eu.openaire.urls_controller.controllers.UrlsController;
|
|
import eu.openaire.urls_controller.services.BulkImportServiceImpl;
|
|
import eu.openaire.urls_controller.services.UrlsServiceImpl;
|
|
import eu.openaire.urls_controller.util.FileUtils;
|
|
import eu.openaire.urls_controller.util.UriBuilder;
|
|
import io.micrometer.core.aop.TimedAspect;
|
|
import io.micrometer.core.instrument.MeterRegistry;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.boot.CommandLineRunner;
|
|
import org.springframework.boot.SpringApplication;
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.core.env.Environment;
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
import org.springframework.web.cors.CorsConfiguration;
|
|
import org.springframework.web.cors.CorsConfigurationSource;
|
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
|
|
import javax.annotation.PreDestroy;
|
|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
@SpringBootApplication
|
|
@EnableScheduling
|
|
public class Application {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(Application.class);
|
|
|
|
private static ConfigurableApplicationContext context;
|
|
|
|
|
|
public static void main(String[] args) {
|
|
context = SpringApplication.run(Application.class, args);
|
|
}
|
|
|
|
@Bean
|
|
public CorsConfigurationSource corsConfigurationSource() {
|
|
CorsConfiguration configuration = new CorsConfiguration();
|
|
configuration.setAllowedOrigins(Collections.singletonList("*"));
|
|
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
|
|
configuration.setAllowedHeaders(Arrays.asList("authorization", "content-type", "x-auth-token"));
|
|
configuration.setExposedHeaders(Collections.singletonList("x-auth-token"));
|
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
|
source.registerCorsConfiguration("/**", configuration);
|
|
return source;
|
|
}
|
|
|
|
|
|
public static void gentleAppShutdown()
|
|
{
|
|
int exitCode = 0;
|
|
try {
|
|
exitCode = SpringApplication.exit(context, () -> 0); // The "PreDestroy" method will be called. (the "context" will be closed automatically (I checked it))
|
|
} catch (IllegalArgumentException iae) {
|
|
logger.error(iae.getMessage()); // This will say "Context must not be null", in case the "gentleAppShutdown()" was called too early in the app's lifetime. But it's ok.
|
|
}
|
|
System.exit(exitCode);
|
|
}
|
|
|
|
|
|
@PreDestroy
|
|
public void preDestroy() {
|
|
logger.info("Shutting down the threads..");
|
|
|
|
shutdownThreads(UrlsServiceImpl.insertsExecutor);
|
|
shutdownThreads(FileUtils.hashMatchingExecutor);
|
|
shutdownThreads(UrlsController.backgroundExecutor);
|
|
shutdownThreads(BulkImportServiceImpl.bulkImportExecutor);
|
|
|
|
logger.info("Exiting..");
|
|
}
|
|
|
|
|
|
private void shutdownThreads(ExecutorService executorService)
|
|
{
|
|
executorService.shutdown(); // Define that no new tasks will be scheduled.
|
|
try {
|
|
if ( ! executorService.awaitTermination(2, TimeUnit.MINUTES) ) {
|
|
logger.warn("The working threads did not finish on time! Stopping them immediately..");
|
|
executorService.shutdownNow();
|
|
}
|
|
} catch (SecurityException se) {
|
|
logger.error("Could not shutdown the threads in any way..!", se);
|
|
} catch (InterruptedException ie) {
|
|
try {
|
|
executorService.shutdownNow();
|
|
} catch (SecurityException se) {
|
|
logger.error("Could not shutdown the threads in any way..!", se);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@Bean
|
|
public CommandLineRunner setServerBaseUrl(Environment environment, ServletWebServerApplicationContext webServerAppCtxt)
|
|
{
|
|
return args -> new UriBuilder(environment, webServerAppCtxt);
|
|
}
|
|
|
|
|
|
@Bean
|
|
public TimedAspect timedAspect(MeterRegistry registry) {
|
|
return new TimedAspect(registry);
|
|
}
|
|
|
|
} |