Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果客户端关闭浏览器选项卡,如何停止spring MVC控制器的执行?_Java_Spring_Spring Mvc_Websocket_Spring Websocket - Fatal编程技术网

Java 如果客户端关闭浏览器选项卡,如何停止spring MVC控制器的执行?

Java 如果客户端关闭浏览器选项卡,如何停止spring MVC控制器的执行?,java,spring,spring-mvc,websocket,spring-websocket,Java,Spring,Spring Mvc,Websocket,Spring Websocket,我正在开发一个web应用程序,它使用SpringWebSocket连接向特定用户发送多条消息。 我有一个SpringMVC控制器,当从客户端收到STOMP时,它开始向用户发送消息。是这样的: @Controller public class Controller{ @Autowired private SimpMessagingTemplate template; @MessageMapping("/") @SendToUser("/queue/user

我正在开发一个web应用程序,它使用SpringWebSocket连接向特定用户发送多条消息。 我有一个SpringMVC控制器,当从客户端收到STOMP时,它开始向用户发送消息。是这样的:

@Controller
public class Controller{
     @Autowired
     private SimpMessagingTemplate template;

     @MessageMapping("/")
     @SendToUser("/queue/user")
     public void myMethod(ClientMessage msg, Principal principal){
          Object myObject = new Object();
          //stuff
          while(true){
               //more stuff
               this.template.convertAndSendToUser(principal.getName(), "/queue/user", myObject);
          }
     }
}
@Controller
public class Controller implements ApplicationListener<SessionDisconnectEvent>{

     @Autowired
     private SimpMessagingTemplate template;

     private boolean disconnected;

     @MessageMapping("/")
     @SendToUser("/queue/user")
     public void myMethod(ClientMessage msg, Principal principal){
          Object myObject = new Object();
          //stuff
          while(!disconnected){
               //more stuff
               this.template.convertAndSendToUser(principal.getName(), "/queue/user", myObject);
          }
     }

     @EventListener
     @Override
     public void onApplicationEvent(SessionDisconnectEvent  applicationEvent) {
          System.out.println("SESSION " + applicationEvent.getSessionId() + " DISCONNECTED");
          this.graphService.setDisconnect(true);
     }
}
现在我的问题是:有一种方法可以知道客户端何时关闭(或重新加载)浏览器选项卡? 当客户端关闭选项卡以终止控制器执行时,我想退出while循环。 我想通过检查一些东西来停止while循环,比如:
while(!session.isClose())

有人能帮我解决这个问题吗?

如果有人需要,我会发布我的解决方案

我真的不知道我做的是否正确,但我将SessionDisconnectEvent侦听器从特定类移动到了我的控制器。因此,现在控制器实现ApplicationListener并重写onApplicationEvent。当用户关闭选项卡时,布尔值被设置为true,控制器停止执行。 现在我有了这样的东西:

@Controller
public class Controller{
     @Autowired
     private SimpMessagingTemplate template;

     @MessageMapping("/")
     @SendToUser("/queue/user")
     public void myMethod(ClientMessage msg, Principal principal){
          Object myObject = new Object();
          //stuff
          while(true){
               //more stuff
               this.template.convertAndSendToUser(principal.getName(), "/queue/user", myObject);
          }
     }
}
@Controller
public class Controller implements ApplicationListener<SessionDisconnectEvent>{

     @Autowired
     private SimpMessagingTemplate template;

     private boolean disconnected;

     @MessageMapping("/")
     @SendToUser("/queue/user")
     public void myMethod(ClientMessage msg, Principal principal){
          Object myObject = new Object();
          //stuff
          while(!disconnected){
               //more stuff
               this.template.convertAndSendToUser(principal.getName(), "/queue/user", myObject);
          }
     }

     @EventListener
     @Override
     public void onApplicationEvent(SessionDisconnectEvent  applicationEvent) {
          System.out.println("SESSION " + applicationEvent.getSessionId() + " DISCONNECTED");
          this.graphService.setDisconnect(true);
     }
}
@控制器
公共类控制器实现ApplicationListener{
@自动连线
私有SimpMessagingTemplate;
私有布尔连接;
@消息映射(“/”)
@发送者(“/queue/user”)
public void myMethod(ClientMessage消息,主体){
对象myObject=新对象();
//东西
当(!断开连接){
//更多的东西
this.template.convertAndSendToUser(principal.getName(),“/queue/user”,myObject);
}
}
@事件监听器
@凌驾
applicationEvent上的公共无效(SessionDisconnectEvent applicationEvent){
System.out.println(“SESSION”+applicationEvent.getSessionId()+“DISCONNECTED”);
this.graphService.setDisconnect(true);
}
}