From 1514a6e64acd5fd897a0725d2d581e9a470c406d Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 2 Mar 2011 09:32:29 -0500 Subject: [PATCH] INT-1815 removed size() method in AbstractDispatcher --- .../channel/AbstractSubscribableChannel.java | 17 +++++++++---- .../dispatcher/AbstractDispatcher.java | 4 ---- .../integration/channel/P2pChannelTests.java | 24 +++++++------------ 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractSubscribableChannel.java b/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractSubscribableChannel.java index 7d40249cb1..93f5bd41a7 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractSubscribableChannel.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractSubscribableChannel.java @@ -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); } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/AbstractDispatcher.java b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/AbstractDispatcher.java index a5170dda88..d57ffec6b2 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/AbstractDispatcher.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/AbstractDispatcher.java @@ -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; - } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/P2pChannelTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/P2pChannelTests.java index 1497dc8428..623514864c 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/P2pChannelTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/P2pChannelTests.java @@ -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()); } }