SUPPORT-8529: Добавлен вызов http

This commit is contained in:
malkov34 2024-09-05 09:06:50 +03:00
parent c246132121
commit f5854c7592
9 changed files with 185 additions and 130 deletions

View file

@ -1,21 +1,26 @@
import java.time.Duration;
import javax.sql.DataSource;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import net.javacrumbs.shedlock.spring.ScheduledLockConfiguration;
import net.javacrumbs.shedlock.spring.ScheduledLockConfigurationBuilder;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import javax.sql.DataSource;
import java.time.Duration;
/**
* Root application context
* This context imports XML configs from all the other jars, and is created by {@link WebAppInitializer}
@ -41,6 +46,11 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@EnableScheduling
public class AppConfig {
@Value("${config-data-executor.socket-timeout:10}")
private int socketTimeout;
@Value("${config-data-executor.connection-timeout:10}")
private int connectionTimeout;
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
@ -62,4 +72,21 @@ public class AppConfig {
public LockProvider lockProvider(@Qualifier("datasource") DataSource dataSource) {
return new JdbcTemplateLockProvider(dataSource);
}
@Bean
public RestTemplate restTemplate() {
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout)
.setConnectionRequestTimeout(connectionTimeout)
.setConnectTimeout(connectionTimeout)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
return new RestTemplate(factory);
}
}

View file

@ -21,7 +21,7 @@ public class ConfigExecutorRpcService extends Behavior {
}
@RpcCall
public void callConfigExecutor(String path, List<String> ids) {
configExecutorService.callConfigExecutor(path, ids);
public void callConfigExecutor(String methodPath, List<String> ids) {
configExecutorService.call(methodPath, ids);
}
}

View file

@ -2,8 +2,11 @@ package service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.lang.invoke.MethodHandles;
import java.util.List;
@ -14,18 +17,27 @@ import java.util.List;
@Service
public class ConfigExecutorService {
@Value("${config-data-executor.host}")
String host;
@Value("${config-data-executor.port}")
String port;
private final RestTemplate restTemplate;
private final String url;
private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public ConfigExecutorService(@Autowired RestTemplate restTemplate,
@Value("${config-data-executor.url}") String url) {
this.restTemplate = restTemplate;
this.url = url;
}
public void callConfigExecutor(String path, List<String> ids) {
LOGGER.info("host: " + host);
LOGGER.info("port " + port);
LOGGER.info("path: " + path);
ids.forEach(LOGGER::info);
public void call(String methodPath, List<String> ids) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<List<String>> entity = new HttpEntity<>(ids, headers);
LOGGER.info("Starts call config executor service with method: {}, for ids: {}", methodPath, ids);
try {
ResponseEntity<Object> response = restTemplate.exchange(url.concat(methodPath), HttpMethod.POST, entity, Object.class);
LOGGER.info("Method: {}, executed with status: {}, for ids:{}", methodPath, response.getStatusCode().value(), ids);
} catch (Exception e) {
throw new RuntimeException(String.format(
"Failed call config executor service method: %s for ids: %s with error", methodPath, ids), e);
}
}
}