项目中有2个定时任务要执行,并且调用相同的公共类查询hbase
public class CronA {
@Autowired
GetDataByAccounts a;
@Async
@Scheduled(fixedRate = 1000*60*1)
public void A(){
String key = "xx";
a.getDataByAccounts(key);
}
}
public class CronB {
@Autowired
GetDataByAccounts b;
@Async
@Scheduled(fixedRate = 1000*60*3)
public void B(){
String key = "xx";
b.getDataByAccounts(key);
}
}
公共类的代码如下:
public class GetDataByAccounts{
Connection conn;
Configuration conf;
public String getDataByAccounts(String key){
// Connection conn = null;
// Configuration conf = HBaseConfiguration.create();
// hbase操作省略
}
}
上线后,报错:
error日志:
2021-02-07 15:27:18 [task-2] ERROR o.a.h.h.c.AsyncRequestFutureImpl -- - Cannot get replica 0 location for {"cacheBlocks":true,"totalColumns":0,"row":"xx","families":{},"maxVersions":1,"timeRange":[0,9223372036854775807]}
2021-02-07 15:27:18 [task-2] ERROR c.i.tools.service.GetDataByAccounts -- - 操作hbase异常:Failed 33 actions: hconnection-0xd440f89 closed: 33 times, servers with issues: null
org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException: Failed 33 actions: hconnection-0xd440f89 closed: 33 times, servers with issues: null
warn日志:
2021-02-07 15:27:18 [task-2] WARN o.a.h.h.c.AsyncRequestFutureImpl -- - id=1, task rejected by pool. Unexpected. Server=n2,16020,1609750056695
java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@7803f2e2 rejected from java.util.concurrent.ThreadPoolExecutor@4cdfbc3a[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 14]
查阅发现:可能的原因是table被提前关闭了
结合代码来看:
1、使用@Autowired注解的对象a,b,打印出地址:发现是同一个对象,从而共享了同一个Connection和Table,构造函数也只调用了1次
2、2个定时任务第一次运行的时间相同,导致其中1个操作结束后关闭了table,另一个操作报错,并且是一定概率发生
解决方法是:
将Connection和Configuration的定义由类变量改为函数内的局部变量,即:GetDataByAccounts中注释的2行代码
网友评论