issues/31 перенос тестовых контроллеров в отдельный модуль (т.к. опасные функции)

This commit is contained in:
akurakin 2023-09-27 18:37:42 +03:00 committed by AKurakin
parent 1c9de78f5e
commit bf3ba4e6aa
20 changed files with 978 additions and 230 deletions

View file

@ -10,7 +10,6 @@ import org.springframework.stereotype.Component;
public class ImdgSettings { public class ImdgSettings {
private HazelcastServerSettings hazelcast; private HazelcastServerSettings hazelcast;
private DatabaseSettings database; private DatabaseSettings database;
private ControllerSettings debugServer;
public HazelcastServerSettings getHazelcast() { public HazelcastServerSettings getHazelcast() {
return hazelcast; return hazelcast;
@ -27,12 +26,4 @@ public class ImdgSettings {
public void setDatabase(DatabaseSettings database) { public void setDatabase(DatabaseSettings database) {
this.database = database; this.database = database;
} }
public ControllerSettings getDebugServer() {
return debugServer;
}
public void setDebugServer(ControllerSettings debugServer) {
this.debugServer = debugServer;
}
} }

View file

@ -16,7 +16,6 @@ import ru.spcex.clearing.imdg.base.SimpleObjectMapStore;
import ru.spcex.platform.imdg.iml.hazelcast.util.HazelcastHelper; import ru.spcex.platform.imdg.iml.hazelcast.util.HazelcastHelper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.concurrent.*; import java.util.concurrent.*;
@ -122,77 +121,6 @@ public abstract class AbstractHazelcastLifecycleSupport implements InitializingB
} }
public int reloadMapFromDB() {
log.info("Reload all from DB...");
long loadTime = System.currentTimeMillis();
int count = 0;
try {
List<Callable<Long>> tasks = new ArrayList<>();
Collection<String> mapNames = hazelcastServerInstance.getConfig().getMapConfigs().keySet();
for (String mapName : mapNames) {
tasks.add(() -> {
Long maxKey = null;
MapStoreConfig mapStoreConfig = hazelcastServerInstance.getConfig().getMapConfig(mapName).getMapStoreConfig();
if (mapStoreConfig != null && mapStoreConfig.isEnabled()) {
long start = System.currentTimeMillis();
log.debug("evict map {}", mapName);
IMap<Long, BusinessObject> map = hazelcastServerInstance.getMap(mapName);
map.evictAll();
log.debug("Load map {}", mapName);
map.loadAll(false);
int size = map.size();
long time = System.currentTimeMillis() - start;
log.debug("{} {} rows reloaded in {}ms", mapName, size, time);
Object mapStore = mapStoreConfig.getImplementation();
if (mapStore instanceof SimpleObjectMapStore) {
String tableName = ((SimpleObjectMapStore) mapStore).getTableName();
maxKey = map.keySet().stream().max(Long::compareTo).orElse(null); // jdbcTemplate.queryForObject("select max(id) from " + tableName, Long.class);
} else if (mapStore instanceof DictionaryMapStore) {
// для Dictionary не используется общий id генератор
// } else if (mapStore instanceof FrontendUserSessionMapStore) {
// // не используется общий id генератор
} else {
throw new RuntimeException("unknown map store implementation " + mapStore);
}
log.debug("{} max(id)={}", mapName, maxKey);
}
return maxKey;
});
}
long maxKey = 0L;
int threadCount = Runtime.getRuntime().availableProcessors();// todo config * Config.get().getRoot().getSettings().getInitHazelcastThreadMultiplier();
log.info("Initializing threads count = {}", threadCount);
ExecutorService executor = Executors.newWorkStealingPool(threadCount);
try {
List<Future<Long>> results = executor.invokeAll(tasks);
for (Future<Long> result : results) {
Long maxKeyResult = result.get();
if (maxKeyResult != null) {
maxKey = Math.max(maxKey, maxKeyResult);
}
count++;
}
} finally {
executor.shutdown();
}
log.info("IDGenerator can not reinit. Max map ID {}", maxKey);
// plannerAllTodayMaker.makeSchedulerAllTodayMap();
} catch (InterruptedException | ExecutionException e) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw new RuntimeException("MapStore multithreaded reload not complete.", e);
}
// HazelcastHelper.imdgSystem_setStorageState(true, hazelcastServerInstance);
loadTime = System.currentTimeMillis() - loadTime;
log.info("All map reload time {} ms", loadTime);
return count;
}
@Override @Override
public void destroy() { public void destroy() {
hazelcastServerInstance.shutdown(); hazelcastServerInstance.shutdown();

View file

@ -5,8 +5,4 @@ imdg.hazelcast.cluster-members[0]=127.0.0.1
imdg.database.login=clearing imdg.database.login=clearing
imdg.database.password=Aa111111 imdg.database.password=Aa111111
imdg.database.url=jdbc:postgresql://10.200.200.133:5432/clearing?currentSchema=clearing_prod imdg.database.url=jdbc:postgresql://10.200.200.133:5432/clearing?currentSchema=clearing_prod
#imdg.database.url=jdbc:postgresql://10.200.200.133:5432/postgres?currentSchema=clearing_tester #imdg.database.url=jdbc:postgresql://10.200.200.133:5432/postgres?currentSchema=clearing_tester
#debug tester mode:
#imdg.debug-server.port=8701
#imdg.debug-server.context-path=/imdg/reload

View file

@ -35,6 +35,7 @@
<module>clearing-service</module> <module>clearing-service</module>
<module>registry-service</module> <module>registry-service</module>
<module>test-clearing</module> <module>test-clearing</module>
<module>test-api-clearing</module>
<module>cleaning-builders</module> <module>cleaning-builders</module>
<module>trade-importer</module> <module>trade-importer</module>
<module>lim-exporter</module> <module>lim-exporter</module>

View file

@ -0,0 +1,135 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.spcex.clearing</groupId>
<artifactId>clearing-parent</artifactId>
<version>SPCEX-1.0.0.0</version>
</parent>
<artifactId>test-api-clearing</artifactId>
<name>Test-api-clearing</name>
<version>SPCEX-1.0.0.0</version>
<description>Тестовые контроллеры для отладки клиринговой системы. Только для разработчиков.</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>ru.spcex.platform</groupId>
<artifactId>platform-messaging</artifactId>
</dependency>
<dependency>
<groupId>ru.spcex.platform</groupId>
<artifactId>platform-enum</artifactId>
</dependency>
<dependency>
<groupId>ru.spcex.platform</groupId>
<artifactId>platform-enum</artifactId>
</dependency>
<dependency>
<groupId>ru.spcex.clearing</groupId>
<artifactId>classes</artifactId>
<version>SPCEX-1.0.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ru.spcex.clearing</groupId>
<artifactId>dictionary</artifactId>
<version>SPCEX-1.0.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ru.spcex.platform</groupId>
<artifactId>platform-imdg-api</artifactId>
</dependency>
<dependency>
<groupId>ru.spcex.platform</groupId>
<artifactId>platform-imdg-api-hazelcast-impl</artifactId>
</dependency>
<dependency>
<groupId>ru.spcex.clearing</groupId>
<artifactId>classes</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>ru.spcex.clearing</groupId>
<artifactId>test-clearing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!--<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-starter</artifactId>
<version>${keycloak-spring-boot-starter.version}</version>
</dependency>-->
<!-- TEST -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.properties</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>${project.artifactId}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,99 @@
package ru.spcex.clearing.test;
import com.hazelcast.config.MapStoreConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import ru.clearing.classes.objects.BusinessObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
@Service
public class ImdgService {
protected final Logger log= LoggerFactory.getLogger(getClass());
int thread=3;
HazelcastInstance hazelcastServerInstance;
Collection<String> allMaps() {
return null;
}
public int reloadMapFromDB() {
log.info("Reload all from DB...");
long loadTime = System.currentTimeMillis();
int count = 0;
try {
List<Callable<Long>> tasks = new ArrayList<>();
Collection<String> mapNames = hazelcastServerInstance.getConfig().getMapConfigs().keySet();
for (String mapName : mapNames) {
tasks.add(() -> {
Long maxKey = null;
MapStoreConfig mapStoreConfig = hazelcastServerInstance.getConfig().getMapConfig(mapName).getMapStoreConfig();
if (mapStoreConfig != null && mapStoreConfig.isEnabled()) {
long start = System.currentTimeMillis();
log.debug("evict map {}", mapName);
IMap<Long, BusinessObject> map = hazelcastServerInstance.getMap(mapName);
// todo проверить что ничего лишнего не попадет map.evictAll();
// log.debug("Load map {}", mapName);
// map.loadAll(false);
int size = map.size();
long time = System.currentTimeMillis() - start;
log.debug("{} {} rows reloaded in {}ms", mapName, size, time);
Object mapStore = mapStoreConfig.getImplementation();
//todo проверить реализацию.
// if (mapStore instanceof SimpleObjectMapStore) {
// String tableName = ((SimpleObjectMapStore) mapStore).getTableName();
// maxKey = map.keySet().stream().max(Long::compareTo).orElse(null); // jdbcTemplate.queryForObject("select max(id) from " + tableName, Long.class);
// } else if (mapStore instanceof DictionaryMapStore) {
// // для Dictionary не используется общий id генератор
//// } else if (mapStore instanceof FrontendUserSessionMapStore) {
//// // не используется общий id генератор
// } else {
// throw new RuntimeException("unknown map store implementation " + mapStore);
// }
log.debug("{} max(id)={}", mapName, maxKey);
}
return maxKey;
});
}
long maxKey = 0L;
int threadCount = Runtime.getRuntime().availableProcessors();// todo config * Config.get().getRoot().getSettings().getInitHazelcastThreadMultiplier();
log.info("Initializing threads count = {}", threadCount);
ExecutorService executor = Executors.newWorkStealingPool(threadCount);
try {
List<Future<Long>> results = executor.invokeAll(tasks);
for (Future<Long> result : results) {
Long maxKeyResult = result.get();
if (maxKeyResult != null) {
maxKey = Math.max(maxKey, maxKeyResult);
}
count++;
}
} finally {
executor.shutdown();
}
log.info("IDGenerator can not reinit. Max map ID {}", maxKey);
// plannerAllTodayMaker.makeSchedulerAllTodayMap();
} catch (InterruptedException | ExecutionException e) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw new RuntimeException("MapStore multithreaded reload not complete.", e);
}
// HazelcastHelper.imdgSystem_setStorageState(true, hazelcastServerInstance);
loadTime = System.currentTimeMillis() - loadTime;
log.info("All map reload time {} ms", loadTime);
return count;
}
}

View file

@ -0,0 +1,18 @@
package ru.spcex.clearing.test;
import org.springframework.stereotype.Service;
import java.util.Collection;
@Service
public class KafkaService {
Collection<String> allTopics() {
return null;
}
int putMessage(String topic, String json) {
//todo
return 0;
}
}

View file

@ -0,0 +1,27 @@
package ru.spcex.clearing.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@SpringBootApplication
public class TestApiClearingApplication {
public static void main(String[] args) {
// ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(SpringEnableComponentScanConfig.class);
// StartupInfo startupInfo = context.getBean(StartupInfo.class);
// startupInfo.logStart();
// try {
// ProcessorService processorService = context.getBean(ProcessorService.class);
// processorService.process();
// } finally {
// startupInfo.logEnd();
// context.close();
// }
//todo not spring boot, please
SpringApplication springApplication = new SpringApplication(TestApiClearingApplication.class);
springApplication.run(args);
}
}

View file

@ -0,0 +1,66 @@
package ru.spcex.clearing.test.config;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.core.HazelcastInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ru.spcex.clearing.test.config.settings.TestServiceSettings;
import ru.spcex.platform.imdg.api.ImdgProvider;
import ru.spcex.platform.imdg.iml.hazelcast.config.HazelcastClientParams;
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
import ru.spcex.platform.imdg.iml.hazelcast.util.HazelcastHelper;
@Configuration
public class DirectImdgConfig {
Logger log = LoggerFactory.getLogger(getClass());
// @Autowired
// @Bean
// public ImdgProvider imdgProvider(
// @Qualifier("taskExecutorHazelcastClientInitializer") ThreadPoolTaskExecutor taskExecutorHazelcastClientInitializer,
// @Qualifier("taskExecutorIdGeneratorAwaiter") ThreadPoolTaskExecutor taskExecutorIdGeneratorAwaiter,
// TestServiceSettings clientSetting
// ) {
// ImdgProvider imdg = new HazelcastService(taskExecutorHazelcastClientInitializer,
// taskExecutorIdGeneratorAwaiter,
// clientSetting.getHazelcast());
// return imdg;
// }
@Autowired
@Bean("imdgNative")
public HazelcastInstance imdgNativeService(TestServiceSettings settings) {
HazelcastClientParams settingsElementHazelcastClient = settings.getHazelcast();
// if (hazelcastInstance != null) {
// try {
// hazelcastInstance.shutdown();
// } catch (Throwable ignored) {
// }
// }
HazelcastInstance hazelcastInstance;
log.info("Hazelcast: client init");
ClientConfig clientConfig = HazelcastHelper.getClientConfig(
settingsElementHazelcastClient.getClusterMembers(),
settingsElementHazelcastClient.getLogin(),
settingsElementHazelcastClient.getPassword(),
settingsElementHazelcastClient.getInstanceName(),
settingsElementHazelcastClient.getNearCacheConfig()
);
// if (environment != null && Arrays.asList(environment.getActiveProfiles()).contains("tests")) {
// clientConfig.getNetworkConfig().setConnectionAttemptLimit(HazelcastHelper.TEST_CONNECTION_ATTEMPT_LIMIT);
// }
log.info("Hazelcast: client created, trying connect to server ({}) ...", settingsElementHazelcastClient.getClusterMembers());
hazelcastInstance = HazelcastClient.newHazelcastClient(clientConfig);
log.info("Hazelcast: client created and connected");
return hazelcastInstance;
}
}

View file

@ -0,0 +1,65 @@
package ru.spcex.clearing.test.config;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.producer.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.clearing.platform.messaging.config.KafkaConsumerFactory;
import ru.spcex.clearing.platform.messaging.config.KafkaProducerFactory;
import ru.spcex.clearing.platform.messaging.config.element.KafkaProducerSettings;
import ru.spcex.clearing.platform.messaging.service.RequestInfo;
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
import ru.spcex.clearing.test.config.settings.TestServiceSettings;
import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgId;
import ru.spcex.platform.imdg.api.ImdgProvider;
@Configuration
public class KafkaConfig {
@Autowired
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Consumer<String, Object> createConsumer(TestServiceSettings settings) {
return KafkaConsumerFactory.consumer(settings.getKafkaConsumer());
}
@Autowired
@Bean
public Producer<String, Object> createProducer(TestServiceSettings settings) {
return KafkaProducerFactory.producer(settings.getKafkaProducer());
}
@Bean
public ProducerFactory<String, Object> pf(TestServiceSettings settings) {
KafkaProducerSettings kafkaSettings = settings.getKafkaProducer();
return KafkaProducerFactory.producerFactory(kafkaSettings);
}
@Bean("kafkaTemplate")
public KafkaTemplate<String, Object> kafkaTemplate(ProducerFactory<String, Object> pf) {
return new KafkaTemplate<>(pf);
}
@Autowired
@Bean
public KafkaSender kafkaSender(@Qualifier("kafkaTemplate") KafkaTemplate<String, Object> kafkaTemplate,
ImdgProvider imdgProvider) {
ImdgId imdgIdGenerator = imdgProvider.getImdgIdGenerator();
return KafkaSender
.setup()
.setKafkaTemplate(kafkaTemplate)
.idGenerator(imdgIdGenerator::nextId)
.imdgProvider(s -> {
Imdg<RequestInfo> imdg = imdgProvider.getImdg(IMDGDistributedNames.Map_RequestInfo, RequestInfo.class);
return imdg::insert;
})
.build();
}
}

View file

@ -0,0 +1,39 @@
package ru.spcex.clearing.test.config;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SuppressWarnings("Guava")
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("clearing-backend-api")
.apiInfo(metadata())
.select()
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
.paths(PathSelectors.any())
.build()
.useDefaultResponseMessages(false);
}
private ApiInfo metadata() {
return new ApiInfoBuilder()
.title("Spcex Clearing service")
.description("Сервис клиринга")
.version("0.0.1")
.build();
}
}

View file

@ -0,0 +1,106 @@
package ru.spcex.clearing.test.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.server.Cookie;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.boot.web.servlet.server.CookieSameSiteSupplier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.servlet.config.annotation.*;
import ru.spcex.clearing.test.config.settings.TestServiceSettings;
import java.util.List;
import java.util.function.Consumer;
@SuppressWarnings("Duplicates")
@Configuration
@EnableWebMvc
//todo remove? see ClearingCorsFilter
@CrossOrigin
public class WebConfig implements WebMvcConfigurer {
private final MappingJackson2HttpMessageConverter customJsonHttpConverter;
private final String sameSite;
@Bean("customJsonHttpConverter")
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
return new MappingJackson2HttpMessageConverter(JsonUtil.JacksonObjectMapper.getMapper());
}
@Autowired
public WebConfig(@Qualifier("customJsonHttpConverter") MappingJackson2HttpMessageConverter customJsonHttpConverter,
TestServiceSettings backendSettings
) {
this.customJsonHttpConverter = customJsonHttpConverter;
sameSite = null; // this.sameSite = backendSettings.getSecurity().getSameSite();
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Autowired
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
Consumer<HttpMessageConverter<?>> addConverter = httpMessageConverter -> {
messageConverters.removeIf(registeredConverter -> registeredConverter.getClass().equals(httpMessageConverter.getClass()));
messageConverters.add(httpMessageConverter);
};
addConverter.accept(new StringHttpMessageConverter());
addConverter.accept(customJsonHttpConverter);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/v2/api-docs", "/v2/api-docs?group=api");
registry.addRedirectViewController("/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui");
registry.addRedirectViewController("/swagger-resources/configuration/security", "/swagger-resources/configuration/security");
registry.addRedirectViewController("/swagger-resources", "/swagger-resources");
registry.addRedirectViewController("", "/swagger-ui.html");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/swagger-ui.html**")
.addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
}
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> enableDefaultServlet() {
return (factory) -> factory.setRegisterDefaultServlet(true);
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*");
}
@Bean
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}
@Bean
public CookieSameSiteSupplier applicationCookieSameSiteSupplier() {
if (sameSite == null) {
return null;
} else if (sameSite.equalsIgnoreCase(Cookie.SameSite.NONE.attributeValue())) {
return CookieSameSiteSupplier.ofNone();
} else if (sameSite.equalsIgnoreCase(Cookie.SameSite.LAX.attributeValue())) {
return CookieSameSiteSupplier.ofLax();
} else if (sameSite.equalsIgnoreCase(Cookie.SameSite.STRICT.attributeValue())) {
return CookieSameSiteSupplier.ofStrict();
} else {
throw new IllegalStateException("unknown SameSite setting");
}
}
}

View file

@ -1,25 +1,25 @@
package ru.spcex.clearing.imdg.config.element; package ru.spcex.clearing.test.config.settings;
/** /**
* Debug config * Debug config
*/ */
public class ControllerSettings { public class ControllerSettings {
private String port; private String port;
private String contextPath; private String contextPath;
public String getPort() { public String getPort() {
return port; return port;
} }
public void setPort(String port) { public void setPort(String port) {
this.port = port; this.port = port;
} }
public String getContextPath() { public String getContextPath() {
return contextPath; return contextPath;
} }
public void setContextPath(String contextPath) { public void setContextPath(String contextPath) {
this.contextPath = contextPath; this.contextPath = contextPath;
} }
} }

View file

@ -0,0 +1,51 @@
package ru.spcex.clearing.test.config.settings;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import ru.spcex.clearing.platform.messaging.config.element.KafkaConsumerSettings;
import ru.spcex.clearing.platform.messaging.config.element.KafkaProducerSettings;
import ru.spcex.platform.imdg.iml.hazelcast.config.HazelcastClientParams;
@Component
@PropertySource("file:${spring.config.location}/application.properties")
@ConfigurationProperties("test-service")
public class TestServiceSettings {
private HazelcastClientParams hazelcast;
private KafkaConsumerSettings kafkaConsumer;
private KafkaProducerSettings kafkaProducer;
private ControllerSettings controllerSettings;//todo refactoring
public ControllerSettings getControllerSettings() {
return controllerSettings;
}
public void setControllerSettings(ControllerSettings controllerSettings) {
this.controllerSettings = controllerSettings;
}
public HazelcastClientParams getHazelcast() {
return hazelcast;
}
public void setHazelcast(HazelcastClientParams hazelcast) {
this.hazelcast = hazelcast;
}
public KafkaConsumerSettings getKafkaConsumer() {
return kafkaConsumer;
}
public void setKafkaConsumer(KafkaConsumerSettings kafkaConsumer) {
this.kafkaConsumer = kafkaConsumer;
}
public KafkaProducerSettings getKafkaProducer() {
return kafkaProducer;
}
public void setKafkaProducer(KafkaProducerSettings kafkaProducer) {
this.kafkaProducer = kafkaProducer;
}
}

View file

@ -0,0 +1,139 @@
package ru.spcex.clearing.test.controller;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import ru.spcex.clearing.test.ImdgService;
import ru.spcex.clearing.test.config.settings.ControllerSettings;
import ru.spcex.clearing.test.config.settings.TestServiceSettings;
import ru.spcex.platform.utils.log.ExceptionUtils;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.time.LocalDateTime;
@Service
public class Controller implements InitializingBean, DisposableBean {
protected final Logger log = LoggerFactory.getLogger(getClass());
HttpServer server;
final ControllerSettings settings;
final ImdgService imdgService;
@Autowired
public Controller(ImdgService imdgService, TestServiceSettings settings) {
this.imdgService = imdgService;
this.settings = settings.getControllerSettings();
}
@Override
public void afterPropertiesSet() throws Exception {
Integer port = null; // 8701
try {
if (settings == null || StringUtils.isEmpty(settings.getPort())) {
log.debug("Debug HTTP port not set, do not init HTTP controller service.");
return;
}
port = Integer.parseInt(settings.getPort().trim());
String url = settings.getContextPath().trim();
log.info("Controller for debug reload star at port {} with url \"{}\"", port, url);
this.server = HttpServer.create(new InetSocketAddress(port), 0);
IndexHandler index=new IndexHandler();
server.createContext(url, index);
server.createContext(url+"/index.html", index);
server.createContext(url+"/imdg", new ImdgHandler());
server.createContext(url+"/kafka", new KafkaHandler());
server.setExecutor(null); // default
server.start();
} catch (Throwable t) {
log.error("Can not start debug HTTP server in port {}: {}", port, ExceptionUtils.getStackTrace(t));
}
}
@Override
public void destroy() throws Exception {
if (server != null) {
server.stop(100);
log.info("Controller for reload stop.");
}
}
class IndexHandler extends HtmlHandler {
@Override
public void makePage(HttpExchange t, OutputStream os) throws IOException {
writeLine(os, "Welcome!</br>");
writeLine(os, " <a href=\"imdg\">Reload IMDG.</a> ");
writeLine(os, " <a href=\"kafka\">Send to kafka.</a> ");
}
}
class KafkaHandler extends HtmlHandler {
@Override
public void makePage(HttpExchange t, OutputStream os) throws IOException {
writeLine(os, "Welcome kafka send!</br>");
byte[] jsonB = t.getRequestBody().readAllBytes();
//t.getRequestHeaders().getFirst()
String json=new String(jsonB, "windows-1251");
//todo ...
writeLine(os, " <a href=\"imdg\">Reload IMDG.</a> ");
writeLine(os, " <a href=\"kafka\">Send to kafka.</a> ");
}
}
class ImdgHandler extends HtmlHandler {
@Override
public void makePage(HttpExchange t, OutputStream os) throws IOException {
writeLine(os, "Wait, reload all maps from DB... </br>");
synchronized (this) {
os.flush();
try {
long clock = System.currentTimeMillis();
int count = imdgService.reloadMapFromDB();
clock = System.currentTimeMillis() - clock;
writeLine(os, count + " map per " + clock + " ms</br>");
} catch (Throwable e) {
String msg = "Error reload: " + ExceptionUtils.getStackTrace(e);
log.error(msg);
writeLine(os, msg);
}
}
writeLine(os, "<b title=\":)\">Done.</b> " + LocalDateTime.now() + "</br>");
writeLine(os, " <a href=\"\">Reload again.</a> ");
}
}
abstract class HtmlHandler implements HttpHandler {
public abstract void makePage(HttpExchange t, OutputStream os) throws IOException;
@Override
public void handle(HttpExchange t) throws IOException {
log.info("{} request by user \"{}\"", getClass().getSimpleName(), t.getRemoteAddress().getAddress());
t.sendResponseHeaders(200, 0);
t.setAttribute("Content-Type", "text/html; charset=windows-1251"); // or "text/plain или text/html; charset=windows-1251"
try (OutputStream os = t.getResponseBody()) {
writeLine(os, "<!DOCTYPE html>\n<html><head></head>\n<body style=\"background-color:lightgray; font-size:14pt;color:black;\">");
writeLine(os, "<div>");
makePage(t, os);
writeLine(os, "</div>");
writeLine(os, "</body></html>");
}
log.trace("HTTP Request done.");
}
void writeLine(OutputStream os, String text) throws IOException {
if (text != null)
os.write(text.getBytes("windows-1251"));
os.write("\n".getBytes("windows-1251"));
}
}
}

View file

@ -1,112 +1,111 @@
package ru.spcex.clearing.imdg.services.controller; package ru.spcex.clearing.test.controller;
import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpServer;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.spcex.clearing.imdg.config.element.ControllerSettings; import ru.spcex.clearing.test.ImdgService;
import ru.spcex.clearing.imdg.config.element.ImdgSettings; import ru.spcex.clearing.test.config.settings.ControllerSettings;
import ru.spcex.clearing.imdg.services.AbstractHazelcastLifecycleSupport; import ru.spcex.clearing.test.config.settings.TestServiceSettings;
import ru.spcex.platform.utils.log.ExceptionUtils; import ru.spcex.platform.utils.log.ExceptionUtils;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets; import java.time.LocalDateTime;
import java.time.LocalDateTime;
@Service
@Service public class ImdgController implements InitializingBean, DisposableBean {
public class ImdgController implements InitializingBean, DisposableBean { protected final Logger log = LoggerFactory.getLogger(getClass());
protected final Logger log = LoggerFactory.getLogger(getClass()); protected HttpServer server;
HttpServer server;
protected final ControllerSettings settings;
final ControllerSettings settings; protected final ImdgService imdgService;
final AbstractHazelcastLifecycleSupport imdgService;
@Autowired
@Autowired public ImdgController(ImdgService imdgService, TestServiceSettings settings) {
public ImdgController(AbstractHazelcastLifecycleSupport imdgService, ImdgSettings settings) { this.imdgService = imdgService;
this.imdgService = imdgService; this.settings = settings.getControllerSettings();
this.settings = settings.getDebugServer(); }
}
@Override
@Override public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() throws Exception { Integer port = null; // 8701
Integer port = null; // 8701 try {
try { if (settings == null || StringUtils.isEmpty(settings.getPort())) {
if (settings == null || StringUtils.isEmpty(settings.getPort())) { log.debug("Debug HTTP port not set, do not init HTTP controller service.");
log.debug("Debug HTTP port not set, do not init HTTP controller service."); return;
return; }
} port = Integer.parseInt(settings.getPort().trim());
port = Integer.parseInt(settings.getPort().trim()); String url = StringUtils.isBlank(settings.getContextPath()) ? "/imdg" : settings.getContextPath().trim();
String url = StringUtils.isBlank(settings.getContextPath()) ? "/imdg" : settings.getContextPath().trim(); log.info("Controller for debug reload star at port {} with url \"{}\"", port, url);
log.info("Controller for debug reload star at port {} with url \"{}\"", port, url); this.server = HttpServer.create(new InetSocketAddress(port), 0);
this.server = HttpServer.create(new InetSocketAddress(port), 0); server.createContext(url, new MyHandler());
server.createContext(url, new MyHandler()); server.setExecutor(null); // default
server.setExecutor(null); // default server.start();
server.start(); } catch (Throwable t) {
} catch (Throwable t) { log.error("Can not start debug HTTP server in port {}: {}", port, ExceptionUtils.getStackTrace(t));
log.error("Can not start debug HTTP server in port {}: {}", port, ExceptionUtils.getStackTrace(t)); }
} }
}
@Override
@Override public void destroy() throws Exception {
public void destroy() throws Exception { if (server != null) {
if (server != null) { server.stop(100);
server.stop(100); log.info("Controller for reload stop.");
log.info("Controller for reload stop."); }
} }
}
class MyHandler implements HttpHandler {
class MyHandler implements HttpHandler { @Override
@Override public void handle(HttpExchange t) throws IOException {
public void handle(HttpExchange t) throws IOException { log.info("Reload request by user \"{}\"", t.getRemoteAddress().getAddress());
log.info("Reload request by user \"{}\"", t.getRemoteAddress().getAddress()); t.sendResponseHeaders(200, 0);
t.sendResponseHeaders(200, 0); t.setAttribute("Content-Type", "text/html; charset=windows-1251"); // or "text/plain или text/html; charset=windows-1251"
t.setAttribute("Content-Type", "text/html; charset=windows-1251"); // or "text/plain или text/html; charset=windows-1251" try (OutputStream os = t.getResponseBody()) {
try (OutputStream os = t.getResponseBody()) { writeHead(os);
writeHead(os); writeLine(os, "Wait, reload all maps from DB... </br>");
writeLine(os, "Wait, reload all maps from DB... </br>"); synchronized (ImdgController.this) {
synchronized (ImdgController.this) { os.flush();
os.flush(); try {
try { long clock = System.currentTimeMillis();
long clock = System.currentTimeMillis(); int count = imdgService.reloadMapFromDB();
int count = imdgService.reloadMapFromDB(); clock = System.currentTimeMillis() - clock;
clock = System.currentTimeMillis() - clock; writeLine(os, count + " map per " + clock + " ms</br>");
writeLine(os, count + " map per " + clock + " ms</br>"); } catch (Throwable e) {
} catch (Throwable e) { String msg = "Error reload: " + ExceptionUtils.getStackTrace(e);
String msg = "Error reload: " + ExceptionUtils.getStackTrace(e); log.error(msg);
log.error(msg); writeLine(os, msg);
writeLine(os, msg); }
} }
} writeLine(os, "<b title=\":)\">Done.</b> " + LocalDateTime.now() + "</br>");
writeLine(os, "<b title=\":)\">Done.</b> " + LocalDateTime.now() + "</br>"); writeLine(os, " <a href=\"\">Reload again.</a> ");
writeLine(os, " <a href=\"\">Reload again.</a> "); writeEnd(os);
writeEnd(os); } //os.close();
} //os.close(); log.trace("HTTP Request done.");
log.trace("HTTP Request done."); }
}
void writeHead(OutputStream os) throws IOException {
void writeHead(OutputStream os) throws IOException { writeLine(os, "<!DOCTYPE html>\n<html><head></head>\n<body style=\"background-color:lightgray; font-size:14pt;color:black;\">");
writeLine(os, "<!DOCTYPE html>\n<html><head></head>\n<body style=\"background-color:lightgray; font-size:14pt;color:black;\">"); writeLine(os, "<div>");
writeLine(os, "<div>"); }
}
void writeLine(OutputStream os, String text) throws IOException {
void writeLine(OutputStream os, String text) throws IOException { if (text != null)
if (text != null) os.write(text.getBytes("windows-1251"));
os.write(text.getBytes("windows-1251")); os.write("\n".getBytes("windows-1251"));
os.write("\n".getBytes("windows-1251")); }
}
void writeEnd(OutputStream os) throws IOException {
void writeEnd(OutputStream os) throws IOException { writeLine(os, "</div>");
writeLine(os, "</div>"); writeLine(os, "</body></html>");
writeLine(os, "</body></html>"); }
} }
} }
}

View file

@ -1,14 +1,13 @@
package ru.spcex.clearing.backendapi.controller.test; package ru.spcex.clearing.test.controller.kafka;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRawValue; import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import ru.spcex.clearing.backendapi.domain.actions.IAction;
import ru.spcex.clearing.platform.messaging.domain.ActionType; import ru.spcex.clearing.platform.messaging.domain.ActionType;
import ru.spcex.clearing.platform.messaging.domain.cud.registry.TradingClearingRegistryNewRequest; import ru.spcex.clearing.platform.messaging.domain.cud.registry.TradingClearingRegistryNewRequest;
public class AnyKafkaMessageAction implements IAction<TradingClearingRegistryNewRequest> { public class AnyKafkaMessageAction /*implements IAction<TradingClearingRegistryNewRequest>*/ {
@ApiModelProperty(value = "полное имя класса payload для BaseRequest", example = "ru.spcex.clearing.platform.messaging.domain.cud.schedule.LauncherCommandRequest") @ApiModelProperty(value = "полное имя класса payload для BaseRequest", example = "ru.spcex.clearing.platform.messaging.domain.cud.schedule.LauncherCommandRequest")
@JsonProperty @JsonProperty
private String fullClassName; private String fullClassName;
@ -22,14 +21,14 @@ public class AnyKafkaMessageAction implements IAction<TradingClearingRegistryNew
this.json = json.toString(); this.json = json.toString();
} }
@Override // @Override
public TradingClearingRegistryNewRequest toRequest() { public TradingClearingRegistryNewRequest toRequest() {
var req = new TradingClearingRegistryNewRequest(); var req = new TradingClearingRegistryNewRequest();
return req; return req;
} }
@ApiModelProperty(hidden = true) @ApiModelProperty(hidden = true)
@Override // @Override
public ActionType getActionType() { public ActionType getActionType() {
return ActionType.SYSTEM; return ActionType.SYSTEM;
} }

View file

@ -1,4 +1,4 @@
package ru.spcex.clearing.backendapi.controller.test; package ru.spcex.clearing.test.controller.kafka;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
@ -17,9 +17,9 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import ru.spcex.clearing.backendapi.errors.BackEndError;
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender; import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
import ru.spcex.platform.utils.enumeration.EnumMessage; import ru.spcex.platform.utils.enumeration.EnumMessage;
import ru.spcex.platform.utils.enumeration.IErrorEnumId;
import ru.spcex.platform.utils.error.ValidationException; import ru.spcex.platform.utils.error.ValidationException;
import ru.spcex.platform.utils.text.TextUtil; import ru.spcex.platform.utils.text.TextUtil;
@ -32,6 +32,26 @@ public class KafkaApiController {
static { static {
} }
enum BackEndError implements IErrorEnumId {
ValidationError(9000L),
UnknownJsonProperty(9001L),
FailedToReadHttpMessage(9002L),
KeycloakRepeatedRoles(9003L),
DictionaryNotFound(9004L),
ResourceNotFound(9005L)
;
private final Long id;
BackEndError(Long id) {
this.id = id;
}
@Override
public Long getId() {
return id;
}
}
@Autowired @Autowired
public KafkaApiController(KafkaSender kafkaSender) { public KafkaApiController(KafkaSender kafkaSender) {
this.kafkaSender = kafkaSender; this.kafkaSender = kafkaSender;

View file

@ -0,0 +1,31 @@
#spring.main.web-application-type=none
test-service.port=8701
test-service.url=/clearing/test/
#debug tester mode:
test-service.debug-server.port=8701
test-service.debug-server.context-path=/imdg/reload
server.port=8070
server.servlet.context-path=/backend-api-test
test-service.hazelcast.cluster-members=127.0.0.1:5701
test-service.hazelcast.login=dev
test-service.hazelcast.password=dev-pass
#test-service.kafka-consumer.bootstrap-servers=localhost:9092
#test-service.kafka-consumer.group-id=dev-group-utility-service
#test-service.kafka-consumer.enable-auto-commit=true
#test-service.kafka-consumer.session-timeout-ms=30000
#test-service.kafka-consumer.auto-offset-reset=latest
#test-service.kafka-consumer.linger-ms=1
#test-service.kafka-consumer.buffer-memory=33554432
test-service.kafka-producer.bootstrap-servers=localhost:9092
test-service.kafka-producer.acks=all
test-service.kafka-producer.retries=0
test-service.kafka-producer.batch-size=16384
test-service.kafka-producer.linger-ms=1
test-service.kafka-producer.buffer-memory=33554432

View file

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!-- |%X{ru.nbch.scoring.web.logging.mdc_key}-->
<Pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %class{0}:%line - %message%n</Pattern>
<charset>utf-8</charset>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./logs/utility-service.log</file>
<encoder>
<!-- |%X{ru.nbch.scoring.web.logging.mdc_key}-->
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %class{0}:%msg%n</Pattern>
<charset>utf8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>
./logs/utility-service.%i.log
</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>500MB</maxFileSize>
</triggeringPolicy>
</appender>
<root level="warn">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
<logger name="ru.spcex" level="debug" additivity="false">
<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE"/>
</logger>
</configuration>