INT-1815 removed size() method in AbstractDispatcher

This commit is contained in:
Oleg Zhurakousky
2011-03-02 09:32:29 -05:00
parent f085a094c8
commit 1514a6e64a
3 changed files with 21 additions and 24 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.integration.channel;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.MessageHandler;
@@ -32,20 +34,27 @@ import org.springframework.util.Assert;
* @author Oleg Zhurakousky
*/
public abstract class AbstractSubscribableChannel extends AbstractMessageChannel implements SubscribableChannel {
private final AtomicInteger handlers = new AtomicInteger();
public boolean subscribe(MessageHandler handler) {
MessageDispatcher dispatcher = this.getRequiredDispatcher();
if (dispatcher instanceof UnicastingDispatcher){
if (((UnicastingDispatcher) dispatcher).size() > 0){
String message = "Point-to-Point channel '" + this.getComponentName() + "' has more then 1 subscriber. " +
int counter = handlers.incrementAndGet();
if (counter > 1){
String message = "Point-to-Point channel '" + this.getComponentName() + "' has more then 1 subscriber - (" + counter + "). " +
"If load balancing strategy is provided, messages will be dispatched following its rules.";
this.logger.info(message);
}
}
}
return dispatcher.addHandler(handler);
}
public boolean unsubscribe(MessageHandler handle) {
if (this.getRequiredDispatcher() instanceof UnicastingDispatcher){
handlers.getAndDecrement();
}
return this.getRequiredDispatcher().removeHandler(handle);
}

View File

@@ -76,8 +76,4 @@ public abstract class AbstractDispatcher implements MessageDispatcher {
public String toString() {
return this.getClass().getSimpleName() + " with handlers: " + this.handlers.toString();
}
public int size(){
return this.handlers.toArray().length;
}
}

View File

@@ -24,8 +24,8 @@ import java.util.concurrent.Executor;
import org.apache.commons.logging.Log;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.core.MessageHandler;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;
@@ -41,9 +41,7 @@ public class P2pChannelTests {
final DirectChannel channel = new DirectChannel();
channel.setBeanName("directChannel");
String logMessage = "Point-to-Point channel '" + channel.getComponentName() + "' has more then 1 subscriber. " +
"If load balancing strategy is provided, messages will be dispatched following its rules.";
final Log logger = mock(Log.class);
ReflectionUtils.doWithFields(AbstractMessageChannel.class, new FieldCallback() {
public void doWith(Field field) throws IllegalArgumentException,
@@ -56,9 +54,9 @@ public class P2pChannelTests {
});
channel.subscribe(mock(MessageHandler.class));
verify(logger, times(0)).info(logMessage);
verify(logger, times(0)).info(Mockito.anyString());
channel.subscribe(mock(MessageHandler.class));
verify(logger, times(1)).info(logMessage);
verify(logger, times(1)).info(Mockito.anyString());
}
@Test
@@ -66,9 +64,6 @@ public class P2pChannelTests {
final ExecutorChannel channel = new ExecutorChannel(mock(Executor.class));
channel.setBeanName("executorChannel");
String logMessage = "Point-to-Point channel '" + channel.getComponentName() + "' has more then 1 subscriber. " +
"If load balancing strategy is provided, messages will be dispatched following its rules.";
final Log logger = mock(Log.class);
ReflectionUtils.doWithFields(AbstractMessageChannel.class, new FieldCallback() {
@@ -81,9 +76,9 @@ public class P2pChannelTests {
}
});
channel.subscribe(mock(MessageHandler.class));
verify(logger, times(0)).info(logMessage);
verify(logger, times(0)).info(Mockito.anyString());
channel.subscribe(mock(MessageHandler.class));
verify(logger, times(1)).info(logMessage);
verify(logger, times(1)).info(Mockito.anyString());
}
@Test
@@ -91,9 +86,6 @@ public class P2pChannelTests {
final PublishSubscribeChannel channel = new PublishSubscribeChannel();
channel.setBeanName("pubSubChannel");
String logMessage = "Point-to-Point channel '" + channel.getComponentName() + "' has more then 1 subscriber. " +
"If load balancing strategy is provided, messages will be dispatched following its rules.";
final Log logger = mock(Log.class);
ReflectionUtils.doWithFields(AbstractMessageChannel.class, new FieldCallback() {
@@ -106,8 +98,8 @@ public class P2pChannelTests {
}
});
channel.subscribe(mock(MessageHandler.class));
verify(logger, times(0)).info(logMessage);
verify(logger, times(0)).info(Mockito.anyString());
channel.subscribe(mock(MessageHandler.class));
verify(logger, times(0)).info(logMessage);
verify(logger, times(0)).info(Mockito.anyString());
}
}