我配置了一个Eventbus,它将使用监视器事件更新Map。因此,对于每个名称,地图将始终具有最新的监视器事件。作为用户,您会看到最近的事件,您会转到URL“/ overview”,它会显示一个基本的HTML页面。
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
@Controller
public class OverviewController {
private final Map<String, MonitorResponse> monitorEvents;
@Autowired
public OverviewController(EventBus eventBus) {
monitorEvents = new HashMap<>();
eventBus.register(this);
}
@RequestMapping("/overview")
public String overview(Model model) {
model.addAttribute("monitorResponseMap", monitorEvents);
return "overview";
}
@Subscribe
public void subscribe(MonitorEvent monitorEvent) {
monitorEvents.put(monitorEvent.getName(), monitorEvent.getMonitorResponse());
}
}
HTML:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="/bootstrap-4.0.0/css/bootstrap.min.css"/>
<link rel="stylesheet" href="/css/styles.css"/>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-"
th:each="monitorevent : ${monitorResponseMap}"
style="margin: 10px;padding:20px;border-radius: 10px;"
th:classappend="${monitorevent.value.getMonitorStatus().name()}">
<div class="card-title" style="font-weight: bolder" th:text="${monitorevent.key}"></div>
<p th:text="${monitorevent.value.getMessage()}"></p>
</div>
</div>
</div>
<script src="/jquery-3.3.1.min.js"></script>
<script src="/bootstrap-4.0.0/js/bootstrap.min.js"></script>
</body>
</html>
当有新事件时,动态更新UI的最简单方法是什么?所以当调用subscribe方法时? 现在我对页面进行自动Javascript刷新(使用标签元(http-equiv = refresh),但这对用户来说很烦人。使用websockets?Ajax?
答案 0 :(得分:2)
感谢Venu Duggireddy,我了解了SSE,并按照https://github.com/aliakh/demo-spring-sse的演示
这很容易解决我的问题:
SSE控制器
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@RestController
public class SseController {
private final SseService sseService;
@Autowired
public SseController(SseService sseService) {
this.sseService = sseService;
}
@RequestMapping(path = "/sse/infinite", method = RequestMethod.GET)
public SseEmitter getInfiniteMessages() {
return sseService.getInfiniteMessages();
}
}
当我想要更新前端时,我只需使用notifyListeners调用SseService:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@Component
public class SseService {
private final static Logger LOGGER = LoggerFactory.getLogger(SseService.class);
private final SseEmitter emitter;
@Autowired
public SseService() {
emitter = new SseEmitter();
}
SseEmitter getInfiniteMessages() {
return emitter;
}
public void notifyListeners(MonitorEvent monitorEvent) {
try {
emitter.send(monitorEvent);
} catch (IOException e) {
LOGGER.error("Sse message " + monitorEvent + "not send", e);
}
}
}
在HTML中:
<script type="application/javascript">
if (!!window.EventSource) {
var eventSource = new EventSource("/sse/infinite");
eventSource.onmessage = function (e) {
var message = JSON.parse(e.data);
console.log(message);
//updating my HTML here
};
eventSource.onerror = function (e) {
if (e.readyState === EventSource.CONNECTING) {
console.log('event: CONNECTING');
} else if (e.readyState === EventSource.OPEN) {
console.log('event: OPEN');
} else if (e.readyState === EventSource.CLOSING) {
console.log('event: CLOSING');
} else if (e.readyState === EventSource.CLOSED) {
console.log('event: CLOSED');
}
};
} else {
alert('The browser does not support Server-Sent Events');
}
</script>