{"id":24188,"date":"2015-07-28T18:45:55","date_gmt":"2015-07-28T13:15:55","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=24188"},"modified":"2015-07-29T16:30:24","modified_gmt":"2015-07-29T11:00:24","slug":"sending-scheduleddelayed-messages-with-rabbitmq-through-java-client","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/sending-scheduleddelayed-messages-with-rabbitmq-through-java-client\/","title":{"rendered":"Sending Scheduled\/delayed messages with RabbitMQ through java client"},"content":{"rendered":"<p>The requirement to send delayed\/scheduled message is to publish any message with a delay time. To achieve this, earlier we had to use dead letter exchange but now we can send scheduled\/delayed <a href=\"http:\/\/www.tothenew.com\/blog\/few-simple-steps-for-integrating-rabbit-mq-with-grails\/\">messages from RabbitMQ<\/a> with &#8220;rabbitmq_delayed_message_exchange&#8221; plugin. we can send scheduled\/ delayed messages from rabbitMQ by following these steps :-<\/p>\n<p><strong>Step 1<\/strong>&#8211; Install rabbitMQ server to 3.5.3 or above on your machine. Follow this guide to install rabbitMQ <a href=\"http:\/\/www.rabbitmq.com\/download.html\" target=\"_blank\">http:\/\/www.rabbitmq.com\/download.html<\/a><\/p>\n<p><strong>Step 2\u00a0<\/strong>&#8211; Download &#8220;rabbitmq_delayed_message_exchange&#8221; plugin from rabbitMQ official website. Follow this link to download &#8220;rabbitmq_delayed_message_exchange&#8221; <a href=\"http:\/\/www.rabbitmq.com\/community-plugins.html\" target=\"_blank\">http:\/\/www.rabbitmq.com\/community-plugins.html<\/a><\/p>\n<p><strong>Step 3\u00a0<\/strong>&#8211; Place &#8220;rabbitmq_delayed_message_exchange&#8221; plugin into plugins directory of rabbitMQ server directory (in linux &#8216;\/usr\/lib\/rabbitmq\/lib\/rabbitmq_server-3.5.3\/plugins&#8217; path)<\/p>\n<p><strong>Step 4\u00a0<\/strong>&#8211; Execute &#8220;rabbitmq-plugins enable rabbitmq_delayed_message_exchange&#8221; command to enable &#8220;rabbitmq_delayed_message_exchange&#8221; plugin.<\/p>\n<p><strong>Step 5\u00a0<\/strong>&#8211; Now, after configuring rabbitMQ server we are ready to send scheduled or delayed messages. To send scheduled message we have to declare an exchange with &#8216;x-delayed-message&#8217; which is indirectly mapped with the any of the exchange types i.e topic,direct,fanout or headers.<br \/>\nFollow this code to publish message to delayed exchange:-<\/p>\n<p>[java]<br \/>\n String readyToPushContent = &quot;Hello this is the delayed message.&quot;;<\/p>\n<p>Map&lt;String, Object&gt; args = new HashMap&lt;String, Object&gt;();<br \/>\n args.put(&quot;x-delayed-type&quot;, &quot;direct&quot;);<br \/>\n channel.exchangeDeclare(&quot;exChangeName&quot;, &quot;x-delayed-message&quot;, true, false, args);<\/p>\n<p>Map&lt;String, Object&gt; headers = new HashMap&lt;String, Object&gt;()<br \/>\n headers.put(&quot;x-delay&quot;, 10000); \/\/delay in miliseconds i.e 10secs<\/p>\n<p>AMQP.BasicProperties.Builder props = new AMQP.BasicProperties.Builder().headers(headers);<br \/>\n channel.basicPublish(&quot;exChangeName&quot;, &quot;&quot;, props.build(), readyToPushContent.bytes);<\/p>\n<p>[\/java]<\/p>\n<p><strong>a.<\/strong> This plugin allows flexible routing behavior via &#8220;x-delayed-type&#8221; which can be passed at the time of\u00a0\u00a0exchange declaration. \u00a0i.e \u00a0this exchange will provide routing behavior like &#8220;direct&#8221; exchange, and exchange with type &#8220;x-delayed-message&#8221; will act as proxy.<\/p>\n<p><strong>b.<\/strong> Here we declare an exchange with &#8220;x-delayed-message&#8221; type. Then we attach a header with &#8220;x-delay&#8221; at the time of publish message, \u00a0which accepts integer value \u00a0as time (in milliseconds).<\/p>\n<p><strong>Step 6\u00a0<\/strong>&#8211; To Subscribe message from delayed exchange follow this code :-<\/p>\n<p>[java]<br \/>\n QueueingConsumer queueingConsumer = new QueueingConsumer(channel) \/\/ creating a consumer from channel<\/p>\n<p>Map&lt;String, Object&gt; args = new HashMap&lt;String, Object&gt;();<br \/>\n args.put(&quot;x-delayed-type&quot;, &quot;direct&quot;);<br \/>\n channel.exchangeDeclare(&quot;exChangeName&quot;, &quot;x-delayed-message&quot;, true, false, args); \/\/ declare delayed exchange<\/p>\n<p>String queueName = channel.queueDeclare().getQueue(); \/\/ declaring dynamic queue in channel<\/p>\n<p>channel.queueBind(queueName, &quot;exChangeName&quot;, &quot;&quot;); \/\/ binding queue with delayed exchnage<\/p>\n<p>channel.basicConsume(queueName, true, queueingConsumer) \/\/ consuming message from queue in consumer object<\/p>\n<p>try {<br \/>\n QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery() \/\/ <\/p>\n<p> String message = (new String(delivery.getBody()))<\/p>\n<p> } catch (Exception exception) {<br \/>\n exception.printStackTrace()<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p>Hope, this will help you to send scheduled\/delayed messages with rabbitMQ.<br \/>\nBouquets and brickbats are welcome.<\/p>\n<p>~vishal(d0t)kumar(at)tothenew(d0t)com~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The requirement to send delayed\/scheduled message is to publish any message with a delay time. To achieve this, earlier we had to use dead letter exchange but now we can send scheduled\/delayed messages from RabbitMQ with &#8220;rabbitmq_delayed_message_exchange&#8221; plugin. we can send scheduled\/ delayed messages from rabbitMQ by following these steps :- Step 1&#8211; Install rabbitMQ [&hellip;]<\/p>\n","protected":false},"author":250,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":23},"categories":[7,446,1],"tags":[2049,4844,1077],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24188"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/250"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=24188"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24188\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=24188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=24188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=24188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}