title: Persist message in ActiveMQ across server restart
body: <p>I am learning Spring Integration JMS. Since ActiveMQ is a message broker. I referring to project given here-> <a href="http://www.javaworld.com/article/2142107/spring-framework/open-source-java-projects-spring-integration.html?page=2#" rel="nofollow noreferrer">http://www.javaworld.com/article/2142107/spring-framework/open-source-java-projects-spring-integration.html?page=2#</a> </p>
  
  <p>But I want to know how can I persist message in ActiveMQ. I mean I started ActiveMQ then send request using REST client. I am calling <code>publishService.send( message );</code> in for loop for 50 times and and the receiver end I have sleep timer of 10 seconds. So that 50 messages gets queued and its start processing at 10 seconds interval.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Look at the screen shot below:</p>
  
  <p>It says 50 messages enqueued and 5 of them have been dequeued.</p>
  
  <p><a href="https://i.stack.imgur.com/917AL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/917AL.png" alt="enter image description here"></a> </p>
  
  <p>But then in between I stopped ActiveMQ server and by the time it has consumed 5 messages out of 50 and then again restart it. </p>
  
  <p>But then I was expecting it to show remaining 45 in Messages Enqueued column. But I can see 0(see screenshot below) there and they all vanished after server restart without having to persist remaining 45 messages. How can I come over this problem ?</p>
  
  <p><a href="https://i.stack.imgur.com/4Ydlq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Ydlq.png" alt="enter image description here"></a></p>
  
  <p>Please have a look at the configuration below:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:int="http://www.springframework.org/schema/integration"
         xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
         xmlns:oxm="http://www.springframework.org/schema/oxm"
         xmlns:int-jme="http://www.springframework.org/schema/integration"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                  http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
                  http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
                  http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"&gt;
  
  
      &lt;!-- Component scan to find all Spring components --&gt;
      &lt;context:component-scan base-package="com.geekcap.springintegrationexample" /&gt;
  
      &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
          &lt;property name="order" value="1" /&gt;
          &lt;property name="messageConverters"&gt;
              &lt;list&gt;
                  &lt;!-- Default converters --&gt;
                  &lt;bean class="org.springframework.http.converter.StringHttpMessageConverter"/&gt;
                  &lt;bean class="org.springframework.http.converter.FormHttpMessageConverter"/&gt;
                  &lt;bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /&gt;
                  &lt;bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/&gt;
                  &lt;bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/&gt;
                  &lt;bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Define a channel to communicate out to a JMS Destination --&gt;
      &lt;int:channel id="topicChannel"/&gt;
  
      &lt;!-- Define the ActiveMQ connection factory --&gt;
      &lt;bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory"&gt;
          &lt;property name="brokerURL" value="tcp://localhost:61616"/&gt;
      &lt;/bean&gt;
  
      &lt;!--
          Define an adaptor that route topicChannel messages to the myTopic topic; the outbound-channel-adapter
          automagically fines the configured connectionFactory bean (by naming convention
        --&gt;
      &lt;int-jms:outbound-channel-adapter channel="topicChannel"
                                        destination-name="topic.myTopic"
                                        pub-sub-domain="true" /&gt;
  
      &lt;!-- Create a channel for a listener that will consume messages--&gt;
      &lt;int:channel id="listenerChannel" /&gt;
  
      &lt;int-jms:message-driven-channel-adapter id="messageDrivenAdapter"
                                              channel="getPayloadChannel"
                                              destination-name="topic.myTopic"
                                              pub-sub-domain="true" /&gt;
  
      &lt;int:service-activator input-channel="listenerChannel" ref="messageListenerImpl" method="processMessage" /&gt;
  
      &lt;int:channel id="getPayloadChannel" /&gt;
  
      &lt;int:service-activator input-channel="getPayloadChannel" output-channel="listenerChannel" ref="retrievePayloadServiceImpl" method="getPayload" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Also please see the code:</p>
  
  <p>Controller from where I am sending message in for loop at once:</p>
  
  <pre><code>@Controller
  public class MessageController
  {
      @Autowired
      private PublishService publishService;
  
      @RequestMapping( value = "/message", method = RequestMethod.POST )
      @ResponseBody
      public void postMessage( @RequestBody com.geekcap.springintegrationexample.model.Message message, HttpServletResponse response )
      {
          for(int i = 0; i &lt; 50; i++){
              // Publish the message
              publishService.send( message );
  
              // Set the status to 201 because we created a new message
              response.setStatus( HttpStatus.CREATED.value() );
          }
      }
  
  }
  </code></pre>
  
  <p>Consumer code to which I have applied timer:</p>
  
  <pre><code>@Service
  public class MessageListenerImpl
  {
      private static final Logger logger = Logger.getLogger( MessageListenerImpl.class );
  
      public void processMessage( String message )
      {
          try {
              Thread.sleep(10000);
              logger.info( "Received message: " + message );
              System.out.println( "MessageListener::::::Received message: " + message );
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>
  
  <p>Further by searching more I found <a href="http://activemq.apache.org/what-is-the-difference-between-persistent-and-non-persistent-delivery.html" rel="nofollow noreferrer">here</a> that <strong>As per the JMS specification, the default delivery mode is persistent.</strong> But in my case it does not seems to be worked.</p>
  
  <p>Please help me to have right configuration in place so that messages can be persist across broker failure. </p>