问题是这样在一个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);
}
}
有没有更好的办法啊,