@@ -63,7 +63,8 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
|||||||
|
|
||||||
String yandexReverseSearchResponse;
|
String yandexReverseSearchResponse;
|
||||||
try {
|
try {
|
||||||
yandexReverseSearchResponse = yandexSearchService.searchReverseByPublicUrlWithTimeout(fileEntity, 20);
|
yandexReverseSearchResponse = yandexSearchService.searchReverseByPublicUrlWithTimeout(fileEntity,
|
||||||
|
20);
|
||||||
} catch (TimeoutException e) {
|
} catch (TimeoutException e) {
|
||||||
log.warn("Search timeout for file {}, returning empty results", fileId);
|
log.warn("Search timeout for file {}, returning empty results", fileId);
|
||||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||||
|
|||||||
@@ -209,7 +209,6 @@ public class YandexSearchService {
|
|||||||
|
|
||||||
public String searchReverseByPublicUrlWithTimeout(FileEntity fileEntity, int timeoutSeconds)
|
public String searchReverseByPublicUrlWithTimeout(FileEntity fileEntity, int timeoutSeconds)
|
||||||
throws IOException, TimeoutException {
|
throws IOException, TimeoutException {
|
||||||
|
|
||||||
if (!isImageFile(fileEntity)) {
|
if (!isImageFile(fileEntity)) {
|
||||||
log.error("File not image: {}", fileEntity.getMimeType());
|
log.error("File not image: {}", fileEntity.getMimeType());
|
||||||
throw new NotValidFieldException("File not image", new BaseResponse(20007,
|
throw new NotValidFieldException("File not image", new BaseResponse(20007,
|
||||||
@@ -220,16 +219,13 @@ public class YandexSearchService {
|
|||||||
String publicUrl = String.format("%s/api/files/public/%s", appBaseUrl, fileEntity.getId());
|
String publicUrl = String.format("%s/api/files/public/%s", appBaseUrl, fileEntity.getId());
|
||||||
log.info("Searching reverse for image: {}", publicUrl);
|
log.info("Searching reverse for image: {}", publicUrl);
|
||||||
|
|
||||||
// Создаем ExecutorService с одним потоком
|
|
||||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Запускаем задачу с таймаутом
|
|
||||||
Future<String> future = executor.submit(() ->
|
Future<String> future = executor.submit(() ->
|
||||||
callReverseImageApiByUrlWithTimeout(publicUrl, timeoutSeconds)
|
callReverseImageApiByUrlWithTimeout(publicUrl, timeoutSeconds)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Получаем результат с таймаутом
|
|
||||||
return future.get(timeoutSeconds, TimeUnit.SECONDS);
|
return future.get(timeoutSeconds, TimeUnit.SECONDS);
|
||||||
|
|
||||||
} catch (TimeoutException e) {
|
} catch (TimeoutException e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user