{"id":824,"date":"2011-04-01T11:16:59","date_gmt":"2011-04-01T09:16:59","guid":{"rendered":"http:\/\/mcl.jogger.pl\/2011\/04\/01\/advisory-messages-to-the-rescue\/"},"modified":"2023-03-23T15:31:43","modified_gmt":"2023-03-23T14:31:43","slug":"advisory-messages-to-the-rescue","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2011\/04\/01\/advisory-messages-to-the-rescue\/","title":{"rendered":"Advisory Messages to the rescue"},"content":{"rendered":"<p>The most crucial part of software development is testing. It should ensure us, that our code is correct, works according to given specs, etc. There are many kinds of tests: unit tests, integration, functional. In general you should try to test the smallest possible subset of your code and be able to check the state of the objects after the test.<\/p>\n<p>This seems as rather easy task, but what if you have an integration end-to-end test to perform? In most cases asserting state in integration test is rather hard due to multiple systems interoperability. Let\u2019s focus on a specific situation.<\/p>\n<p>What I needed to do the other day was write some integration test for Jms based system. The processing pipeline is easy:<\/p>\n<ul>\n<li>fetch object from DB<\/li>\n<li>process it<\/li>\n<li>publish on JMS<\/li>\n<\/ul>\n<p>some other system (X-system) polls JMS:<\/p>\n<ul>\n<li>if message is found<\/li>\n<li>fetch it (message disappears from the JMS queue)<\/li>\n<li>do sth with it<\/li>\n<li>Looks simple but since I didn\u2019t have any sane access to the X-system I wanted to be sure that my object was actually put into the queue. It was not acceptable to subscribe to the queue and fetch that object in my test \u2013 it would dusrupt the flow of the whole process.\n<p>Fortunately I\u2019ve been using <a href=\"http:\/\/activemq.apache.org\">ActiveMQ<\/a> and since it offers a thing called <a href=\"http:\/\/activemq.apache.org\/advisory-message.html\">Advisory Messages<\/a> I\u2019ve decided to use just them.<\/p>\n<p>What are advisory messages? They are a set of administrative messages that are generated on a specific event, like message consumption, message delivery, topic destruction, and many more. Each type of message is delivered to a separate topic \u2013 prefixed with <strong>ActiveMQ.Advisory<\/strong>. Since generation of such messages may be an overhead in production systems these features are turned off by default. You need to enable specific type of advisory message for a specific jms destination. You can do this with ths configuration change to <strong>activemq.xml<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\r\n<destinationPolicy>\r\n   <policyMap>\r\n      <policyEntries>\r\n        <policyEntry queue=&quot;my\/test\/queue&quot; advisoryForDelivery=&quot;true&quot; advisoryForConsumed=&quot;true&quot;\/>                                                   \r\n        <policyEntry topic=&quot;>&quot; producerFlowControl=&quot;true&quot; memoryLimit=&quot;1mb&quot;>\r\n          <pendingSubscriberPolicy>\r\n            <vmCursor \/>\r\n          <\/pendingSubscriberPolicy>\r\n        <\/policyEntry>\r\n      <\/policyEntries>\r\n    <\/policyMap>\r\n<\/destinationPolicy>\r\n<\/pre>\n<p>As you can see, I\u2019ve specified which advisories I want enabled. The full list of available advisories can be found <a href=\"http:\/\/activemq.apache.org\/advisory-message.html\">here<\/a>.<\/p>\n<p>Since I wanted to read messages from that topic I\u2019ve added the following configuration to my spring context \u2013 there is one destination bean for inserting messages and one bean for advisory topic.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\r\n<bean id=&quot;testQueue&quot; class=&quot;org.apache.activemq.command.ActiveMQQueue&quot; autowire=&quot;constructor&quot;>\r\n    <constructor-arg value=&quot;my\/test\/queue&quot; \/>\r\n<\/bean>\r\n\r\n<bean id=&quot;deliveredToTestQueueAdvisory&quot; class=&quot;org.apache.activemq.command.ActiveMQTopic&quot; autowire=&quot;constructor&quot;>\r\n    <constructor-arg value=&quot;ActiveMQ.Advisory.MessageDelivered.Queue.my\/test\/queue&quot; \/>\r\n<\/bean>\r\n<\/pre>\n<p>Thanks to this configuration I\u2019ve been able to check that my message was actually delivered to the queue. There\u2019ve been no need to worry about race conditions in consuming the message from original queue \u2013 if the X-system read the message, I\u2019d be unable to determine if it has ever been in JMS at all.<\/p>\n<p>What\u2019s not so nice about that:<\/p>\n<ul>\n<li>advisory messages can be thought of as counters rather than debugging information<\/li>\n<li>they don\u2019t contain any data that would allow us to match advisory message to the original message \u2013 thou you could correlate by timestamp<\/li>\n<\/ul>\n<p>All in all, it\u2019s a good tool to have! But perhaps you have some other thoughts on this subject? How do you test JMS?<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"The most crucial part of software development is testing. It should ensure us, that our code is correct,&hellip;\n","protected":false},"author":11,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[186],"class_list":{"0":"post-824","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-message-queue"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=824"}],"version-history":[{"count":5,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/824\/revisions"}],"predecessor-version":[{"id":15654,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/824\/revisions\/15654"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}