问题
How to set loggingPeriod in WebSocketMessageBrokerStats to decrease value (default is 30')
WebSocketMessageBrokerStats is loaded by @Bean in WebSocketMessageBrokerConfigurationSupport
version : Spring 4.2.0.RELEASE
My current Config :
@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class AppWebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(final MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
}
@Override
public void registerStompEndpoints(final StompEndpointRegistry registry) {
registry.addEndpoint("/entry")
.setAllowedOrigins("*")
.withSockJS()
.setDisconnectDelay(10000);
}
}
回答1:
According to the WebSocketMessageBrokerStats javadoc:
This class is declared as a Spring bean by the above configuration with the name "webSocketMessageBrokerStats"
So you can add this to your configuration class:
@Autowired
private WebSocketMessageBrokerStats webSocketMessageBrokerStats;
@PostConstruct
public void init() {
webSocketMessageBrokerStats.setLoggingPeriod(10 * 1000); // desired time in millis
}
回答2:
If you want to setup the logging period before Spring has put the bean into service, you can use a PostBeanProcessor
:
@Bean
public BeanPostProcessor beanPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof WebSocketMessageBrokerStats) {
WebSocketMessageBrokerStats webSocketMessageBrokerStats = (WebSocketMessageBrokerStats) bean;
webSocketMessageBrokerStats.setLoggingPeriod(30 * 1000); // your customization
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
};
}
With WebSocketMessageBrokerStats
it is not much of a difference to Toyo's way. However if you have a bean which won't let you change a setting after initialization, this way will always work.
来源:https://stackoverflow.com/questions/32865949/websocketmessagebrokerstats-how-to-set-loggingperiod