{"id":2413,"date":"2010-08-12T14:31:45","date_gmt":"2010-08-12T12:31:45","guid":{"rendered":"http:\/\/blog.jteam.nl\/?p=2413"},"modified":"2010-08-12T14:31:45","modified_gmt":"2010-08-12T12:31:45","slug":"running-activemq-using-spring","status":"publish","type":"post","link":"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/","title":{"rendered":"Running ActiveMQ using Spring"},"content":{"rendered":"<p>Apache ActiveMQ is an open source messaging framework. The ActiveMQ web site is not really clear on how to integrate it with the Spring framework. Therefore, I decided to write this post to explain how to use ActiveMQ in combination with Spring and clarify some points.<br \/>\nThe good news is that you can run JMS inside a servlet container (e.g. Apache Tomcat) without the need for a JCA adapter. This means you do not need Jencks or something similar.<\/p>\n<p><!--more--><\/p>\n<div style=\"background-color: #4B08A1;color: white;font-weight: bold;padding: 10px;margin: 10px\">\nNote:<br \/>\nThis is a repost of a blog item that was originally posted in the Func knowledge base by Diego Castorina on January 29, 2010.\n<\/div>\n<p>To get started, you can embed the configuration of a complete running environment in a Spring configuration file. Here is the example we&#8217;ll use for the remainder of this blog post:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n       xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\n       xmlns:amq=&quot;http:\/\/activemq.apache.org\/schema\/core&quot;\n       xmlns:jms=&quot;http:\/\/www.springframework.org\/schema\/jms&quot;\n       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\u00a0http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\n           http:\/\/www.springframework.org\/schema\/jms http:\/\/www.springframework.org\/schema\/jms\/spring-jms-2.5.xsd&quot;&gt;\n  &lt;amq:broker brokername=&quot;test-broker&quot; start=&quot;true&quot;&gt;\n    &lt;amq:persistenceAdapter&gt;\n      &lt;amq:amqPersistenceAdapter directory=&quot;\/opt\/activemq&quot; maxFileLength=&quot;32mb&quot;\/&gt;\n    &lt;\/amq:persistenceAdapter&gt;\n    &lt;amq:transportconnectors&gt;\n      &lt;amq:transportconnector uri=&quot;tcp:\/\/localhost:7171&quot;\/&gt;\n    &lt;\/amq:transportconnectors&gt;\n  &lt;\/amq:broker&gt;\n  &lt;amq:connectionFactory id=&quot;amqConnectionFactory&quot;  brokerURL=&quot;vm:\/\/test-broker&quot;\/&gt;\n  &lt;bean class=&quot;org.springframework.jms.connection.CachingConnectionFactory&quot; id=&quot;connectionFactory&quot;&gt;\n    &lt;constructor-arg ref=&quot;amqConnectionFactory&quot;\/&gt;\n    &lt;property name=&quot;sessionCacheSize&quot; value=&quot;100&quot;\/&gt;\n  &lt;\/bean&gt;\n  &lt;amq:queue physicalName=&quot;testQueue&quot; \/&gt;\n  &lt;bean class=&quot;org.springframework.jms.core.JmsTemplate&quot;  id=&quot;jmsTemplate&quot;&gt;\n    &lt;constructor-arg ref=&quot;connectionFactory&quot;\/&gt;\n  &lt;\/bean&gt;\n  &lt;bean class=&quot;packageName.ClassName&quot; id=&quot;queueProducer&quot;&gt;\n    &lt;property name=&quot;jmsTemplate&quot; ref=&quot;jmsTemplate&quot;\/&gt;\n    &lt;property name=&quot;queueName&quot; value=&quot;testQueue&quot;\/&gt;\n  &lt;\/bean&gt;\n  &lt;bean class=&quot;packageName.ClassName2&quot; id=&quot;queueListener&quot;\/&gt;\n  &lt;jms:listener-container concurrency=&quot;10&quot; connectionfactory=&quot;connectionFactory&quot;&gt;\n    &lt;jms:listener destination=&quot;testQueue&quot; ref=&quot;queueListener&quot;\/&gt;\n  &lt;\/jms:listener-container&gt;\n&lt;\/beans&gt;\n<\/pre>\n<p>That&#8217;s a lot of configuration, but let&#8217;s analyze each one:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;amq:broker brokername=&quot;test-broker&quot; start=&quot;true&quot;&gt;\n  &lt;amq:persistenceAdapter&gt;\n    &lt;amq:amqPersistenceAdapter directory=&quot;\/opt\/activemq&quot; maxFileLength=&quot;32mb&quot;\/&gt;\n  &lt;\/amq:persistenceAdapter&gt;\n  &lt;amq:transportconnectors&gt;\n    &lt;amq:transportconnector uri=&quot;tcp:\/\/localhost:7171&quot;\/&gt;\n  &lt;\/amq:transportconnectors&gt;\n&lt;\/amq:broker&gt;\n<\/pre>\n<p>This is the definition for an embedded JMS broker, called <code>test-broker<\/code> which listens on port 7171 using the tcp protocol and which persist data using the default AMQ Message Store in the <code>\/opt\/activemq<\/code> directory.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;amq:connectionFactory id=&quot;amqConnectionFactory&quot; brokerURL=&quot;vm:\/\/test-broker&quot;\/&gt;\n<\/pre>\n<p>The definition of the JMS connection factory which connects to the broker using a VM transport. In this way the communication is made at the JVM level, thus avoiding network overhead.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;amq:queue id=&quot;testQueue&quot; physicalname=&quot;TestQueue&quot;&gt;\n<\/pre>\n<p>Defines the queue that we are going to use.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;bean class=&quot;org.springframework.jms.connection.CachingConnectionFactory&quot; id=&quot;connectionFactory&quot;&gt;\n   &lt;constructor-arg ref=&quot;amqConnectionFactory&quot;\/&gt;\n   &lt;property name=&quot;sessionCacheSize&quot; value=&quot;100&quot;\/&gt;\n&lt;\/bean&gt;\n<\/pre>\n<p>This is the connection factory we are really going to use in our application. It caches connections, sessions and even the <code>MessageProducer<\/code>. It is important to set the value of the <code>sessionCacheSize<\/code> property since the default value is 1.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;bean class=&quot;org.springframework.jms.core.JmsTemplate&quot; id=&quot;jmsTemplate&quot;&gt;\n   &lt;constructor-arg ref=&quot;connectionFactory&quot;\/&gt;\n&lt;\/bean&gt;\n<\/pre>\n<p>For those who know the Spring support for JDBC and Hibernate, the <code>JmsTemplate<\/code> class should sound familiar since it has a very similar design to the <code>JdbcTemplate<\/code> and <code>HibernateTemplate<\/code> class.<br \/>\nIt hides most of the JMS-related low-level details (e.g. obtaining a session or handling acknowledgments). By default session are not transactional and auto-acknowledged.<br \/>\nThe <code>connectionFactory<\/code> is passed as a property to the constructor.<br \/>\nOne of its most important method on the <code>JmsTemplate<\/code> is <code>send(String destinationName, MessageCreator messageCreator)<\/code>. The <code>MessageCreator<\/code> is an interface defined by Spring having only the <code>createMessage(Session session)<\/code> method. Its output is the JMS message that will be sent to <code>destinationName<\/code>.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;bean class=&quot;packageName.ClassName&quot; id=&quot;queueProducer&quot;&gt;\n    &lt;property name=&quot;jmsTemplate&quot; ref=&quot;jmsTemplate&quot;&gt;&lt;\/property&gt;\n    &lt;property name=&quot;queueName&quot; value=&quot;testQueue&quot;&gt;&lt;\/property&gt;\n&lt;\/bean&gt;\n<\/pre>\n<p>This, finally is an object that is defined within our application, the object that produces the message queue and sends messages to it through the specified <code>JmsTemplate<\/code>.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;bean class=&quot;packageName.ClassName2&quot; id=&quot;queueListener&quot;\/&gt;\n<\/pre>\n<p>The bean definition that makes up the consumer of our message queue. It needs to implement the <code>javax.jms.MessageListener<\/code> interface (or some Spring specific interface like <code>SessionAwareMessageListener<\/code>).<br \/>\nIn Spring terms it is a Message Driven Pojo, the difference with a standard Message Driven Bean is that it does not need to run in an EJB container.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;jms:listener-container concurrency=&quot;1&quot; connection-factory=&quot;connectionFactory&quot;&gt;\n    &lt;jms:listener destination=&quot;AggregateQueue&quot; ref=&quot;queueListener&quot;&gt;&lt;\/jms:listener&gt;\n&lt;\/jms:listener-container&gt;\n<\/pre>\n<p>This is the bean who makes the magic happen: it registers itself as a listener for all the queues and executes the callback on the listener as a message is received in an asynchronous way using a Spring <code>TaskExecutor<\/code>. The concurrency attribute defines how many <code>MessageConsumer<\/code> instances will be created for each listener. If you use a value &gt; 1 remember to set the <code>prefetchLimit<\/code> to 1 in order to avoid undelivered messages to the consumers.<\/p>\n<p>Have fun \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apache ActiveMQ is an open source messaging framework. The ActiveMQ web site is not really clear on how to integrate it with the Spring framework. Therefore, I decided to write this post to explain how to use ActiveMQ in combination with Spring and clarify some points. The good news is that you can run JMS [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[10],"tags":[206,207,95],"class_list":["post-2413","post","type-post","status-publish","format-standard","hentry","category-development","tag-activemq","tag-jms","tag-spring-framework"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Running ActiveMQ using Spring - Trifork Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Running ActiveMQ using Spring - Trifork Blog\" \/>\n<meta property=\"og:description\" content=\"Apache ActiveMQ is an open source messaging framework. The ActiveMQ web site is not really clear on how to integrate it with the Spring framework. Therefore, I decided to write this post to explain how to use ActiveMQ in combination with Spring and clarify some points. The good news is that you can run JMS [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/\" \/>\n<meta property=\"og:site_name\" content=\"Trifork Blog\" \/>\n<meta property=\"article:published_time\" content=\"2010-08-12T12:31:45+00:00\" \/>\n<meta name=\"author\" content=\"Diego Castorina\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Diego Castorina\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/\",\"url\":\"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/\",\"name\":\"Running ActiveMQ using Spring - Trifork Blog\",\"isPartOf\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#website\"},\"datePublished\":\"2010-08-12T12:31:45+00:00\",\"author\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/6f4caf369ca15e0454061a3cd5652848\"},\"breadcrumb\":{\"@id\":\"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/trifork.nl\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Running ActiveMQ using Spring\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/trifork.nl\/blog\/#website\",\"url\":\"https:\/\/trifork.nl\/blog\/\",\"name\":\"Trifork Blog\",\"description\":\"Keep updated on the technical solutions Trifork is working on!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/trifork.nl\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/6f4caf369ca15e0454061a3cd5652848\",\"name\":\"Diego Castorina\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d65c89d9bcbd8b07f37035557b585077?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d65c89d9bcbd8b07f37035557b585077?s=96&d=mm&r=g\",\"caption\":\"Diego Castorina\"},\"url\":\"https:\/\/trifork.nl\/blog\/author\/diego\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Running ActiveMQ using Spring - Trifork Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/","og_locale":"en_US","og_type":"article","og_title":"Running ActiveMQ using Spring - Trifork Blog","og_description":"Apache ActiveMQ is an open source messaging framework. The ActiveMQ web site is not really clear on how to integrate it with the Spring framework. Therefore, I decided to write this post to explain how to use ActiveMQ in combination with Spring and clarify some points. The good news is that you can run JMS [&hellip;]","og_url":"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/","og_site_name":"Trifork Blog","article_published_time":"2010-08-12T12:31:45+00:00","author":"Diego Castorina","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Diego Castorina","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/","url":"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/","name":"Running ActiveMQ using Spring - Trifork Blog","isPartOf":{"@id":"https:\/\/trifork.nl\/blog\/#website"},"datePublished":"2010-08-12T12:31:45+00:00","author":{"@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/6f4caf369ca15e0454061a3cd5652848"},"breadcrumb":{"@id":"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/trifork.nl\/blog\/running-activemq-using-spring\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/trifork.nl\/blog\/"},{"@type":"ListItem","position":2,"name":"Running ActiveMQ using Spring"}]},{"@type":"WebSite","@id":"https:\/\/trifork.nl\/blog\/#website","url":"https:\/\/trifork.nl\/blog\/","name":"Trifork Blog","description":"Keep updated on the technical solutions Trifork is working on!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/trifork.nl\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/6f4caf369ca15e0454061a3cd5652848","name":"Diego Castorina","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d65c89d9bcbd8b07f37035557b585077?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d65c89d9bcbd8b07f37035557b585077?s=96&d=mm&r=g","caption":"Diego Castorina"},"url":"https:\/\/trifork.nl\/blog\/author\/diego\/"}]}},"_links":{"self":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/2413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/comments?post=2413"}],"version-history":[{"count":0,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/2413\/revisions"}],"wp:attachment":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/media?parent=2413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/categories?post=2413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/tags?post=2413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}