CommonConfig.java

  1. package no.nav.data.common;

  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import io.micrometer.core.aop.CountedAspect;
  4. import io.micrometer.core.aop.TimedAspect;
  5. import io.micrometer.core.instrument.MeterRegistry;
  6. import io.prometheus.client.CollectorRegistry;
  7. import lombok.extern.slf4j.Slf4j;
  8. import net.javacrumbs.shedlock.core.LockProvider;
  9. import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
  10. import no.nav.data.common.utils.JsonUtils;
  11. import no.nav.data.common.web.TraceHeaderRequestInterceptor;
  12. import org.springframework.boot.web.client.RestTemplateBuilder;
  13. import org.springframework.context.annotation.*;
  14. import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
  15. import org.springframework.jdbc.core.JdbcTemplate;
  16. import org.springframework.web.client.RestTemplate;


  17. import javax.sql.DataSource;

  18. import static java.util.Collections.emptyList;

  19. @Slf4j
  20. @Configuration
  21. @EnableAspectJAutoProxy
  22. public class CommonConfig {

  23.     @Primary
  24.     @Bean
  25.     public ObjectMapper objectMapper() {
  26.         return JsonUtils.getObjectMapper();
  27.     }

  28.     @Bean
  29.     public RestTemplate restTemplate(RestTemplateBuilder builder) {
  30.         return builder
  31.                 .additionalInterceptors(TraceHeaderRequestInterceptor.fullInterceptor())
  32.                 .build();
  33.     }

  34.     @Bean
  35.     @Profile("!test & !local")
  36.     public RestTemplate externalRestTemplate(RestTemplateBuilder builder) {
  37.         return builder
  38.                 .additionalInterceptors(TraceHeaderRequestInterceptor.correlationInterceptor())
  39.                 .build();
  40.     }

  41.     @Bean
  42.     public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
  43.         MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
  44.         jsonConverter.setObjectMapper(objectMapper());
  45.         return jsonConverter;
  46.     }

  47.     /**
  48.      * Make sure spring uses the defaultRegistry
  49.      */
  50.     @Bean
  51.     public CollectorRegistry collectorRegistry() {
  52.         return CollectorRegistry.defaultRegistry;
  53.     }

  54.     @Bean
  55.     public LockProvider lockProvider(DataSource dataSource) {
  56.         return new JdbcTemplateLockProvider(JdbcTemplateLockProvider.Configuration.builder()
  57.                 .withJdbcTemplate(new JdbcTemplate(dataSource))
  58.                 .usingDbTime()
  59.                 .build());
  60.     }

  61.     @Bean
  62.     TimedAspect timedAspect(MeterRegistry meterRegistry) {
  63.         return new TimedAspect(meterRegistry,
  64.                 proceedingJoinPoint -> emptyList(),
  65.                 proceedingJoinPoint -> false
  66.         );
  67.     }

  68.     @Bean
  69.     CountedAspect countedAspect(MeterRegistry meterRegistry) {
  70.         return new CountedAspect(meterRegistry,
  71.                 proceedingJoinPoint -> emptyList(),
  72.                 proceedingJoinPoint -> false
  73.         );
  74.     }
  75. }