| <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 <link><page ri:content-title="Spring Support"></page><link-body>simple solution</link-body></link> 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> |
| |
| <structured-macro ac:macro-id="7b9bb11a-30f0-4e85-aa75-6895f8975cf1" ac:name="code" ac:schema-version="1"><plain-text-body> |
| <!-- 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> |
| </plain-text-body></structured-macro></div> |
| |