使用 WebSocket 和 Spring 从 ActiveMQ 主题中删除旧消息

Removing old messages from ActiveMQ topics with WebSocket and Spring

我在 Spring 中配置了嵌入式 ActiveMQ 代理,支持 websocket(使用 STOMP)。

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketMqConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableStompBrokerRelay("/topic");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/messaging")
            .setAllowedOrigins("*")
            .withSockJS();
}

@Bean(initMethod = "start", destroyMethod = "stop")
public BrokerService brokerService() throws Exception {
    PersistenceAdapter persistenceAdapter = getPersistenceAdapter();
    BrokerService brokerService = new BrokerService();
    brokerService.setPersistent(true);
    brokerService.setDeleteAllMessagesOnStartup(true);
    brokerService.setUseJmx(false);
    brokerService.setBrokerName("broker");
    brokerService.addConnector("stomp://localhost:61613");
    return borkerService;
}

在我的 JavaScript 客户端中,我订阅了主题:

    var successHandler = function() {
        stompClient.subscribe('/topic/test', function(not) {
            pushNotification(not);
        }, {'id': clientId, 'activemq.subscriptionName': clientId});
    };

    var socket = new SockJS('/messaging');
    var stompClient = Stomp.over(socket);
    stompClient.connect({'client-id': clientId}, successHandler, failureHandler);

我正在使用后端服务来提供这个主题:

@Autowired
private SimpMessagingTemplate messagingTemplate;

messagingTemplate.convertAndSend("/topic/test", event);

这是我的问题:

  1. 当我向主题发送消息时,但客户端尚未订阅,为什么消息没有持久化(我想在客户端订阅后,他应该会收到有关错过消息的通知)?
  2. 如果客户端与主题断开连接,每条消息都会被持久化,是否有任何方法可以限制持久化消息的数量、时间或 KahaDB 日志文件的大小?

除非客户端创建了 durable Topic subscription previously and the message is sent with the persistent flag set. To create a durable subscription add the headers as specified in the ActiveMQ STOMP 文档,否则发送到主题的消息不会持久化。

一旦您开始使用持久主题订阅,那么是的,消息会在 KahaDB 存储中累积,此时您可以配置 store usage limits 来控制大小。