java - 服务可以在 Spring Boot 中更新控制器以进行长进程吗
问题描述
这是一个更多关于spring boot中服务和控制器注释类之间通信的问题。我有一个RestController类,它公开了一个调用Service类中的方法的 POST 映射。现在这个方法可能需要很长时间运行;因此需要向控制器发送某种反馈。
是否有任何机制允许服务调用/更新控制器中的方法/变量?
解决方案
最简单的方法之一是将一些lamda
函数从控制器传递给服务,然后像这样从服务中调用它
控制器类
@RestController
public class controller {
@Autowired
Service service;
public void foo() {
service.foo(..parms, (message/*any params you want*/) -> {
// here the body that will receive the message from the service
System.out.print(message);
});
}
}
服务等级
public class Service {
// updateStatus here is the function you will send the update to the controller from
public void foo(...params, updateStatus) {
updateStatus("starting the process...");
// do some code
updateStatus("in progress...");
// do some code
updateStatus("completed");
}
}
推荐阅读
- angular - NFS 文件系统的正则表达式
- python - 如何将列表与常见元素合并,这些元素本身就是列表/元组?
- python - 要列出的 Python 字符计数器
- c# - Excel 互操作的无效索引异常
- reactjs - React sestate 没有更新
- kdoc - 单击 KDoc 中的外部链接不会打开浏览器
- java - 在Java中断言地图列表忽略顺序
- javascript - 将 ID 传递给 URL 参数和 API。URL 参数获取 ID 但 id 在 React 中传递 null
- crystal-lang - 正在运行水晶 app.cr 与水晶运行 app.cr 相同
- javascript - javascript中的null和undefined有什么区别?