下面列出了怎么用org.springframework.messaging.simp.config.ChannelRegistration的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.setInterceptors(new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
System.out.println("configureClientInboundChannel");
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
User user = (User) accessor.getSessionAttributes().get("user");
return super.preSend(message, channel);
}
});
}
/**
* 通道
*
* @param registration
*/
@Override
public void configureClientInboundChannel ( ChannelRegistration registration ) {
if ( Objects.nonNull( channelInterceptor ) ) {
registration.interceptors( channelInterceptor );
}
}
@Test
public void shouldRegisterInterceptors() {
final AuthChannelInterceptorAdapter authAdaptor = mock(AuthChannelInterceptorAdapter.class);
final WebSocketAuthenticationSecurityConfig webSocketAuthenticationSecurityConfig = spy(new WebSocketAuthenticationSecurityConfig(authAdaptor));
final ChannelRegistration registration = mock(ChannelRegistration.class);
webSocketAuthenticationSecurityConfig.configureClientInboundChannel(registration);
verify(registration, times(1)).interceptors(any());
verify(webSocketAuthenticationSecurityConfig, times(1)).createChannelInterceptors();
}
@Override
public void configureClientOutboundChannel(final ChannelRegistration registration)
{
//
// IMPORTANT: make sure we are using only one thread for sending outbound messages.
// If not, it might be that the messages will not be sent in the right order,
// and that's important for things like WS notifications API.
// ( thanks to http://stackoverflow.com/questions/29689838/sockjs-receive-stomp-messages-from-spring-websocket-out-of-order )
registration.taskExecutor()
.corePoolSize(1)
.maxPoolSize(1);
}
@Override
public void configureClientInboundChannel(final ChannelRegistration registration)
{
registration.setInterceptors(new WebSocketChannelInterceptor());
// NOTE: atm we don't care if the inbound messages arrived in the right order
// When and If we would care we would restrict the taskExecutor()'s corePoolSize to ONE.
// see: configureClientOutboundChannel().
}
@Override
protected void configureClientInboundChannel(ChannelRegistration registration) {
for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {
configurer.configureClientInboundChannel(registration);
}
}
@Override
protected void configureClientOutboundChannel(ChannelRegistration registration) {
for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {
configurer.configureClientOutboundChannel(registration);
}
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
}
@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
}
@Override
protected void configureClientInboundChannel(ChannelRegistration registration) {
for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {
configurer.configureClientInboundChannel(registration);
}
}
@Override
protected void configureClientOutboundChannel(ChannelRegistration registration) {
for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {
configurer.configureClientOutboundChannel(registration);
}
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
}
@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
// 注册一个自定义的拦截器
registration.interceptors(new MyChannelInterceptorAdapter());
}
@Override
public void configureClientInboundChannel(ChannelRegistration channelRegistration) {
}
@Override
public void configureClientOutboundChannel(ChannelRegistration channelRegistration) {
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
ThreadPoolTaskExecutor executor = ThreadHelper.createTaskExecutor(50, 50, 50, "ws-inbound-");
registration.taskExecutor(executor);
}
@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
ThreadPoolTaskExecutor executor = ThreadHelper.createTaskExecutor(50, 50, 50, "ws-outbound-");
registration.taskExecutor(executor);
}
@Override
public void configureClientInboundChannel(final ChannelRegistration registration) {
registration.interceptors(this.createChannelInterceptors());
}
@Override
public void configureClientInboundChannel(final ChannelRegistration registration) {
registration.interceptors(this.authChannelInterceptorAdapter);
}
@Override
protected void configureClientInboundChannel(ChannelRegistration registration) {
for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {
configurer.configureClientInboundChannel(registration);
}
}
@Override
protected void configureClientOutboundChannel(ChannelRegistration registration) {
for (WebSocketMessageBrokerConfigurer configurer : this.configurers) {
configurer.configureClientOutboundChannel(registration);
}
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
}
@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.setInterceptors(interceptor);
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
}
@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
registration.taskExecutor().corePoolSize(4).maxPoolSize(10);
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.taskExecutor().corePoolSize(Runtime.getRuntime().availableProcessors() *4);
}
@Override
//Increase number of threads for slow clients
public void configureClientOutboundChannel(ChannelRegistration registration) {
registration.taskExecutor().corePoolSize(Runtime.getRuntime().availableProcessors() *4);
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
}