Skip to content

Commit

Permalink
Make AsyncRequestRepository a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
f-galland committed Oct 10, 2024
1 parent b92006e commit 8cd0eec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,24 @@
import java.util.concurrent.TimeUnit;

public class AsyncRequestRepository implements Closeable {

private static AsyncRequestRepository instance;
private static final Logger logger = LogManager.getLogger(AsyncRequestRepository.class);
private final HttpHost target;
private final String requestUri;
private CloseableHttpAsyncClient client;

public AsyncRequestRepository(ConfigReader configReader) throws Exception {
private AsyncRequestRepository(ConfigReader configReader) throws Exception {
this.target = new HttpHost(configReader.getHostName(), configReader.getPort());
this.requestUri = configReader.getPath();
}

public static AsyncRequestRepository getInstance(ConfigReader configReader) throws Exception {
if (instance == null) {
instance = new AsyncRequestRepository(configReader);
}
return instance;
}

public Future<SimpleHttpResponse> performAsyncRequest() throws Exception {
logger.info("Preparing Async Request");
IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

public class JobScheduler implements LifecycleComponent {
public class JobScheduler {

private static final Logger logger = LogManager.getLogger(JobScheduler.class);
private final ConfigReader configReader;
Expand All @@ -30,7 +30,7 @@ private void start(ThreadPool threadPool) {
ExecutorService executorService = threadPool.executor(ThreadPool.Names.GENERIC);
Future<SimpleHttpResponse> future = AccessController.doPrivileged(
(PrivilegedAction<Future<SimpleHttpResponse>>) () -> {
try (AsyncRequestRepository asyncRequestRepository = new AsyncRequestRepository(configReader)){
try (AsyncRequestRepository asyncRequestRepository = AsyncRequestRepository.getInstance(configReader)){
return asyncRequestRepository.performAsyncRequest();
} catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -56,34 +56,4 @@ private void start(ThreadPool threadPool) {
}
);
}

@Override
public Lifecycle.State lifecycleState() {
return null;
}

@Override
public void addLifecycleListener(LifecycleListener lifecycleListener) {

}

@Override
public void removeLifecycleListener(LifecycleListener lifecycleListener) {

}

@Override
public void start() {

}

@Override
public void stop() {

}

@Override
public void close() {

}
}

0 comments on commit 8cd0eec

Please sign in to comment.