From d634a4a423d1f3d562ea75c499c08d6335b83859 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Tue, 27 Apr 2010 14:19:04 +0000 Subject: [PATCH] INT-1101 removed list() and purge() methods from ThreadLocalChannel --- .../channel/ThreadLocalChannel.java | 38 +++---------------- .../channel/ThreadLocalChannelTests.java | 15 +++++--- 2 files changed, 15 insertions(+), 38 deletions(-) diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/channel/ThreadLocalChannel.java b/org.springframework.integration/src/main/java/org/springframework/integration/channel/ThreadLocalChannel.java index 7dcfcf61c4..0d6fa2f2da 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/channel/ThreadLocalChannel.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/channel/ThreadLocalChannel.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,19 +16,19 @@ package org.springframework.integration.channel; -import java.util.ArrayList; -import java.util.List; import java.util.Queue; import java.util.concurrent.LinkedBlockingQueue; import org.springframework.integration.core.Message; -import org.springframework.integration.selector.MessageSelector; /** * A channel implementation that stores messages in a thread-bound queue. In * other words, send() will put a message at the tail of the queue for the * current thread, and receive() will retrieve a message from the head of the - * queue. + * queue. Since, by definition, only one thread will interact with the queue + * at a time, the timeout values on send and receive have no effect. If there + * are no Messages in the queue, the receive operations will return a + * null value immediately, regardless of any timeout value. * * @author Dave Syer * @author Mark Fisher @@ -51,36 +51,10 @@ public class ThreadLocalChannel extends AbstractPollableChannel { return messageHolder.get().add(message); } - /** - * Remove and return any messages that are stored for the current thread. - */ - public List> clear() { - List> removedMessages = new ArrayList>(); - Message next = messageHolder.get().poll(); - while (next != null) { - removedMessages.add(next); - next = messageHolder.get().poll(); - } - return removedMessages; - } /** - * Remove and return any messages that are stored for the current thread - * and do not match the provided selector. + * The thread-bound Queue. */ - public List> purge(MessageSelector selector) { - List> removedMessages = new ArrayList>(); - Object[] allMessages = messageHolder.get().toArray(); - for (Object next : allMessages) { - Message message = (Message) next; - if (!selector.accept(message) && messageHolder.get().remove(message)) { - removedMessages.add(message); - } - } - return removedMessages; - } - - private static class ThreadLocalMessageHolder extends ThreadLocal>> { @Override diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/channel/ThreadLocalChannelTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/channel/ThreadLocalChannelTests.java index 8563ad8c5a..9a1795d757 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/channel/ThreadLocalChannelTests.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/channel/ThreadLocalChannelTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import java.util.ArrayList; import java.util.List; import org.junit.Test; @@ -46,17 +47,19 @@ public class ThreadLocalChannelTests { } @Test - public void testSendAndClear() { + public void testSendAndReceiveMultipleMessages() { ThreadLocalChannel channel = new ThreadLocalChannel(); StringMessage message1 = new StringMessage("test1"); StringMessage message2 = new StringMessage("test2"); assertNull(channel.receive()); assertTrue(channel.send(message1)); assertTrue(channel.send(message2)); - List> clearedMessages = channel.clear(); - assertEquals(2, clearedMessages.size()); - assertEquals(message1, clearedMessages.get(0)); - assertEquals(message2, clearedMessages.get(1)); + List> receivedMessages = new ArrayList>(); + receivedMessages.add(channel.receive(0)); + receivedMessages.add(channel.receive(0)); + assertEquals(2, receivedMessages.size()); + assertEquals(message1, receivedMessages.get(0)); + assertEquals(message2, receivedMessages.get(1)); assertNull(channel.receive()); }