title: spring batch write to ActiveMQ
body: <p>I am try to write messages to a JMS queue which I take out in a next step to write to a database. The first part should be sync en the second async. The JMS part is really slow (1100 items to the queue in 1 minute).</p>
  
  <p>This is how my job looks.</p>
  
  <pre><code>@Bean
  public Job multiThreadedStepJob() { 
     Flow flow1 = new FlowBuilder&lt;Flow&gt;("subflow1").from(step()).end(); 
     Flow flow2 = new FlowBuilder&lt;Flow&gt;("subflow2").from(step2()).end();
     Flow splitFlow = new FlowBuilder&lt;Flow&gt;("splitflow")
     .split(new SimpleAsyncTaskExecutor()).add(flow1, flow2) .build();
  
     return jobBuilders.get("multiThreadedStepJob")
                            .start(splitFlow).end().build();
  
  }
  </code></pre>
  
  <p>The first step:</p>
  
  <pre><code>@Bean
  public Step step() {
       return stepBuilders.get("step")
           .&lt;OrderDTO, OrderDTO&gt;chunk(CHUNK_SIZE)
           .reader(reader())
           .writer(writer())   
           .build();
  }
  </code></pre>
  
  <p>the second step:</p>
  
  <pre><code>@Bean
  public Step step2() {
      return stepBuilders.get("step2")
              .&lt;OrderDTO, OrderDTO&gt;chunk(100)
              .reader(reader2())
              .writer(writer2())
              .build();
  }
  </code></pre>
  
  <p>I think that my errors are inside the writer of step and the reader of step2, because I can run the other reader and writer together and I have no problems.</p>
  
  <pre><code>@Bean
  public JmsItemWriter&lt;OrderDTO&gt; writer() {
      JmsItemWriter&lt;OrderDTO&gt; itemWriter = new JmsItemWriter&lt;&gt;();
      itemWriter.setJmsTemplate(infrastructureConfiguration.jmsTemplate());
      return itemWriter;
  }
  
  @Bean
  public JmsItemReader&lt;OrderDTO&gt; reader2() {
      JmsItemReader&lt;OrderDTO&gt; itemReader = new JmsItemReader&lt;&gt;();
      itemReader.setJmsTemplate(infrastructureConfiguration.jmsTemplate());
      itemReader.setItemType(OrderDTO.class);
      return itemReader;
  }
  </code></pre>
  
  <p>They use the same JmsTemplate for connecting to the queue:</p>
  
  <pre><code>@Bean
  public JmsTemplate jmsTemplate() {
      JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory());
      jmsTemplate.setDefaultDestination(queue());
      jmsTemplate.setReceiveTimeout(500);
      return jmsTemplate;
  }
  
  @Bean
  public Queue queue() {
      return new ActiveMQQueue("orderList");
  }
  
  @Bean
  public ConnectionFactory connectionFactory() {
      ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(BROKER_URL);
      factory.setTrustAllPackages(true);
  
      ActiveMQPrefetchPolicy prefetchPolicy = new ActiveMQPrefetchPolicy();
      prefetchPolicy.setQueuePrefetch(30);
  
      factory.setPrefetchPolicy(prefetchPolicy);
  
      PooledConnectionFactory pool = new PooledConnectionFactory(factory);
      pool.setMaxConnections(10);
      pool.setMaximumActiveSessionPerConnection(10);
      pool.isCreateConnectionOnStartup();
  
      return pool;
  }
  </code></pre>
  
  <p>The rest of the configuration I use is the configuration from @EnableBatchProcessing. Does anyone know why this is going so slow?</p>