title: using amazon sqs in a @MessageDriven bean - pooling / parallel processing
body: <p>We need to use queues in our Java EE application and since it is a cloud base application (deployed on OpenShift Online), we like to use amazon sqs.</p>
  
  <p>If I understand the theorie of the receiving part of JMS / Java EE correctly, a <code>@MessageDriven</code> bean is managed by the Java EE container so that a lot of bean instances are created in parallel (according max pool size), if the number of the incoming messages is high. This is of course a big benefit to process high loads.</p>
  
  <p>However, I do not see how we can integrate aws sqs this way in a Java EE application. I know the asynchronous receiver examples from <a href="http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-java-message-service-jms-client.html" rel="nofollow noreferrer">http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-java-message-service-jms-client.html</a>:</p>
  
  <pre><code>class MyListener implements MessageListener {
  
      @Override
      public void onMessage(Message message) {
          try {
              // Cast the received message as TextMessage and print the text to screen.
              if (message != null) {
                  System.out.println("Received: " + ((TextMessage) message).getText());
              }
          } catch (JMSException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>and then:</p>
  
  <pre><code>// Create a consumer for the 'TestQueue'.
  MessageConsumer consumer = session.createConsumer(queue);
  
  // Instantiate and set the message listener for the consumer.
  consumer.setMessageListener(new MyListener());
  
  // Start receiving incoming messages.
  connection.start();
  </code></pre>
  
  <p>This is the official asynchronous receiver example - which is not a <code>@MessageDriven</code> bean. It is obvious, that we need somewhere the credentials to authenticate (by creating an SQSConnectionFactory, then a connection, then a session - which is also well described in the example).<br>
  But I strongly suppose that this example will not process the messages in parallel - i.e. only one bean instance is processing the queue and this is not a good solution for scalable, high loaded applications.</p>
  
  <p>a) How can we go the real Java EE way with Amazon SQS?
  I just find a planty of Spring examples. But it must be Java EE 7. </p>
  
  <p>b) We use Wildfly (currently 8.2.1). Would it be also possible to let Wildfly manage the connection to AWS and application internally, we could use the queue as if it were an application server managed queue (same approach like data sources for DB access)?</p>
  
  <p><strong>Conclusion after got an answer from <em>stdunbar</em></strong>:<br>
  It seems not to be possible in a 'proper way', what I like to do. So what should I do? Implement a <code>ManagedExecutorService</code> as <em>stdunbar</em> described to 'wrap' the queue? - However this implies to have a local queue as well and this is not a good situation for an application, which should be scaleable?!
  What is about alternatives? We are running the application on OpenShift Online. It would probably be bether to instantiate an own gear with e.g. ApacheMQ Cartridge... there are of course a lot of disadventages like costs and that we are responsible for the 'infrastructure'.<br></p>
  
  <p>To be honest, I am really disappointed of AWS in this case...</p>