title: Sending a BlobMessage using JmsTemplate with CachingConnectionFactory
body: <p>I am using ActiveMQ as my message broker and I have a JMS requirement of sending a big document as an input stream to be processed by consumers. The issue I have is that if I use Spring's org.springframework.jms.connection.CachingConnectionFactory to wrap an ActiveMQConnectionFactory, I'll have to cast the session object (javax.jms.Session) into an org.apache.activemq.ActiveMQSession which is what was suggested in here (<a href="https://stackoverflow.com/questions/17550112/sending-files-using-active-mq-with-blobmessage">Sending Files using Active MQ with BlobMessage</a>).
  </p>
  <p>
  I then get an exception if I cast the generic Session object into an ActiveMQSession because Spring only provides a Proxy for the javax.jms.Session and not the target object that it proxies to (ie org.apache.activemq.ActiveMQSession). So the solution I resorted to was to just use a plain ActiveMQConnectionFactory instead of using a CachingConnectionFactory. Is there a way for me to keep a CachingConnectionFactory without getting the ClassCastException from my sample code below?
  </p>
  <pre><code>  jmsTemplate.send(new MessageCreator() {
      public Message createMessage(Session session) throws JMSException {
        org.apache.activemq.ActiveMQSession activeMQSession = ((org.apache.activemq.ActiveMQSession) session);
  
        BlobMessage message = activeMQSession.createBlobMessage(inputStream);
        LOGGER.debug(&quot;created a blob message&quot;);
        return message;
      }
    });
  </code></pre>
  <p>And here's what I currently have now in my application context which will prevent the ClassCastException from being thrown. See the commented blocks as well which shows the old configuration I have which throws the exception:</p>
  <pre><code>&lt;bean id=&quot;jmsTemplate&quot; class=&quot;org.springframework.jms.core.JmsTemplate&quot;&gt;
    &lt;property name=&quot;connectionFactory&quot; ref=&quot;connectionFactory&quot;/&gt;
    &lt;property name=&quot;defaultDestination&quot; ref=&quot;gtJobRequestQueue&quot;/&gt;
  &lt;/bean&gt;
  
  &lt;!--   &lt;bean id=&quot;connectionFactory&quot; class=&quot;org.springframework.jms.connection.CachingConnectionFactory&quot;&gt; --&gt;
  &lt;!--     &lt;property name=&quot;targetConnectionFactory&quot; ref=&quot;activeMqConnectionFactory&quot; /&gt; --&gt;
  &lt;!--     &lt;property name=&quot;sessionCacheSize&quot; value=&quot;3&quot;/&gt; --&gt;
  &lt;!--     &lt;property name=&quot;cacheProducers&quot; value=&quot;true&quot;/&gt; --&gt;
  &lt;!--   &lt;/bean&gt; --&gt;
  
  &lt;!-- &lt;bean id=&quot;activeMqConnectionFactory&quot; class=&quot;org.apache.activemq.spring.ActiveMQConnectionFactory&quot;&gt; --&gt;
  &lt;bean id=&quot;connectionFactory&quot; class=&quot;org.apache.activemq.spring.ActiveMQConnectionFactory&quot;&gt;
    &lt;property name=&quot;brokerURL&quot; value=&quot;${aurin.data-provider.broker.url}&quot;/&gt;
    &lt;property name=&quot;prefetchPolicy&quot; ref=&quot;prefetchPolicy&quot; /&gt; 
  &lt;/bean&gt;
  </code></pre>