title: Spring Integration: Persistent and transactional QueueChannel
body: <p>In Spring Integration we have a <strong>Setup</strong> that looks something like this:</p>
  
  <pre><code>                                                     ---&gt; 
                                                       ---&gt; 
  (dispatcher) Messages --&gt; Gateway ----&gt; QueueChannel ---&gt; MessageHandler (worker)
                                                       ---&gt; 
                                                       ---&gt; 
  </code></pre>
  
  <p>So we have one Dispatcher Thread that takes Messages from a MQTT-Broker and forwards them into the Queue. The Poller for the Queue is provided with a TaskExecuter, so the Consumer is multithreaded. 
  We managed to implement all the functionalities. So the just described setup is already implemented. </p>
  
  <p>Now <strong>to guarantee no data loss</strong> we want to make two things:</p>
  
  <p><strong>1.:</strong>
  We want our queue to persist the data, so when the Programm shuts down ungracefully, all the data in the queue will still be there.
  This also worked for us, we are using MongoDB as a database because we read somewhere in your docs that this is the recommended way to do it.</p>
  
  <p><strong>2.:</strong>
  The second thing we want to assure is that the worker threads are working transactional. So only if the worker threads return correctly the messages will permanently be deleted from the queue (and therefore the persistent MessageStore). If the program shuts down during the processing of a message (by the worker thread) the message will still be in the queue at the next startup.
  Also if the worker, for example, thows an exception during the processing of the message, it will be put back into the queue. </p>
  
  <p><strong>Our implementation:</strong></p>
  
  <p>As explained before, the basic setup of the program is already implemented. We then extended the basic implementation with a message store implementation for the queue.</p>
  
  <p><em>QueueChannel:</em></p>
  
  <pre><code>@Bean
  public PollableChannel inputChannel(BasicMessageGroupStore mongoDbChannelMessageStore) {
      return new QueueChannel(new MessageGroupQueue(mongoDbChannelMessageStore, "inputChannel"));
  }
  </code></pre>
  
  <p><em>backed by a Messagestore:</em></p>
  
  <pre><code>@Bean
  public BasicMessageGroupStore mongoDbChannelMessageStore(MongoDbFactory mongoDbFactory) {
      MongoDbChannelMessageStore store = new MongoDbChannelMessageStore(mongoDbFactory);
      store.setPriorityEnabled(true);
      return store;
  }
  </code></pre>
  
  <p><em>the matching Poller</em>:</p>
  
  <pre><code>@Bean(name = PollerMetadata.DEFAULT_POLLER)
  public PollerMetadata poller() {
      PollerMetadata poll = Pollers.fixedDelay(10).get(); 
      poll.setTaskExecutor(consumer);
      return poll;
  }
  </code></pre>
  
  <p><em>Executor</em>:</p>
  
  <pre><code>private Executor consumer = Executors.newFixedThreadPool(5);
  </code></pre>
  
  <p><strong>What we have tried?</strong>
  As explained now we want to extend this implementation with a transactional functionality. We tried using the setTransactionSynchronizationFactory like explained <a href="https://stackoverflow.com/questions/19423160/sourcepollingchanneladapter-with-transaction">here</a> but it wasn't working (didn't get errors or anything but the behavior was still as it was before we added the TransactionSynchronizer):</p>
  
  <pre><code>@Bean(name = PollerMetadata.DEFAULT_POLLER)
  public PollerMetadata poller() {
      PollerMetadata poll = Pollers.fixedDelay(10).get(); 
      poll.setTaskExecutor(consumer);
  
      BeanFactory factory = mock(BeanFactory.class);
      ExpressionEvaluatingTransactionSynchronizationProcessor etsp = new ExpressionEvaluatingTransactionSynchronizationProcessor();
      etsp.setBeanFactory(factory);
      etsp.setAfterRollbackChannel(inputChannel());
      etsp.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#bix"));
      etsp.setAfterCommitChannel(inputChannel());
      etsp.setAfterCommitExpression(new SpelExpressionParser().parseExpression("#bix"));
      DefaultTransactionSynchronizationFactory dtsf = new DefaultTransactionSynchronizationFactory(etsp);
  
      poll.setTransactionSynchronizationFactory(dtsf);
  
      return poll;
  }
  </code></pre>
  
  <p>What would be the best way to realize our requirements in spring integration?</p>
  
  <p><strong>EDIT:</strong>
  As recommended in the answer I chose to do this with the JdbcChannelMessageStore. So I tried converting the XML Implementation described <a href="https://docs.spring.io/spring-integration/reference/html/jdbc.html#jdbc-message-store" rel="nofollow noreferrer">here</a> (18.4.2) into Java.  I wasn't quite sure on how to do it, this is what I have tried so far:</p>
  
  <p>I created H2 database and run the script shown <a href="https://github.com/spring-projects/spring-integration/blob/master/spring-integration-jdbc/src/main/resources/org/springframework/integration/jdbc/schema-h2.sql" rel="nofollow noreferrer">here</a> on it.</p>
  
  <p>Created JDBCChannelMessageStore Bean:</p>
  
  <pre><code>@Bean
  public JdbcChannelMessageStore store() {
      JdbcChannelMessageStore ms =  new JdbcChannelMessageStore();
      ms.setChannelMessageStoreQueryProvider(queryProvider());
      ms.setUsingIdCache(true);
      ms.setDataSource(dataSource);
      return ms;
  }
  </code></pre>
  
  <p>Created H2ChannelMessageStoreQueryProvider</p>
  
  <pre><code>    @Bean
      public ChannelMessageStoreQueryProvider queryProvider() {
          return new H2ChannelMessageStoreQueryProvider();
      }
  </code></pre>
  
  <p>Adapted the poller:</p>
  
  <pre><code>@Bean(name = PollerMetadata.DEFAULT_POLLER)
  public PollerMetadata poller() throws Exception {
      PollerMetadata poll = Pollers.fixedDelay(10).get();
      poll.setTaskExecutor(consumer);
      poll.setAdviceChain(Collections.singletonList(transactionInterceptor()));
  
      return poll;
  }
  </code></pre>
  
  <p>Autowired my PlaatformTransactionManager:</p>
  
  <pre><code>@Autowired
  PlatformTransactionManager transactionManager;
  </code></pre>
  
  <p>And created TransactionInterceptor from the TransactonManager:</p>
  
  <pre><code>@Bean
  public TransactionInterceptor transactionInterceptor() {
      return new TransactionInterceptorBuilder(true)
                  .transactionManager(transactionManager)
                  .isolation(Isolation.READ_COMMITTED)
                  .propagation(Propagation.REQUIRED)
                  .build();
  }
  </code></pre>