INT-1176: added scope= attribute to channels and removed thread-local-channel

This commit is contained in:
David Syer
2010-06-17 11:07:37 +00:00
parent 49558ea82f
commit 8df7265abb
14 changed files with 169 additions and 316 deletions

View File

@@ -0,0 +1,8 @@
log4j.rootCategory=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%c{1}: %m%n
log4j.category.org.springframework.integration=WARN
log4j.category.org.springframework.integration.file=WARN

View File

@@ -20,12 +20,11 @@ import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.channel.ThreadLocalChannel;
import org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.core.Message;
@@ -46,7 +45,7 @@ public class DirectChannelSubscriptionTests {
private DirectChannel sourceChannel = new DirectChannel();
private ThreadLocalChannel targetChannel = new ThreadLocalChannel();
private PollableChannel targetChannel = new QueueChannel();
@Before

View File

@@ -1,118 +0,0 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.channel;
import static org.junit.Assert.assertEquals;
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 java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.core.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class ThreadLocalChannelTests {
@Before
public void clearThreadLocalQueue() {
ThreadLocalChannel channel = new ThreadLocalChannel();
Message<?> result = null;
do {
result = channel.receive(0);
} while (result != null);
}
@Test
public void testSendAndReceive() {
ThreadLocalChannel channel = new ThreadLocalChannel();
StringMessage message = new StringMessage("test");
assertNull(channel.receive());
assertTrue(channel.send(message));
Message<?> response = channel.receive();
assertNotNull(response);
assertEquals(response, message);
assertNull(channel.receive());
}
@Test
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<Message<?>> receivedMessages = new ArrayList<Message<?>>();
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());
}
@Test
public void multipleThreadLocalChannels() throws Exception {
final ThreadLocalChannel channel1 = new ThreadLocalChannel();
final ThreadLocalChannel channel2 = new ThreadLocalChannel();
channel1.send(new StringMessage("test-1.1"));
channel1.send(new StringMessage("test-1.2"));
channel1.send(new StringMessage("test-1.3"));
channel2.send(new StringMessage("test-2.1"));
channel2.send(new StringMessage("test-2.2"));
Executor otherThreadExecutor = Executors.newSingleThreadExecutor();
final List<Object> otherThreadResults = new ArrayList<Object>();
final CountDownLatch latch = new CountDownLatch(2);
otherThreadExecutor.execute(new Runnable() {
public void run() {
otherThreadResults.add(channel1.receive(0));
latch.countDown();
}
});
otherThreadExecutor.execute(new Runnable() {
public void run() {
otherThreadResults.add(channel2.receive(0));
latch.countDown();
}
});
latch.await(1, TimeUnit.SECONDS);
assertEquals(2, otherThreadResults.size());
assertNull(otherThreadResults.get(0));
assertNull(otherThreadResults.get(1));
assertEquals("test-1.1", channel1.receive(0).getPayload());
assertEquals("test-1.2", channel1.receive(0).getPayload());
assertEquals("test-1.3", channel1.receive(0).getPayload());
assertNull(channel1.receive(0));
assertEquals("test-2.1", channel2.receive(0).getPayload());
assertEquals("test-2.2", channel2.receive(0).getPayload());
assertNull(channel2.receive(0));
}
}

View File

@@ -1,20 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd">
<beans:beans xmlns="http://www.springframework.org/schema/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<thread-local-channel id="simpleChannel"/>
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer" xmlns="http://www.springframework.org/schema/beans">
<property name="scopes">
<map>
<entry key="thread" value="org.springframework.context.support.SimpleThreadScope" />
</map>
</property>
</bean>
<thread-local-channel id="channelWithInterceptor">
<channel id="simpleChannel" scope="thread">
<queue />
</channel>
<channel id="channelWithInterceptor" scope="thread">
<queue />
<interceptors>
<beans:ref bean="interceptor"/>
<beans:ref bean="interceptor" />
</interceptors>
</thread-local-channel>
</channel>
<beans:bean id="interceptor" class="org.springframework.integration.config.TestChannelInterceptor"/>
<beans:bean id="interceptor" class="org.springframework.integration.config.TestChannelInterceptor" />
</beans:beans>

View File

@@ -17,46 +17,99 @@
package org.springframework.integration.channel.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.channel.ThreadLocalChannel;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.config.TestChannelInterceptor;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.message.StringMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ThreadLocalChannelParserTests {
@Autowired @Qualifier("simpleChannel")
private MessageChannel simpleChannel;
private PollableChannel simpleChannel;
@Autowired @Qualifier("channelWithInterceptor")
private MessageChannel channelWithInterceptor;
private PollableChannel channelWithInterceptor;
@Autowired
private TestChannelInterceptor interceptor;
@Test
public void checkType() {
assertEquals(ThreadLocalChannel.class, simpleChannel.getClass());
public void testSendInAnotherThread() throws Exception {
simpleChannel.send(new StringMessage("test"));
Executor otherThreadExecutor = Executors.newSingleThreadExecutor();
final CountDownLatch latch = new CountDownLatch(1);
otherThreadExecutor.execute(new Runnable() {
public void run() {
simpleChannel.send(new StringMessage("crap"));
latch.countDown();
}
});
latch.await(1, TimeUnit.SECONDS);
assertEquals("test", simpleChannel.receive(10).getPayload());
// Message sent on another thread is not collected here
assertEquals(null, simpleChannel.receive(10));
}
@Test
public void verifyInterceptor() {
assertEquals(0, interceptor.getSendCount());
public void testReceiveInAnotherThread() throws Exception {
simpleChannel.send(new StringMessage("test-1.1"));
simpleChannel.send(new StringMessage("test-1.2"));
simpleChannel.send(new StringMessage("test-1.3"));
channelWithInterceptor.send(new StringMessage("test-2.1"));
channelWithInterceptor.send(new StringMessage("test-2.2"));
Executor otherThreadExecutor = Executors.newSingleThreadExecutor();
final List<Object> otherThreadResults = new ArrayList<Object>();
final CountDownLatch latch = new CountDownLatch(2);
otherThreadExecutor.execute(new Runnable() {
public void run() {
otherThreadResults.add(simpleChannel.receive(0));
latch.countDown();
}
});
otherThreadExecutor.execute(new Runnable() {
public void run() {
otherThreadResults.add(channelWithInterceptor.receive(0));
latch.countDown();
}
});
latch.await(1, TimeUnit.SECONDS);
assertEquals(2, otherThreadResults.size());
assertNull(otherThreadResults.get(0));
assertNull(otherThreadResults.get(1));
assertEquals("test-1.1", simpleChannel.receive(0).getPayload());
assertEquals("test-1.2", simpleChannel.receive(0).getPayload());
assertEquals("test-1.3", simpleChannel.receive(0).getPayload());
assertNull(simpleChannel.receive(0));
assertEquals("test-2.1", channelWithInterceptor.receive(0).getPayload());
assertEquals("test-2.2", channelWithInterceptor.receive(0).getPayload());
assertNull(channelWithInterceptor.receive(0));
}
@Test
public void testInterceptor() {
int before = interceptor.getSendCount();
channelWithInterceptor.send(new StringMessage("test"));
assertEquals(1, interceptor.getSendCount());
assertEquals(before+1, interceptor.getSendCount());
}
}