Java ExecutorService获取所有任务的反馈
我想向多个(50010002000)用户发送电子邮件 我已经使用Java ExecutorService获取所有任务的反馈,java,executorservice,Java,Executorservice,我想向多个(50010002000)用户发送电子邮件 我已经使用ExecutorService完成了这项工作 但现在我想从总记录中收集成功发送的电子邮件数量和失败的电子邮件数量 我已实施了以下措施: int startValue=0; int endValue=0; List userEmailList = getListFromDB(); ExecutorService e = Executors.newFixedThreadPool(10); Collection c = new Arra
ExecutorService
完成了这项工作
但现在我想从总记录中收集成功发送的电子邮件数量和失败的电子邮件数量
我已实施了以下措施:
int startValue=0;
int endValue=0;
List userEmailList = getListFromDB();
ExecutorService e = Executors.newFixedThreadPool(10);
Collection c = new ArrayList();
while (someflag)
{
// in MyTask class I am sending email to users.
c.add(new MyTask(startValue, endValue,userEmailList));
}
e.invokeAll(c); //Here I am calling invokeall .
pool.shutdown();
public class MyTask implements Callable<String> {
MyTask(startValue, endValue,userEmailList){
}
public String call(){
//e.g. batch 1 will have - startValue => endValue = 0 -100
//e.g. batch 2 will have - startValue => endValue = 101 -199
//e.g. batch 3 will have - startValue => endValue = 200 -299
//e.g. batch 4 will have - startValue => endValue = 300 -399
//e.g. batch 5 will have - startValue => endValue = 400 - 499
for(int i=startValue;i<endValue;i++){
sendEmailToUser(userEmailList.get(i)){
}
}
int startValue=0;
int-endValue=0;
List userEmailList=getListFromDB();
ExecutorService e=Executors.newFixedThreadPool(10);
集合c=新的ArrayList();
while(someflag)
{
//在MyTask类中,我向用户发送电子邮件。
c、 添加(新的MyTask(startValue、endValue、userEmailList));
}
e、 L(c)//我给你打电话。
pool.shutdown();
公共类MyTask实现可调用的{
MyTask(起始值、结束值、用户电子邮件列表){
}
公共字符串调用(){
//e、 g.第1批的-startValue=>endValue=0-100
//e、 g.第2批将具有-startValue=>endValue=101-199
//e、 g.第3批的-startValue=>endValue=200-299
//e、 g.第4批的-startValue=>endValue=300-399
//e、 g.第5批的-startValue=>endValue=400-499
对于(int i=startValue;i我可以看到您的call方法被声明为返回字符串,但您的代码没有返回任何内容(可能是不完整的代码段)。从您的声明中,我了解到您返回的是任务是否完成,而不是邮件是否已发送。您可以让sendEmailToUser
返回失败的成功,这取决于邮件是否已成功发送,并使用Future.get
您的MyTask获得结果
返回一个字符串
(实现可调用
),这在您的情况下没有多大意义。您可以随意返回任何其他类型。不幸的是,您需要一些简单的POJO来包含结果,例如:
public class Result {
private final int successCount;
private final int failureCount;
public Result(int successCount, int failureCount) {
this.successCount = successCount;
this.failureCount = failureCount;
}
}
然后在完成给定的批处理后返回它(实现可调用
)。当然,您的MyTask
必须跟踪失败的电子邮件数量,并返回正确的值,这些值包含在结果中
不过,我看到了几种改进代码的方法。首先,不要将startValue,endValue
range传递给MyTask
,只需使用userEmailList.subList(startValue,endValue)
-这将大大简化您的代码:
newmytask(userEmailList.subList(startValue,endValue));
//...
公共类MyTask实现可调用的{
MyTask(用户电子邮件列表){
}
公共结果调用(){
用于(电子邮件:userEmailList){
sendEmailToUser(电子邮件);
//在这里收集结果
}
返回新结果(…);
}
}
另一方面,创建MyTask
只发送一封电子邮件并没有什么错误。您只需检查一个任务(一封电子邮件)的结果,而不是在给定的批中聚合计数-要么没有,要么异常(或单个布尔值
)。它简单得多,速度也不应该慢。您可以发布完整的代码片段吗?我想您需要返回类型为List
,而不仅仅是String
new MyTask(userEmailList.subList(startValue, endValue));
//...
public class MyTask implements Callable<Result> {
MyTask(userEmailList){
}
public Result call(){
for(email: userEmailList) {
sendEmailToUser(email);
//collect results here
}
return new Result(...);
}
}