简体   繁体   中英

Removing old messages from ActiveMQ topics with WebSocket and Spring

I have embedded ActiveMQ broker configured in Spring with websocket support (using 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;
}

In my JavaScript client I subscribe to topic:

    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);

And I am using backend service to feed this topic:

@Autowired
private SimpMessagingTemplate messagingTemplate;

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

And here are my questions:

  1. When I send message to topic, but client haven't been subscribed yet, why messages are not persisted (I suppose that after client subscribe, he should be notified about missed messages)?
  2. If client disconnect from topic, every message is persisted, is there any mean to restrict number of persisted messages, time or size of KahaDB's log files?

Messages sent to a Topic are not persisted unless the client has created a 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 documentation.

Once you start using durable Topic subscriptions then yes message can accumulate in the KahaDB store at which point you can configure the store usage limits to control size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM