Results 1 to 3 of 3

Thread: How to run DefaultMessageListenerContainer on the 'main' thread

  1. #1
    Join Date
    May 2012
    Posts
    2

    Default How to run DefaultMessageListenerContainer on the 'main' thread

    I have a case where i want to run DefaultMessageListenerContainer in the same 'main' thread. Right now it uses SimpleAsyncTaskExecutor which spawns new thread every time it receives a message.

    We have a test case which connects to different distributed system and do the processing and in the end it asserts few things. As DefaultMessageListenerContainer runs in seperate thread, main thread returns and start executing assertions before DefaultMessageListenerContainer can complete. This leads to failure of the test case. As work around we have made the main thread to sleep for few seconds.

    Sample config

    <int-jms:message-driven-channel-adapter
    id="mq.txbus.publisher.channel.adapter"
    container="defaultMessageListenerContainer"
    channel="inbound.endpoint.publisher"
    acknowledge="transacted"
    extract-payload="true"/>

    <beans:bean id="defaultMessageListenerContainer" class="org.springframework.jms.listener.DefaultMes sageListenerContainer">
    <beans:property name="connectionFactory" ref="mockConnectionFactory"/>
    <beans:property name="destination" ref="publisherToTxmQueue"/>
    <beans:property name="taskExecutor" ref="taskExecutor"/>
    <beans:property name="maxMessagesPerTask" value="10"/>
    <beans:property name="sessionTransacted" value="true"/>
    </beans:bean>

    <beans:bean id="taskExecutor" class="org.springframework.scheduling.timer.TimerT askExecutor"/>


    I am trying to use TimerTaskExecutor here because it creates single thread but that thread is seperate than main thread so problem is unresolved. I tried using SyncTaskExecutor but that does not work either (or may be i dint provide the correct property value?).

  2. #2
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    You can't have a MessageListener container running in the main thread, because it must run in the background (even if there is only one thread dedicated to that container). Otherwise, it would cause the main thread to hang completely (i.e. even the startup of an ApplicationContext would not be able to proceed once the lifecycle processing kicks in and the ML container is started).

    Can you provide the relevant code from your test case so we can possibly offer some suggestions?

  3. #3
    Join Date
    May 2012
    Posts
    2

    Default

    Thanks Mark! This makes sense. Basically it is a integration test where 5 different components are involved and communication is done over JMS. I have got some idea on how to implement this after reading Tomasz reply on below thread.
    http://stackoverflow.com/questions/1...he-main-thread

    This thread can be marked as closed.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •