给原有界面添加一个停止训练功能(示例代码)

栏目: Web · 发布时间: 2021-01-25

来源:cnblogs.com

简介  这篇文章主要介绍了给原有界面添加一个停止训练功能(示例代码)以及相关的经验技巧,文章约8007字,浏览量312,点赞数2,值得参考!

在界面增加一个“停止训练”按钮

<td  class="center">
  <a href=‘/trainlog/setlist?eq_id=${entity.setId}&eq_orgId=${entity.orgId}‘>查看日志</a>
  <a  class="btn btn-minier" href="#" onclick="stop(‘${entity.id}‘)"  >停止训练</a>
</td>

点击“停止训练”,进到stop方法里,传递id作为参数

function stop(id){
                     $.ajax({
                         type: "POST",
                         url: ‘/traintask/stoptask‘,
                         data:   "id="+id,
                         async: false,
                         success: function(msg)
                         {
                            layer.msg(msg.msg, {
                                  icon: 1,
                                  time: 1000 //2秒关闭(如果不配置,默认是3秒)
                                }, function(){
                                    $("#listForm").submit();
                                });   
                         } 
                    });
                }
在对应路径下建方法 url: ‘/traintask/stoptask‘
@Controller
@RequestMapping("/traintask")
public class TrainTaskController extends BaseController{
    @Autowired
    private TrainTaskService trainTaskService;
 
  /**
     * 停止训练任务
     * @param id
     * @param request
     * @return
     */
    @ResponseBody 
    @RequestMapping(value="/stoptask",produces = "application/json; charset=utf-8",method={RequestMethod.POST,RequestMethod.GET})
    public Object stopTask(String id,HttpServletRequest request){   
        int flag = trainTaskService.stopTask(id);
        return  genRetCommonMsg(flag,0,request);
    }
}

controller建立stopTask方法实现停止训练,将方法放进service里,便于其他controller调用

public interface TrainTaskService extends BaseService<TrainTask,TrainTaskFilter> {
        ...
    public int stopTask (String id);
}

方法写进实现类里

@Service
public class TrainTaskServiceImpl extends BaseServiceImpl<TrainTask,TrainTaskFilter> implements TrainTaskService {
    @Autowired
    TagTypeService tagTypeService;
    @Autowired
    TrainLogService trainLogService;
    @Autowired
    TrainTaskService trainTaskService;


...

    //重构后清空日志
                TrainLogFilter filter = new TrainLogFilter();
                filter.setEq_setId(setId);
                List<TrainLog> trainLogs = trainLogService.selectAll(filter);
                for(TrainLog trainLog : trainLogs){
                    trainLog.setDelFlag(0);
                    trainLogService.save(trainLog);
                }

...
       @Transactional
       public int stopTask(String id){
           TrainTask task = trainTaskService.getById(id);
           int flag=0;
           //查看是否处于训练中,
           //处于训练中,停掉客户端,clientState记为1,训练停止后开启客户端
           //未训练中,clientState仍为0,不需停止客户端
           int clientState =0;//      
           String clientId="";

           //停掉客户端
           if(task != null && task.getDelFlag() != 0 && task.getStates() == 1){
               clientState=1;
               TrainClientFilter filter = new TrainClientFilter();
               filter.setLk_ip(task.getTrainNum().split("-")[0]);
               filter.setEq_number(task.getTrainNum().split("-")[1]);
               filter.setEq_delFlag(1);
               TrainClient client = trainClientService.selectAll(filter).get(0);
               clientId = client.getId();
               
               trainClientService.addtask(clientId, "shutdown");
           }           
           //TagType表取到setid后,改completestates为0
           String setId = task.getSetId();
           TagType set = tagTypeService.getById(setId);
           if(set != null && set.getCompleteStates() != 0){
               set.setCompleteStates(0);                   
           }else {
            System.out.println("已是停止训练状态");
           }
           task.setDelFlag(0);
       
           tagTypeService.save(set);
           trainTaskService.save(task);

           if(set.getCompleteStates()==0 && task.getDelFlag()==0){
               flag=1;
           }
           
           return flag;
       }
}

还有重构时再加上清空日志


以上就是本文的全部内容,希望对大家的学习有所帮助,版权归原作者或者来源机构所有,感谢作者,如果未能解决你的问题,请参考以下文章。

架构师内功心法,有重构项目经验必备的装饰者模式详解

架构师内功心法,有重构项目经验必备的装饰者模式详解

MVC与单元测试实践之健身网站(七)-添加计划(示例代码)

装饰模式(示例代码)

java设计模式之装饰器模式(示例代码)