title: Can't invoke ActiveMQConnection class directly in a java web servlet
body: <p><strong>Issue:</strong></p>
  <p>I'm trying to set a timeout using <code>ActiveMQConnection</code> object to invoke the <code>setSendTimeout</code> method in my Java servlet but when that code is invoked I get:</p>
  <pre><code>Warning:   RAR5038:Unexpected exception while creating resource for pool jms_amq_conn_pool. Exception : javax.resource.ResourceException: Could not create connection.
  Warning:   RAR5117 : Failed to obtain/create connection from connection pool [ jms_amq_conn_pool ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Could not create connection.
  </code></pre>
  <p>How can I get around this?</p>
  <p><strong>Issue in more detail:</strong></p>
  <p>I have a Payara server that has an activemq-rar resource loaded that a servlet I have invokes.  I followed <a href="https://blog.payara.fish/connecting-to-activemq-with-payara-server" rel="nofollow noreferrer">these instructions</a> to do that.</p>
  <p>Here's a list highlighted the steps from that link:</p>
  <ol>
  <li>Get activemq rar file to deploy to Payara server.</li>
  <li>Configure the ActiveMQ Connector</li>
  <li>Create JMS Connection pool</li>
  <li>Create JNDI mapping to JMS Connection pool by creating a JMS Resource.</li>
  </ol>
  <p>I have Java servlet code that looks like this that works fine when I want it to send a message:</p>
  <pre class="lang-java prettyprint-override"><code>    /**
      * Creates a connection using Payara resource then creates a producer/consumer then sends a message.
      */
      public void sendAMQMessage() {
          javax.jms.ConnectionFactory connectionFactory = InitialContext.doLookup(&quot;servlet_jms_amq_conn_factory&quot;);
          javax.jms.Connection connection = connectionFactory.createConnection();
  
          // creation is created, now set the client id and start it
          connection.setClientID(&quot;test_client_id&quot;);
          connection.start();
  
          javax.jms.Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  
          javax.jms.TemporaryQueue queue = session.createTemporaryQueue();
  
          javax.jms.MessageConsumer consumer = session.createConsumer(queue);
  
          consumer.setMessageListener(this);
  
          javax.jms.Queue serviceQueue = session.createQueue(&quot;queue_name&quot;);
  
          javax.jms.MessageProducer producer = session.createProducer(serviceQueue);
  
          producer.send(&quot;data&quot;);
      
      }
  
  
      @Override
      public void onMessage(Message message) {
          // receives data back from the initial request.
      }
  </code></pre>
  <p>The current web application's <code>pom.xml</code> doesn't reference an ActiveMQ library since the code above does not import any ActiveMQ classes. But I want to implement a timeout on the message being sent by the Java servlet  so that if the receiving application doesn't respond in a certain amount of time I can handle that.</p>
  <p>The <a href="https://docs.oracle.com/javaee/7/api/javax/jms/Connection.html" rel="nofollow noreferrer"><code>javax.jms.Connection</code></a> does not have a timeout setter method.</p>
  <p>The <a href="https://activemq.apache.org/maven/apidocs/org/apache/activemq/ActiveMQConnection.html#setSendTimeout-int-" rel="nofollow noreferrer"><code>ActiveMQConnection</code> JavaDoc</a> does have a <code>setSendTimeout(int sendTimeout)</code>. So I included the ActiveMQ dependency in the app's <code>pom.xml</code>:</p>
  <pre class="lang-xml prettyprint-override"><code>        &lt;dependency&gt;
              &lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
              &lt;artifactId&gt;activemq-all&lt;/artifactId&gt;
              &lt;version&gt;5.10.0&lt;/version&gt;
              &lt;type&gt;jar&lt;/type&gt;
          &lt;/dependency&gt;
  </code></pre>
  <p>Then when I update the <code>sendAMQMessage</code> method above to use the <code>setSendTimeout(int sendTimeout)</code>:</p>
  <pre><code>int milliseconds = 10000; // 10,000 ms = 10 seconds
  ((ActiveMQConnection)connection).setSendTimeout(milliseconds);
  </code></pre>
  <p>and then run the code I get these warnings and exception messages:</p>
  <pre><code>Warning:   RAR5038:Unexpected exception while creating resource for pool jms_amq_conn_pool. Exception : javax.resource.ResourceException: Could not create connection.
  Warning:   RAR5117 : Failed to obtain/create connection from connection pool [ jms_amq_conn_pool ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Could not create connection.
  Warning:   RAR5038:Unexpected exception while creating resource for pool jms_amq_conn_pool. Exception : javax.resource.ResourceException: Could not create connection.
  Warning:   RAR5117 : Failed to obtain/create connection from connection pool [ jms_amq_conn_pool ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Could not create connection.
  
  javax.jms.JMSException: Error in allocating a connection. Cause: Could not create connection.
      at org.apache.activemq.ra.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:101)
      at org.apache.activemq.ra.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:67)
  </code></pre>
  <p>I've looked at <a href="https://stackoverflow.com/questions/9428783/failed-to-obtain-create-connection-from-connection-pool">this link</a> which says to just copy the jar to the Payara server's <code>domains/domain1/ilb/ext</code> folder. So I did that with the <code>activemq-all-5.10.0.jar</code> then when I run the code above I get this exception:</p>
  <pre><code>java.lang.ClassCastException: org.apache.activemq.command.ActiveMQQueue cannot be cast to javax.jms.Queue
  </code></pre>
  <p>There must be a way to resolve this.  Anyone have thoughts?</p>