系统的消息消费者组件处理每条消息并使用 WebSockets 异步通知看护者应用程序
The message consumer component of the system processes each message and notifies asynchronously using WebSockets the caregiver application
我正在尝试通过 websocket 从 rabbitmq 的消费者发送一些通知,并将它们发布到 angular 应用程序上。
我是 rabbitmq 和 websocket 的新手,我不知道如何配置 websocket 以从消费者那里获取消息
这是我的消费者:
@Override
@RabbitListener(queues = "${rabbitmq.queue}")
public void onMessage(Message message) {
String timi = new String(message.getBody());
JsonObject jsonObject = new Gson().fromJson(timi, JsonObject.class);
MonitoredData monitoredData = new MonitoredData();
monitoredData.setIdPatient((jsonObject.get("Id patient")).getAsString());
monitoredData.setActivity((jsonObject.get("Activity")).getAsString());
monitoredData.setStartTime((jsonObject.get("Start time")).getAsString());
monitoredData.setEndTime((jsonObject.get("End time")).getAsString());
monitoredDataRepo.save(monitoredData);
if (monitoredData.getActivity().contains("Sleeping")) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date1 = LocalDateTime.from(f.parse(monitoredData.getStartTime()));
LocalDateTime date2 = LocalDateTime.from(f.parse(monitoredData.getEndTime()));
Duration d = Duration.between(date1, date2);
if ((d.getSeconds() / 3600) > 9) {
System.out.println("The patient slept more than 9 hours");
}
}
if (monitoredData.getActivity().contains("Leaving\t")) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date1 = LocalDateTime.from(f.parse(monitoredData.getStartTime()));
LocalDateTime date2 = LocalDateTime.from(f.parse(monitoredData.getEndTime()));
Duration d = Duration.between(date1, date2);
if ((d.getSeconds() / 3600) > 3) {
System.out.println("The patient was out more than 3 hours");
}
}
if (monitoredData.getActivity().contains("Toileting\t") || monitoredData.getActivity().contains("Showering\t")) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date1 = LocalDateTime.from(f.parse(monitoredData.getStartTime()));
LocalDateTime date2 = LocalDateTime.from(f.parse(monitoredData.getEndTime()));
Duration d = Duration.between(date1, date2);
if ((d.getSeconds() / 60) >= 30) {
System.out.println("The patient was in the bathroom more than 30 minutes");
}
}
// System.out.println("Received message" + 消息);
}
我从 System.out.println 获取的消息是我想在 Angular 应用程序中弹出的消息。
我也有这个兔子控制器:
@GetMapping("/send")
public String sendMessage() throws InterruptedException {
producer.produceMsg();
return "Message successfully sent";
}
我也是 RabbitMq 的新手,但这是我对 WebSocket 的配置。
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
static final String MESSAGE_PREFIX = "/topic";
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/gs-guide-websocket").setAllowedOrigins("*").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker(MESSAGE_PREFIX, "/queue");
registry.setApplicationDestinationPrefixes("/app");
}
}
在消费者中class你可以注入SimpMessagingTemplate websocket;然后你可以在 RabbitListener 中使用这个:
this.websocket.convertAndSend("/主题" , json);
至于前端部分,我用的是React,但我觉得差不多。您可以 google sockJs 并尝试接收消息。祝你好运!
我正在尝试通过 websocket 从 rabbitmq 的消费者发送一些通知,并将它们发布到 angular 应用程序上。
我是 rabbitmq 和 websocket 的新手,我不知道如何配置 websocket 以从消费者那里获取消息
这是我的消费者:
@Override
@RabbitListener(queues = "${rabbitmq.queue}")
public void onMessage(Message message) {
String timi = new String(message.getBody());
JsonObject jsonObject = new Gson().fromJson(timi, JsonObject.class);
MonitoredData monitoredData = new MonitoredData();
monitoredData.setIdPatient((jsonObject.get("Id patient")).getAsString());
monitoredData.setActivity((jsonObject.get("Activity")).getAsString());
monitoredData.setStartTime((jsonObject.get("Start time")).getAsString());
monitoredData.setEndTime((jsonObject.get("End time")).getAsString());
monitoredDataRepo.save(monitoredData);
if (monitoredData.getActivity().contains("Sleeping")) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date1 = LocalDateTime.from(f.parse(monitoredData.getStartTime()));
LocalDateTime date2 = LocalDateTime.from(f.parse(monitoredData.getEndTime()));
Duration d = Duration.between(date1, date2);
if ((d.getSeconds() / 3600) > 9) {
System.out.println("The patient slept more than 9 hours");
}
}
if (monitoredData.getActivity().contains("Leaving\t")) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date1 = LocalDateTime.from(f.parse(monitoredData.getStartTime()));
LocalDateTime date2 = LocalDateTime.from(f.parse(monitoredData.getEndTime()));
Duration d = Duration.between(date1, date2);
if ((d.getSeconds() / 3600) > 3) {
System.out.println("The patient was out more than 3 hours");
}
}
if (monitoredData.getActivity().contains("Toileting\t") || monitoredData.getActivity().contains("Showering\t")) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date1 = LocalDateTime.from(f.parse(monitoredData.getStartTime()));
LocalDateTime date2 = LocalDateTime.from(f.parse(monitoredData.getEndTime()));
Duration d = Duration.between(date1, date2);
if ((d.getSeconds() / 60) >= 30) {
System.out.println("The patient was in the bathroom more than 30 minutes");
}
}
// System.out.println("Received message" + 消息); }
我从 System.out.println 获取的消息是我想在 Angular 应用程序中弹出的消息。
我也有这个兔子控制器:
@GetMapping("/send")
public String sendMessage() throws InterruptedException {
producer.produceMsg();
return "Message successfully sent";
}
我也是 RabbitMq 的新手,但这是我对 WebSocket 的配置。
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
static final String MESSAGE_PREFIX = "/topic";
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/gs-guide-websocket").setAllowedOrigins("*").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker(MESSAGE_PREFIX, "/queue");
registry.setApplicationDestinationPrefixes("/app");
}
}
在消费者中class你可以注入SimpMessagingTemplate websocket;然后你可以在 RabbitListener 中使用这个: this.websocket.convertAndSend("/主题" , json);
至于前端部分,我用的是React,但我觉得差不多。您可以 google sockJs 并尝试接收消息。祝你好运!