2015-02-11 20 views
0

因此,在前端主页中有一个可以打开和关闭的开关。交换机的状态码可以使用Ajax发送到后端。Spring MVC:通过打开和关闭网页上的开关来触发和终止后端方法

现在,当开关接通时,我想后端方法继续运行,这是我的定义如下:

public static void foo { 

    Thread thread = new Thread(new Runnable() { 

     @Override 
     public void run() { 
      while(true) { 
       try { 
        Thread.sleep(8000); 
        ArticleDAO.addNewArticles(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }); 

    thread.start(); 
} 

而当切换截止时,我想的方法,或要终止的线程。

在JSP页面中的Ajax代码看起来是这样的:

$("#switcher").on("switchChange.bootstrapSwitch", function(e, state) { 
    if (state == true) { 

     $(function() { 
      $.ajax({ 
       type: "POST", 
       url: "http://localhost:8080/wx/admin/toggle_state.html", 
       data: { 
        stateCode: 1 
       }, 
       success: function(data) { 
        alert("Switched to auto mode"); 
       }, 
       error: function() { 
        alert("Something went wrong!!"); 
       } 
      }); 
     }); 

    } else if (state == false) { 

     $(function() { 
      $.ajax({ 
       type: "POST", 
       url: "http://localhost:8080/wx/admin/toggle_state.html", 
       data: { 
        stateCode: 0 
       }, 
       success: function(data) { 
        alert("Switched to manual mode"); 
       }, 
       error: function() { 
        alert("Something went wrong!!"); 
       } 
      }); 
     }); 

    } else { 
     alert("Something went wrong!!"); 
    } 
}); 

而这里的春天控制器:

@RequestMapping(value = "admin/toggle_state.html", method = RequestMethod.POST) 
@ResponseBody 
public String toggleState(@RequestParam Integer stateCode) throws Exception { 
    if (stateCode == 1) { 
     /* 

     EXECUTE THE FOO METHOD RIGHT HERE 

     */ 
     System.out.println("Switched to auto mode."); 
    } else if (stateCode == 0) { 
     // TERMINATE THE METHOD/THREAD HERE 
    } 
    return ""; 
} 

那么,什么是推荐的方式来实现这一目标?它实际上并不难,但我不知道如何开始和终止我的情况下的线程。我能想到的一种方法是将交换机的状态代码存储到数据库,并使方法在线程中每次运行该方法时检查数据库。但这似乎不是一个好的解决方案。有任何想法吗?提前致谢!!

回答

1

添加一个bean(适用范围),以保持对线程的引用。将bean自动装入控制器。

检查stateCode值并使用bean的引用来启动()/ stop()线程。

@Component 
public ThreadHolderBean { 
    Thread theThreadReference=new Thread();//init the thread properly 
} 
在CONTROLER

现在只需加

@Autowired 
private ThreadHolderBean threadHolder; 

,并使用threadHolder在toggleState()方法

如果需要,例如为每个会话改变bean的范围多线程或定义线程池。

0

既然你已经使用Spring,那么TaskExecutor 是此方案完美契合,与您能够使用底层的方法,如`ExecutorService.shutdown(),ExecutorService.shutdownNow(),awaitTerminationIfNecessary()

0

你可以做这样的事情从控制器...

public static void foo(int stateCodeFromFrontEnd) { 

Thread thread = new Thread(new Runnable() { 

    @Override 
    public void run() { 
     int stateCode = stateCodeFromFrontEnd; 
     while(stateCode == 1) { 
      try { 
       Thread.sleep(8000); 
       if(Thread.currentThread().isInterrupted()) { 
        stateCode = 0; 
       } 
       ArticleDAO.addNewArticles(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
}); 

thread.start(); 
} 

当你的开关关断时,调用了Thread.interrupt()。

+0

这对你很清楚吗? – 2015-02-11 16:49:18

+0

谢谢,但是如何在切换关闭时获得对该线程的引用? – jwong 2015-02-12 03:29:00

+0

你可以通过创建线程作为实例变量来完成。 – 2015-02-12 03:31:31