@ -31,6 +31,7 @@ import org.springframework.context.annotation.Bean;
@@ -31,6 +31,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration ;
import org.springframework.context.support.StaticApplicationContext ;
import org.springframework.messaging.Message ;
import org.springframework.messaging.MessageChannel ;
import org.springframework.messaging.MessageHandler ;
import org.springframework.messaging.converter.* ;
import org.springframework.messaging.handler.annotation.MessageMapping ;
@ -66,24 +67,30 @@ import static org.junit.Assert.*;
@@ -66,24 +67,30 @@ import static org.junit.Assert.*;
* /
public class MessageBrokerConfigurationTests {
private AnnotationConfigApplicationContext simpleContext ;
private AnnotationConfigApplicationContext simpleBroker Context ;
private AnnotationConfigApplicationContext brokerRelayContext ;
private AnnotationConfigApplicationContext defaultContext ;
private AnnotationConfigApplicationContext customChannelContext ;
@Before
public void setupOnce ( ) {
this . simpleContext = new AnnotationConfigApplicationContext ( ) ;
this . simpleContext . register ( SimpleConfig . class ) ;
this . simpleContext . refresh ( ) ;
this . simpleBroker Context = new AnnotationConfigApplicationContext ( ) ;
this . simpleBroker Context . register ( SimpleBroker Config . class ) ;
this . simpleBroker Context . refresh ( ) ;
this . brokerRelayContext = new AnnotationConfigApplicationContext ( ) ;
this . brokerRelayContext . register ( BrokerRelayConfig . class ) ;
this . brokerRelayContext . refresh ( ) ;
this . defaultContext = new AnnotationConfigApplicationContext ( ) ;
this . defaultContext . register ( DefaultConfig . class ) ;
this . defaultContext . refresh ( ) ;
this . customChannelContext = new AnnotationConfigApplicationContext ( ) ;
this . customChannelContext . register ( CustomChannelConfig . class ) ;
this . customChannelContext . refresh ( ) ;
@ -93,13 +100,13 @@ public class MessageBrokerConfigurationTests {
@@ -93,13 +100,13 @@ public class MessageBrokerConfigurationTests {
@Test
public void clientInboundChannel ( ) {
TestChannel channel = this . simpleContext . getBean ( "clientInboundChannel" , TestChannel . class ) ;
TestChannel channel = this . simpleBroker Context . getBean ( "clientInboundChannel" , TestChannel . class ) ;
Set < MessageHandler > handlers = channel . getSubscribers ( ) ;
assertEquals ( 3 , handlers . size ( ) ) ;
assertTrue ( handlers . contains ( simpleContext . getBean ( SimpAnnotationMethodMessageHandler . class ) ) ) ;
assertTrue ( handlers . contains ( simpleContext . getBean ( UserDestinationMessageHandler . class ) ) ) ;
assertTrue ( handlers . contains ( simpleContext . getBean ( SimpleBrokerMessageHandler . class ) ) ) ;
assertTrue ( handlers . contains ( simpleBroker Context . getBean ( SimpAnnotationMethodMessageHandler . class ) ) ) ;
assertTrue ( handlers . contains ( simpleBroker Context . getBean ( UserDestinationMessageHandler . class ) ) ) ;
assertTrue ( handlers . contains ( simpleBroker Context . getBean ( SimpleBrokerMessageHandler . class ) ) ) ;
}
@Test
@ -130,8 +137,8 @@ public class MessageBrokerConfigurationTests {
@@ -130,8 +137,8 @@ public class MessageBrokerConfigurationTests {
@Test
public void clientOutboundChannelUsedByAnnotatedMethod ( ) {
TestChannel channel = this . simpleContext . getBean ( "clientOutboundChannel" , TestChannel . class ) ;
SimpAnnotationMethodMessageHandler messageHandler = this . simpleContext . getBean ( SimpAnnotationMethodMessageHandler . class ) ;
TestChannel channel = this . simpleBroker Context . getBean ( "clientOutboundChannel" , TestChannel . class ) ;
SimpAnnotationMethodMessageHandler messageHandler = this . simpleBroker Context . getBean ( SimpAnnotationMethodMessageHandler . class ) ;
StompHeaderAccessor headers = StompHeaderAccessor . create ( StompCommand . SUBSCRIBE ) ;
headers . setSessionId ( "sess1" ) ;
@ -151,8 +158,8 @@ public class MessageBrokerConfigurationTests {
@@ -151,8 +158,8 @@ public class MessageBrokerConfigurationTests {
@Test
public void clientOutboundChannelUsedBySimpleBroker ( ) {
TestChannel channel = this . simpleContext . getBean ( "clientOutboundChannel" , TestChannel . class ) ;
SimpleBrokerMessageHandler broker = this . simpleContext . getBean ( SimpleBrokerMessageHandler . class ) ;
TestChannel channel = this . simpleBroker Context . getBean ( "clientOutboundChannel" , TestChannel . class ) ;
SimpleBrokerMessageHandler broker = this . simpleBroker Context . getBean ( SimpleBrokerMessageHandler . class ) ;
StompHeaderAccessor headers = StompHeaderAccessor . create ( StompCommand . SUBSCRIBE ) ;
headers . setSessionId ( "sess1" ) ;
@ -197,12 +204,14 @@ public class MessageBrokerConfigurationTests {
@@ -197,12 +204,14 @@ public class MessageBrokerConfigurationTests {
@Test
public void brokerChannel ( ) {
TestChannel channel = this . simpleContext . getBean ( "brokerChannel" , TestChannel . class ) ;
TestChannel channel = this . simpleBroker Context . getBean ( "brokerChannel" , TestChannel . class ) ;
Set < MessageHandler > handlers = channel . getSubscribers ( ) ;
assertEquals ( 2 , handlers . size ( ) ) ;
assertTrue ( handlers . contains ( simpleContext . getBean ( UserDestinationMessageHandler . class ) ) ) ;
assertTrue ( handlers . contains ( simpleContext . getBean ( SimpleBrokerMessageHandler . class ) ) ) ;
assertTrue ( handlers . contains ( simpleBrokerContext . getBean ( UserDestinationMessageHandler . class ) ) ) ;
assertTrue ( handlers . contains ( simpleBrokerContext . getBean ( SimpleBrokerMessageHandler . class ) ) ) ;
assertNull ( channel . getExecutor ( ) ) ;
}
@Test
@ -217,8 +226,9 @@ public class MessageBrokerConfigurationTests {
@@ -217,8 +226,9 @@ public class MessageBrokerConfigurationTests {
@Test
public void brokerChannelUsedByAnnotatedMethod ( ) {
TestChannel channel = this . simpleContext . getBean ( "brokerChannel" , TestChannel . class ) ;
SimpAnnotationMethodMessageHandler messageHandler = this . simpleContext . getBean ( SimpAnnotationMethodMessageHandler . class ) ;
TestChannel channel = this . simpleBrokerContext . getBean ( "brokerChannel" , TestChannel . class ) ;
SimpAnnotationMethodMessageHandler messageHandler =
this . simpleBrokerContext . getBean ( SimpAnnotationMethodMessageHandler . class ) ;
StompHeaderAccessor headers = StompHeaderAccessor . create ( StompCommand . SEND ) ;
headers . setDestination ( "/foo" ) ;
@ -236,10 +246,10 @@ public class MessageBrokerConfigurationTests {
@@ -236,10 +246,10 @@ public class MessageBrokerConfigurationTests {
@Test
public void brokerChannelUsedByUserDestinationMessageHandler ( ) {
TestChannel channel = this . simpleContext . getBean ( "brokerChannel" , TestChannel . class ) ;
UserDestinationMessageHandler messageHandler = this . simpleContext . getBean ( UserDestinationMessageHandler . class ) ;
TestChannel channel = this . simpleBroker Context . getBean ( "brokerChannel" , TestChannel . class ) ;
UserDestinationMessageHandler messageHandler = this . simpleBroker Context . getBean ( UserDestinationMessageHandler . class ) ;
this . simpleContext . getBean ( UserSessionRegistry . class ) . registerSessionId ( "joe" , "s1" ) ;
this . simpleBroker Context . getBean ( UserSessionRegistry . class ) . registerSessionId ( "joe" , "s1" ) ;
StompHeaderAccessor headers = StompHeaderAccessor . create ( StompCommand . SEND ) ;
headers . setDestination ( "/user/joe/foo" ) ;
@ -285,6 +295,24 @@ public class MessageBrokerConfigurationTests {
@@ -285,6 +295,24 @@ public class MessageBrokerConfigurationTests {
assertEquals ( MimeTypeUtils . APPLICATION_JSON , ( ( DefaultContentTypeResolver ) resolver ) . getDefaultMimeType ( ) ) ;
}
@Test
public void threadPoolSizeDefault ( ) {
String name = "clientInboundChannelExecutor" ;
ThreadPoolTaskExecutor executor = this . defaultContext . getBean ( name , ThreadPoolTaskExecutor . class ) ;
assertEquals ( Runtime . getRuntime ( ) . availableProcessors ( ) * 2 , executor . getCorePoolSize ( ) ) ;
// No way to verify queue capacity
name = "clientOutboundChannelExecutor" ;
executor = this . defaultContext . getBean ( name , ThreadPoolTaskExecutor . class ) ;
assertEquals ( Runtime . getRuntime ( ) . availableProcessors ( ) * 2 , executor . getCorePoolSize ( ) ) ;
name = "brokerChannelExecutor" ;
executor = this . defaultContext . getBean ( name , ThreadPoolTaskExecutor . class ) ;
assertEquals ( 0 , executor . getCorePoolSize ( ) ) ;
assertEquals ( 1 , executor . getMaxPoolSize ( ) ) ;
}
@Test
public void configureMessageConvertersCustom ( ) {
final MessageConverter testConverter = Mockito . mock ( MessageConverter . class ) ;
@ -360,7 +388,7 @@ public class MessageBrokerConfigurationTests {
@@ -360,7 +388,7 @@ public class MessageBrokerConfigurationTests {
@Test
public void simpValidatorInjected ( ) {
SimpAnnotationMethodMessageHandler messageHandler =
this . simpleContext . getBean ( SimpAnnotationMethodMessageHandler . class ) ;
this . simpleBroker Context . getBean ( SimpAnnotationMethodMessageHandler . class ) ;
assertThat ( messageHandler . getValidator ( ) , Matchers . notNullValue ( Validator . class ) ) ;
}
@ -381,8 +409,9 @@ public class MessageBrokerConfigurationTests {
@@ -381,8 +409,9 @@ public class MessageBrokerConfigurationTests {
}
}
@Configuration
static class SimpleConfig extends AbstractMessageBrokerConfiguration {
static class SimpleBroker Config extends AbstractMessageBrokerConfiguration {
@Bean
public TestController subscriptionController ( ) {
@ -409,7 +438,7 @@ public class MessageBrokerConfigurationTests {
@@ -409,7 +438,7 @@ public class MessageBrokerConfigurationTests {
}
@Configuration
static class BrokerRelayConfig extends SimpleConfig {
static class BrokerRelayConfig extends SimpleBroker Config {
@Override
public void configureMessageBroker ( MessageBrokerRegistry registry ) {
@ -417,6 +446,10 @@ public class MessageBrokerConfigurationTests {
@@ -417,6 +446,10 @@ public class MessageBrokerConfigurationTests {
}
}
@Configuration
static class DefaultConfig extends AbstractMessageBrokerConfiguration {
}
@Configuration
static class CustomChannelConfig extends AbstractMessageBrokerConfiguration {