using completableFutures to froce the Rest controller immediately repliying with 200 ok http response after calling service-method.
Created
June 6, 2018 08:16
-
-
Save smathys/4c22a963b5a701b0e8b8c8963c78995a to your computer and use it in GitHub Desktop.
Rest Controller with Service-Call using CompletableFuture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.LoggerFactory | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.http.HttpStatus | |
import org.springframework.http.ResponseEntity | |
import org.springframework.web.bind.annotation.RequestBody | |
import org.springframework.web.bind.annotation.RequestMapping | |
import org.springframework.web.bind.annotation.RequestMethod | |
import org.springframework.web.bind.annotation.RestController | |
import java.time.Instant | |
import java.util.Objects.requireNonNull | |
import javax.validation.Valid | |
@RestController | |
class ImporterController @Autowired | |
constructor(private val fileTransformer : FileTransformer) { | |
companion object { | |
private val LOGGER = LoggerFactory.getLogger(ImporterController::class.java) | |
} | |
@RequestMapping(value = ["/gtfs/import"], method = [RequestMethod.POST]) | |
fun importResource(@Valid @RequestBody importData : ImportData) : ResponseEntity<*> { | |
LOGGER.info("Initiating import from: $importData.getResource() at: ${Instant.now()}") | |
requireNonNull(importData) | |
fileTransformer.processImport(importData) | |
return ResponseEntity<String>("ok", HttpStatus.CREATED) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.vavr.Tuple; | |
import io.vavr.Tuple2; | |
import io.vavr.Tuple3; | |
import io.vavr.collection.*; | |
import io.vavr.control.Try; | |
import org.apache.commons.csv.CSVFormat; | |
import org.apache.commons.csv.CSVRecord; | |
import org.apache.commons.io.FileUtils; | |
import org.apache.commons.lang3.StringUtils; | |
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.core.io.Resource; | |
import org.springframework.core.io.ResourceLoader; | |
import org.springframework.stereotype.Service; | |
import java.io.*; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.ExecutorService; | |
import java.util.function.Function; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipFile; | |
import static com.siemens.justgo.dinoimporter.domain.callback.CallbackResult.failure; | |
import static com.siemens.justgo.dinoimporter.domain.callback.CallbackResult.success; | |
@Service | |
public class FileTransformer { | |
private static final Logger LOG = LoggerFactory.getLogger(FileTransformer.class); | |
private static final String FILE_ENCODING = "ISO-8859-1"; | |
private final ResourceLoader resourceLoader; | |
private final CallbackService callbackService; | |
private final ExecutorService executor; | |
@Autowired | |
public FileTransformer(ServiceInfrastructureController serviceInfrastructureController, | |
ResourceLoader resourceLoader, | |
CallbackService callbackService, ExecutorService executor) { | |
this.serviceInfrastructureController = serviceInfrastructureController; | |
this.resourceLoader = resourceLoader; | |
this.callbackService = callbackService; | |
this.executor = executor; | |
} | |
public void processImport(ImportData importData) { | |
CompletableFuture.supplyAsync( | |
() -> getFile(importData.getResource()), executor).thenAcceptAsync(file -> parseFile(importData, file)); | |
} | |
private File getFile(String resource) { | |
File theFile; | |
try { | |
LOG.info("Provided resource is remote, copying locally"); | |
Resource zipFilePath = resourceLoader.getResource(resource); | |
theFile = File.createTempFile(zipFilePath.getFilename(), ""); | |
FileUtils.copyURLToFile(zipFilePath.getURL(), theFile); | |
return theFile; | |
} catch (IOException e) { | |
throw new IllegalArgumentException("Error opening GTFS file:" + resource); | |
} | |
} | |
private void parseFile(ImportData importData, File theFile) { | |
try (ZipFile zipFile = new ZipFile(theFile)) { | |
LOG.debug("Reading zip file: {}", theFile.toString()); | |
RawFeed rawFeed = parseZipFile(zipFile); | |
List<String> parsingErrors = rawFeed.getParsingErrors(); | |
//do something with the feed | |
} catch (Exception e) { | |
LOG.error("Exception while importing gtfs file {} for {}", | |
importData.getResource(), importData.getImportId(), e); | |
postFailureToCallBack(importData, e); | |
} | |
LOG.info("Done parsing file"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment