问题是这样在一个service类中有一个方法异步关闭操作,另外一个删除方法可以调用这个关闭操作,但是会卡住,并不能进入新线程关闭。代码如下:
@Slf4j
@Service("gAutoCmdService")
public class GAutoCmdServiceImpl extends ServiceImpl<GAutoCmdMapper, GAutoCmd> implements GAutoCmdService {
@Override
public boolean removeById(Serializable id) {
GAutoCmd gAutoCmd = getById(id);
if(gAutoCmd.getStatus()==1){
//运行中,停止任务 再删除
stopTask(new GAutoCmd(gAutoCmd), false);
}
return super.removeById(id);
}
@Override
@Async
public void stopTask(GAutoCmd gAutoCmd, boolean restart){
Thread.sleep(60000);
}
}
现在有一个办法可以解决,就是不知道有没有更好的办法,使用applicationContext,代码如下:
@Slf4j
@Service("gAutoCmdService")
public class GAutoCmdServiceImpl extends ServiceImpl<GAutoCmdMapper, GAutoCmd> implements GAutoCmdService {
@Autowired
private ApplicationContext applicationContext;
@Override
public boolean removeById(Serializable id) {
GAutoCmd gAutoCmd = getById(id);
if(gAutoCmd.getStatus()==1){
//运行中,停止任务 再删除
GAutoCmdService self = (GAutoCmdService)applicationContext.getBean("gAutoCmdService");
self.stopTask(new GAutoCmd(gAutoCmd), false);
}
return super.removeById(id);
}
@Override
@Async
public void stopTask(GAutoCmd gAutoCmd, boolean restart){
Thread.sleep(60000);
}
}
有没有更好的办法啊,
Java服务中异步处理导致删除阻塞的问题,
在GAutoCmdServiceImpl类中,removeById方法尝试异步调用stopTask来关闭运行中的任务,但在实际执行时出现阻塞。通过使用ApplicationContext获取bean的实例来解决这个问题,但寻求更好的解决方案。目前的解决方法是通过应用上下文自我调用来避免阻塞。
1967





