| <div class="wiki-content maincontent"> |
| <p>Spring supports a useful JMS abstraction called JmsTemplate which makes sending and consuming messages a little simpler than using the JMS APIs directly. The only downside of this abstraction is that each send() will create a connection, session, producer, send the message, then close them all down. This is a really inefficient operation with most JMS providers as each create of a connection, session, producer ends up being an RPC with the broker, if nothing else, for security reasons.</p> |
| |
| <p>In 3.1-M2 or later of ActiveMQ there's a <a shape="rect" href="../../../spring-support.xml">simple solution</a> using the org.activemq.pool.PooledConnectionFactory which will perform pooling of JMS resources (connection, session, producer) to make sending messages efficiently. The same thing is true if you wanna use a similar pattern to the JmsTemplate to send messages from inside EJBs.</p> |
| |
| <p>Here's an example of it in use inside a Spring config file.</p> |
| |
| <div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl"> |
| <script class="brush: java; gutter: false; theme: Default" type="syntaxhighlighter"><![CDATA[ |
| <!-- a pooling based JMS provider --> |
| <bean id="jmsFactory" class="org.activemq.pool.PooledConnectionFactory"> |
| <property name="connectionFactory"> |
| <bean class="org.activemq.ActiveMQConnectionFactory"> |
| <property name="brokerURL"> |
| <value>tcp://localhost:61616</value> |
| </property> |
| </bean> |
| </property> |
| </bean> |
| |
| <!-- Spring JMS Template --> |
| <bean id="myJmsTemplate" class="org.springframework.jms.core.JmsTemplate"> |
| <property name="connectionFactory"> |
| <ref local="jmsFactory"/> |
| </property> |
| </bean> |
| ]]></script> |
| </div></div></div> |
| |