config executor some changes
This commit is contained in:
parent
bfa32f453a
commit
702920da9f
6 changed files with 108 additions and 98 deletions
|
|
@ -1,14 +1,15 @@
|
|||
package org.micord.controller;
|
||||
|
||||
import org.micord.exceptions.FileNotModifiedException;
|
||||
import org.micord.service.ApiService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.micord.service.ApiService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* REST Controller for API operations.
|
||||
|
|
@ -21,27 +22,30 @@ public class ApiController {
|
|||
private ApiService apiService;
|
||||
|
||||
@PostMapping("/block")
|
||||
public CompletableFuture<ResponseEntity<?>> block(@RequestBody List<String> ids) throws FileNotFoundException, FileNotModifiedException {
|
||||
return apiService.process("block", ids)
|
||||
.thenApply(ResponseEntity::ok);
|
||||
public ResponseEntity<?> block(@RequestBody List<String> ids) throws FileNotFoundException {
|
||||
apiService.process("block", ids);
|
||||
return ResponseEntity.ok("");
|
||||
}
|
||||
|
||||
@PostMapping("/unblock")
|
||||
public CompletableFuture<ResponseEntity<?>> unblock(@RequestBody List<String> ids) throws FileNotFoundException, FileNotModifiedException {
|
||||
return apiService.process("unblock", ids)
|
||||
.thenApply(ResponseEntity::ok);
|
||||
public ResponseEntity<?> unblock(@RequestBody List<String> ids) throws FileNotFoundException {
|
||||
|
||||
apiService.process("unblock", ids);
|
||||
return ResponseEntity.ok("");
|
||||
}
|
||||
|
||||
@PostMapping("/removeFromSystem")
|
||||
public CompletableFuture<ResponseEntity<?>> removeFromSystem(@RequestBody List<String> ids) throws FileNotFoundException, FileNotModifiedException {
|
||||
return apiService.process("removeFromSystem", ids)
|
||||
.thenApply(ResponseEntity::ok);
|
||||
public ResponseEntity<?> removeFromSystem(@RequestBody List<String> ids)
|
||||
throws FileNotFoundException {
|
||||
|
||||
apiService.process("removeFromSystem", ids);
|
||||
return ResponseEntity.ok("");
|
||||
}
|
||||
|
||||
@PostMapping("/removeFromCallList")
|
||||
public CompletableFuture<ResponseEntity<?>> removeFromCallList(@RequestBody List<String> ids) throws FileNotFoundException, FileNotModifiedException {
|
||||
return apiService.process("removeFromCallList", ids)
|
||||
.thenApply(ResponseEntity::ok);
|
||||
public ResponseEntity<?> removeFromCallList(@RequestBody List<String> ids)
|
||||
throws FileNotFoundException {
|
||||
apiService.process("removeFromCallList", ids);
|
||||
return ResponseEntity.ok("");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
package org.micord.exceptions;
|
||||
|
||||
/**
|
||||
* @author Maksim Tereshin
|
||||
*/
|
||||
public class FileNotModifiedException extends Exception {
|
||||
public FileNotModifiedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package org.micord.models;
|
||||
|
||||
import java.nio.file.attribute.FileTime;
|
||||
|
||||
public class CachedConfig {
|
||||
private final Requests config;
|
||||
private final FileTime modifiedTime;
|
||||
|
||||
public CachedConfig(Requests config, FileTime modifiedTime) {
|
||||
this.config = config;
|
||||
this.modifiedTime = modifiedTime;
|
||||
}
|
||||
|
||||
public Requests getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public FileTime getModifiedTime() {
|
||||
return modifiedTime;
|
||||
}
|
||||
}
|
||||
|
|
@ -16,26 +16,16 @@ public class ApiService {
|
|||
@Autowired
|
||||
private ConfigLoader configLoader;
|
||||
|
||||
@Autowired
|
||||
private S3Service s3Service;
|
||||
|
||||
@Autowired
|
||||
private SqlAqlService sqlAndAqlService;
|
||||
|
||||
public CompletableFuture<String> process(String methodName, List<String> ids) throws FileNotFoundException, FileNotModifiedException {
|
||||
public void process(String methodName, List<String> ids) throws FileNotFoundException {
|
||||
Optional<Requests> optionalConfig = configLoader.loadConfigIfModified(methodName);
|
||||
|
||||
if (optionalConfig.isEmpty()) {
|
||||
throw new FileNotFoundException("Configuration for method " + methodName + " could not be loaded.");
|
||||
}
|
||||
|
||||
Requests config = optionalConfig.get();
|
||||
|
||||
sqlAndAqlService.processSqlAndAqlRequests(config, ids);
|
||||
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
s3Service.processS3Requests(config.getS3Requests(), ids);
|
||||
}).thenApply(v -> "Processing complete");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -15,6 +15,7 @@ import org.micord.models.Requests;
|
|||
import org.micord.models.SqlRequest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
|
@ -35,6 +36,9 @@ public class SqlAqlService {
|
|||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SqlAqlService.class);
|
||||
|
||||
@Autowired
|
||||
private S3Service s3Service;
|
||||
|
||||
@Transactional
|
||||
public void processSqlAndAqlRequests(Requests config, List<String> ids) {
|
||||
if (config.getSqlRequests() != null) {
|
||||
|
|
@ -48,6 +52,7 @@ public class SqlAqlService {
|
|||
processAqlRequests(request, ids);
|
||||
}
|
||||
}
|
||||
s3Service.processS3Requests(config.getS3Requests(), ids);
|
||||
}
|
||||
|
||||
private void processSqlRequests(SqlRequest request, List<String> ids) {
|
||||
|
|
|
|||
|
|
@ -1,23 +1,23 @@
|
|||
package org.micord.utils;
|
||||
|
||||
import org.micord.exceptions.FileNotModifiedException;
|
||||
import org.micord.models.Requests;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
|
||||
import org.micord.models.CachedConfig;
|
||||
import org.micord.models.Requests;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Maksim Tereshin
|
||||
|
|
@ -26,13 +26,14 @@ import java.util.logging.Logger;
|
|||
public class ConfigLoader {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(ConfigLoader.class.getName());
|
||||
private final Map<String, FileTime> lastModifiedTimes = new HashMap<>();
|
||||
private static final Map<String, CachedConfig> cachedConfigs = new ConcurrentHashMap<>();
|
||||
|
||||
@Value("${configDirectory}")
|
||||
private String configDirectory;
|
||||
|
||||
public Optional<Requests> loadConfigIfModified(String methodName) throws FileNotModifiedException {
|
||||
public Optional<Requests> loadConfigIfModified(String methodName) {
|
||||
String fileName = methodName + ".xml";
|
||||
|
||||
if (configDirectory == null) {
|
||||
LOGGER.log(Level.SEVERE, "No configuration directory found for method: " + methodName);
|
||||
return Optional.empty();
|
||||
|
|
@ -41,30 +42,29 @@ public class ConfigLoader {
|
|||
try {
|
||||
File configFile = new File(configDirectory + File.separator + fileName);
|
||||
Path configFilePath = configFile.toPath();
|
||||
|
||||
FileTime currentModifiedTime = Files.getLastModifiedTime(configFilePath);
|
||||
FileTime lastModifiedTime = lastModifiedTimes.getOrDefault(methodName, null);
|
||||
|
||||
if (lastModifiedTime == null || currentModifiedTime.compareTo(lastModifiedTime) > 0) {
|
||||
lastModifiedTimes.put(methodName, currentModifiedTime);
|
||||
CachedConfig cachedConfig = cachedConfigs.getOrDefault(methodName, null);
|
||||
|
||||
if (cachedConfig == null || !currentModifiedTime.equals(cachedConfig.getModifiedTime())) {
|
||||
// Load the updated configuration
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(Requests.class);
|
||||
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
||||
Requests loadedConfig = (Requests) unmarshaller.unmarshal(configFile);
|
||||
|
||||
cachedConfigs.put(methodName, new CachedConfig(loadedConfig, currentModifiedTime));
|
||||
return Optional.of(loadedConfig);
|
||||
} else {
|
||||
throw new FileNotModifiedException("Configuration file for method " + methodName + " has not been modified.");
|
||||
}
|
||||
else {
|
||||
return Optional.of(cachedConfigs.get(methodName).getConfig());
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Failed to load configuration file: " + fileName, e);
|
||||
return Optional.empty(); // Return empty if there is an IO error
|
||||
} catch (JAXBException e) {
|
||||
}
|
||||
catch (JAXBException e) {
|
||||
LOGGER.log(Level.SEVERE, "Failed to unmarshal configuration file: " + fileName, e);
|
||||
return Optional.empty(); // Return empty if unmarshalling fails
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue