首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring在JUNIT中创建了额外的线程

Spring在JUNIT中创建了额外的线程
EN

Stack Overflow用户
提问于 2016-02-18 11:48:38
回答 1查看 1.4K关注 0票数 2

我的Springconfig.xml中有简单的任务执行器,并附加了几条链。

代码语言:javascript
复制
      <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代码段

代码语言:javascript
复制
@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

添加应用程序配置信息:

代码语言:javascript
复制
@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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-18 22:05:13

在这里,您可以找到简单的测试用例来演示正确的行为:

代码语言:javascript
复制
<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)"/>
代码语言:javascript
复制
@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();
}

结果如下:

代码语言:javascript
复制
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: 9
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35480640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档