Link to home
Start Free TrialLog in
Avatar of SarahDeng
SarahDeng

asked on

How to start/stop ActiveMQ listener programatically?

Hi Experts,
I have ActiveMQ 5.4 and Spring3.1. This MessageListener will be kicked off once there is a message dropped in the queue. Because this MessageListener will insert a large amount of data, I wonder if there is a way to stop this Listener, and restart it once the a large amount of data inserted into DB.
How to stop/restart the listener programatically? Thanks!

public class ExampleListener implements MessageListener {
      public void onMessage(Message message) {
                        insert a LARGE amount of data into DB table using Hibernate.
                        I       
      }
}



This is Spring ApplicationContext.xml

<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
      <constructor-arg>
      <value>HelloWorldQueue</value>
      </constructor-arg>
</bean>
 <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
 <property name="connectionFactory">
  <bean class="org.apache.activemq.ActiveMQConnectionFactory">
 <property name="brokerURL">
<value>tcp://localhost:61616</value>
 </property>
 </bean>
</property>
</bean>
      <bean id="myJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
          <property name="connectionFactory" ref="jmsFactory"/>
      </bean>      
      <!-- this is the Message Driven POJO (MDP) -->
      <bean id="messageListener" class="com.sprint.cto.sp.helloworld.ExampleListener" />
      
      <!-- and this is the message listener container -->
      <bean id="jmsContainer"
      class="org.springframework.jms.listener.DefaultMessageListenerContainer">
            <property name="connectionFactory" ref="jmsFactory"/>
            <property name="destination" ref="destination"/>
            <property name="messageListener" ref="messageListener" />
      </bean>
      
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ajaycv
ajaycv

Have you tried using the start method and the stop method on the DefaultMessageListenerContainer?

The DefaultMessageListenerContainer (DMLC) is another wonderful convenience class that is part of the Spring Framework's JMS package. As you can see in the Javadoc, the DMLC is not a single class, but a well-abstracted hierarchy for the purpose of receiving messages. The reason for this is that the DMLC takes its inspiration from Message Driven Beans (MDB).

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jms/listener/DefaultMessageListenerContainer.html
Avatar of SarahDeng

ASKER

Thanks! The question is well answered!
ajaycv,
 I want to learn more about how to stop/restart DMLC, do you have any example I can use? Thanks!
Do I need to configure "DefaultMessageListenerContainer " or other connectionFactory as such to work well in the use case (it would insert a large data into DB, it takes a longer time to finish)? There is Another message Listener listens to another queue will remove some records from this SAME table.
If I use the default configuration I may get timeout or exception in this case.
Please comment.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!!