可调用任务未正确执行
Callable Task is not executing properly
您好,我创建了一个简单的任务,它正在访问一个第三方 API,
我尝试创建一个可调用任务并与 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();
}
任务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;
}
};
}
}
服务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;
}
我得到的输出为
Size5list[RELIANCE, SBIN, YESBANK, ITC, VEDL]
Callable Tasks5
Starting Job for ScriptRELIANCE
After wait
但没有得到作业完成的输出,
我尝试减少作业大小,将 ExecutorService
的创建更改为 FixedThreadPool
和 CachedThreadPool
将 DataFetchTask
更改为 newCallable 作为方法,而早些时候这个 class 本身正在直接实现 Callable<T>
当我尝试获取 future.get()
时,它给出 NullPointerException
我尝试了多种方法,但不确定 Callable
任务创建有什么问题。
我正在通过控制器点击服务
您的 yahooFinDataFetcher 可能为空,因为 IOC 无法注入它的实例。 DataFetchTask 未作为组件处理。
尝试在 DataFetchTask 中手动实例化 yahooFinDataFetcher,看看是否真的如此
@RequiredArgsConstructor
public class DataFetchTask {
private final String script;
private final TimeInterval timeInterval;
private final Range timeRange;
@Autowired
YahooFinDataFetcher yahooFinDataFetcher = new YahooFinDataFetcher();
...
您好,我创建了一个简单的任务,它正在访问一个第三方 API,
我尝试创建一个可调用任务并与 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();
}
任务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;
}
};
}
}
服务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;
}
我得到的输出为
Size5list[RELIANCE, SBIN, YESBANK, ITC, VEDL]
Callable Tasks5
Starting Job for ScriptRELIANCE
After wait
但没有得到作业完成的输出,
我尝试减少作业大小,将 ExecutorService
的创建更改为 FixedThreadPool
和 CachedThreadPool
将 DataFetchTask
更改为 newCallable 作为方法,而早些时候这个 class 本身正在直接实现 Callable<T>
当我尝试获取 future.get()
时,它给出 NullPointerException
我尝试了多种方法,但不确定 Callable
任务创建有什么问题。
我正在通过控制器点击服务
您的 yahooFinDataFetcher 可能为空,因为 IOC 无法注入它的实例。 DataFetchTask 未作为组件处理。
尝试在 DataFetchTask 中手动实例化 yahooFinDataFetcher,看看是否真的如此
@RequiredArgsConstructor
public class DataFetchTask {
private final String script;
private final TimeInterval timeInterval;
private final Range timeRange;
@Autowired
YahooFinDataFetcher yahooFinDataFetcher = new YahooFinDataFetcher();
...