forked from lsmyrnaios/UrlsController
Add thread-safety when reading the bulkImportReport-files.
This commit is contained in:
parent
846c53913f
commit
903c3e1ffc
|
@ -224,6 +224,8 @@ public class BulkImportController {
|
||||||
// Write the contents of the report-file to a string (efficiently!) and return the whole content as an HTTP-response.
|
// Write the contents of the report-file to a string (efficiently!) and return the whole content as an HTTP-response.
|
||||||
final StringBuilder stringBuilder = new StringBuilder(25_000);
|
final StringBuilder stringBuilder = new StringBuilder(25_000);
|
||||||
String line;
|
String line;
|
||||||
|
|
||||||
|
FileUtils.fileAccessLock.lock();
|
||||||
try ( BufferedReader in = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(this.bulkImportReportLocation, bulkImportReportId + ".json"))), FileUtils.twentyFiveKb) ) {
|
try ( BufferedReader in = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(this.bulkImportReportLocation, bulkImportReportId + ".json"))), FileUtils.twentyFiveKb) ) {
|
||||||
while ( (line = in.readLine()) != null )
|
while ( (line = in.readLine()) != null )
|
||||||
stringBuilder.append(line).append("\n"); // The "readLine()" does not return the line-term char.
|
stringBuilder.append(line).append("\n"); // The "readLine()" does not return the line-term char.
|
||||||
|
@ -234,6 +236,8 @@ public class BulkImportController {
|
||||||
String errorMsg = "Failed to read the contents of report-file with ID: " + bulkImportReportId;
|
String errorMsg = "Failed to read the contents of report-file with ID: " + bulkImportReportId;
|
||||||
logger.error(errorMsg, e);
|
logger.error(errorMsg, e);
|
||||||
return ResponseEntity.internalServerError().body(errorMsg); // It's ok to give the file-path to the user, since the report already contains the file-path.
|
return ResponseEntity.internalServerError().body(errorMsg); // It's ok to give the file-path to the user, since the report already contains the file-path.
|
||||||
|
} finally {
|
||||||
|
FileUtils.fileAccessLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
String json = stringBuilder.toString().trim();
|
String json = stringBuilder.toString().trim();
|
||||||
|
|
|
@ -698,12 +698,12 @@ public class FileUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static final Lock fileWriteLock = new ReentrantLock(true);
|
public static final Lock fileAccessLock = new ReentrantLock(true);
|
||||||
|
|
||||||
public String writeToFile(String fileFullPath, String stringToWrite, boolean shouldLockThreads)
|
public String writeToFile(String fileFullPath, String stringToWrite, boolean shouldLockThreads)
|
||||||
{
|
{
|
||||||
if ( shouldLockThreads ) // In case multiple threads write to the same file. for ex. during the bulk-import procedure.
|
if ( shouldLockThreads ) // In case multiple threads write to the same file. for ex. during the bulk-import procedure.
|
||||||
fileWriteLock.lock();
|
fileAccessLock.lock();
|
||||||
|
|
||||||
try ( BufferedWriter bufferedWriter = new BufferedWriter(Files.newBufferedWriter(Paths.get(fileFullPath)), halfMb) )
|
try ( BufferedWriter bufferedWriter = new BufferedWriter(Files.newBufferedWriter(Paths.get(fileFullPath)), halfMb) )
|
||||||
{
|
{
|
||||||
|
@ -714,7 +714,7 @@ public class FileUtils {
|
||||||
return errorMsg;
|
return errorMsg;
|
||||||
} finally {
|
} finally {
|
||||||
if ( shouldLockThreads )
|
if ( shouldLockThreads )
|
||||||
fileWriteLock.unlock();
|
fileAccessLock.unlock();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue