我的Springconfig.xml中有简单的任务执行器,并附加了几条链。
<channel id="inputChannel" />
<task:executor id="threadPoolExecutor" pool-size="2" />
<publish-subscribe-channel id="multiCastChannel"
task-executor="threadPoolExecutor" />
<chain input-channel="inputChannel"
output-channel="multiCastChannel">
<json-to-object-transformer
type="com.company.integration.domain.DomainObject" />
<service-activator ref="validator"
method="validate" />
</chain>
<chain input-channel="multiCastChannel"
output-channel="inventoryAdjustmentOutputChannelOne">
<service-activator ref="adapterOne"
method="buildOutputMessageOne" />
</chain>
<chain input-channel="multiCastChannel"
output-channel="inventoryAdjustmentOutputChannelTwo">
<service-activator ref="adapterTwo"
method="buildOutputMessageTwo" />
</chain>当消息发布到"inputChannel“并在处理后发送到"multuCastChannel”时,就会创建两个线程,而不会出现以下问题
threadPoolExecutor-1 threadPoolExecutor-2
每输入一条消息,这两条信息只创建一次,这很好。但当我试着用JUnit做同样的测试时.每个"multiCastChannel“链执行两次.意味着链中的服务激活器(adapterOne,adapterTwo)正在调用两次,每个链...which都是有线的。
知道为什么JUnit会有这种行为吗?
下面是Junit代码段
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:configuration/spring-config.xml"})
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@PropertySource("classpath:application.properties")
@SuppressWarnings("unchecked")
@WebAppConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class InventoryAdjustmentMessageTest {
@Autowired
private DirectChannel inputChannel;
@Test
public void testTaskShed()
throws IOException, InterruptedException, JMSException {
String validInput = setup("valid-message.txt");
Message<String> inputMessage = TextMessageUtil.createNewGenericMessage(validInput);
inputChannel.send(inputMessage);
Thread.sleep(5000);}
Spring集成版本: 4.1.6
添加应用程序配置信息:
@Import({ HarnessConfiguration.class, LocalConfiguration.class, MongoDbConfiguration.class, WebConfiguration.class,
WebsphereMQJMSConfiguration.class })
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.abc.inventory.adjustment.integration.service",
"com.abc.inventory.adjustment.integration.domain" }, useDefaultFilters = false, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ApplicationConfiguration.class) }, includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, value = { Controller.class,
Component.class }) })
@ImportResource({ "classpath:configuration/spring-config.xml", "classpath:configuration/spring-adapters.xml" })
@EnableMongoRepositories("com.abc.inventory.adjustment.integration.service.audit.repository")
@EnableAutoConfiguration
@EnableMongoAuditing
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {
//}
添加调试快照

-Tej
发布于 2016-02-18 22:05:13
在这里,您可以找到简单的测试用例来演示正确的行为:
<task:executor id="executor" pool-size="2"/>
<publish-subscribe-channel id="pubSubChannel" task-executor="executor" />
<service-activator input-channel="pubSubChannel" expression="T(System).out.println(payload)"/>
<service-activator input-channel="pubSubChannel" expression="T(System).out.println('foo: ' + payload)"/>@Test
public void testPubSubChannel() throws InterruptedException {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("pubSubChannelConfig.xml", getClass());
MessageChannel channel = (MessageChannel) context.getBean("pubSubChannel");
for (int i = 0; i < 10; i++) {
channel.send(new GenericMessage<Integer>(i));
}
Thread.sleep(10000);
context.close();
}结果如下:
foo: 0
0
foo: 1
1
2
foo: 2
3
foo: 3
4
foo: 4
5
foo: 5
6
foo: 6
7
foo: 7
8
foo: 8
9
foo: 9https://stackoverflow.com/questions/35480640
复制相似问题