Reputation: 729
Hi I have created a simple task which is hitting one of the third party apis,
I tried to create a callable task and execute it in parallel with ExecutorService
for (String script : allScripts) {
callableTasks.add(new DataFetchTask(script,TimeInterval.get(interval),
Range.get(range)).newCallable());
}
final ExecutorService executorService = new ThreadPoolExecutor(1, 10,
10000, TimeUnit.MILLISECONDS,
new SynchronousQueue<Runnable>());
try {
System.out.println("Callable Tasks"+callableTasks.size());
final Future<YahooFinResponse> yahooFinResponseFuture = executorService.submit(callableTasks.get(0));
/*final List<Future<YahooFinResponse>> futures = executorService.invokeAll(callableTasks);
for(Future<YahooFinResponse> future : futures) {
yahooFinResponses.add(future.get());
}*/
return FinDataServiceUtil.writeResponsesToCSVFile(yahooFinResponses);
} catch (Exception e) {
e.printStackTrace();
}
Task Class
@RequiredArgsConstructor
public class DataFetchTask {
private final String script;
private final TimeInterval timeInterval;
private final Range timeRange;
@Autowired
YahooFinDataFetcher yahooFinDataFetcher;
public Callable<YahooFinResponse> newCallable() {
return new Callable<YahooFinResponse>() {
public YahooFinResponse call() {
System.out.println("Starting Job for Script"+script);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("After wait");
final YahooFinResponse yahooFinResponse = yahooFinDataFetcher.fetchData(script +
".NS", timeInterval, timeRange);
System.out.println("Returning from call"+yahooFinResponse.chart.error);
return yahooFinResponse;
}
};
}
}
Service class
@Component
public class YahooFinDataFetcher {
private static final OkHttpClient client = new OkHttpClient();
private static final Gson gson = new Gson();
private static final String BASE_URL = "***;
private static final String DOMAIN = "**";
private static final String CORS_DOMAIN = "***";
public YahooFinResponse fetchData (final String scriptName, final TimeInterval timeInterval,
final Range range) {
System.out.println("Calling Service");
final Request request = new Request.Builder()
.url(createUrl(scriptName, timeInterval, range))
.build();
final Call call = client.newCall(request);
try {
final Response response = call.execute();
final String responseStr = response.body().string();
final YahooFinResponse yahooFinResponse = gson.fromJson(responseStr,
YahooFinResponse.class);
System.out.println(String.format("Returning Data For YahooFInResponse %s "
+ "\n",yahooFinResponse));
return yahooFinResponse;
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(String.format("Returning null from fetchData for %s TimeInterval %s, "
+ "Range %s \n",scriptName,timeInterval,range));
return null;
}
I am getting output as
Size5list[RELIANCE, SBIN, YESBANK, ITC, VEDL]
Callable Tasks5
Starting Job for ScriptRELIANCE
After wait
But not getting output of job completion,
I tried with reducing job size, Changing creation of ExecutorService
with FixedThreadPool
and CachedThreadPool
Changing DataFetchTask
to newCallable as method and earlier this class itself was implementing Callable<T>
directly
When i try to get future.get()
it is giving NullPointerException
I tried multiple things, but not sure what is wrong with Callable
task creation.
I am hitting service through the controller
Upvotes: 0
Views: 304
Reputation: 2046
Your yahooFinDataFetcher is probably null since IOC was not able to inject an instance of it. DataFetchTask is not being handle as a component.
Try to manually instantiate yahooFinDataFetcher at DataFetchTask and see if it was reallly the case
@RequiredArgsConstructor
public class DataFetchTask {
private final String script;
private final TimeInterval timeInterval;
private final Range timeRange;
@Autowired
YahooFinDataFetcher yahooFinDataFetcher = new YahooFinDataFetcher();
...
Upvotes: 1