title: Spring JMS - Draining Topic on a Timer
body: <p>I'm fairly new to Spring JMS, and I've found lots of documentation and examples at the Spring site and elsewhere, but my use case doesn't seem to be described anywhere, or at least in a way I can understand. I hope you might be able to help.</p>
  
  <p>I would like to create a publisher of a topic and several durable subscribers to that topic. I'm working on the first subscriber now, and it is intended to run hourly (on a timer) and drain the topic of messages and process them all at once (i.e. to send an email summarizing all messages).</p>
  
  <p>I do not know how to configure this setup in Spring, although I feel like this should be easy. Advice would be tremendously helpful.</p>
  
  <p>My plan, such as it is, was to have the timer invoke the "processBatch" method, which would call receiveAndConvert() in a loop until it timed out, building up its list of messages.</p>
  
  <p>This doesn't seem to work, though, because the consumer isn't really subscribed to the topic. Certainly not before it's run, and potentially not afterward.</p>
  
  <p>How can I configure this using Spring and/or direct ActiveMQ?</p>
  
  <p>I'm not sure where I ended up with my XML is a useful place for this discussion to start, but I'll provide it in case it is helpful:</p>
  
  <pre><code>&lt;beans&gt;
      &lt;!-- some unrelated beans --&gt;
  
      &lt;!-- my Active MQ connection factory --&gt;
      &lt;bean id="mqConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"&gt;
          &lt;property name="connectionFactory"&gt;
              &lt;bean class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
                  &lt;property name="brokerURL" value="vm://broker"/&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- my topic --&gt;
      &lt;amq:topic id="completionsTopic" physicalName="completions.topic"/&gt;
  
      &lt;!-- my subscriber --&gt;
      &lt;bean id="emailer" class="com.j128.Emailer"&gt;
          &lt;property name="jmsTemplate"&gt;
              &lt;bean class="org.springframework.jms.core.JmsTemplate"&gt;
                  &lt;property name="connectionFactory" ref="mqConnectionFactory"/&gt;
                  &lt;property name="defaultDestination" ref="completionsTopic"/&gt;
                  &lt;property name="receiveTimeout" value="2000"/&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- my scheduler and periodic call to the topic drainer --&gt;
      &lt;task:scheduler id="taskScheduler" pool-size="10"/&gt;
      &lt;task:scheduled-tasks&gt;
          &lt;!-- send emails hourly --&gt;
          &lt;task:scheduled ref="emailer" method="processBatch" cron="0 * * * *"/&gt;
      &lt;/task:scheduled-tasks&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>But I'm certain I fundamentally have the wrong strategy and that there's a simple way to configure this.</p>
  
  <p>Thank you for your assistance.</p>