{"id":22668,"date":"2015-07-08T17:34:44","date_gmt":"2015-07-08T12:04:44","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=22668"},"modified":"2016-12-19T15:02:50","modified_gmt":"2016-12-19T09:32:50","slug":"aem-robust-sling-eventing-using-sling-jobs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/aem-robust-sling-eventing-using-sling-jobs\/","title":{"rendered":"AEM : Robust Sling Eventing using Sling Jobs"},"content":{"rendered":"<p>So far we have\u00a0<a href=\"http:\/\/www.tothenew.com\/blog\/creating-osgi-factory-configurations-in-aem\/\">used OSGi Event Admin<\/a> for event publishing that uses publisher subscriber model. Though it works really well, there is\u00a0a downside in using this:<\/p>\n<p>&#8211; No guarantee of delivery<\/p>\n<p>As soon as an event is published, job of the publisher is done. No matter whether any subscriber has worked on it or not, the event just dies down.<\/p>\n<p>&#8211; No distributed delivery<\/p>\n<p>OSGi Event Admin doesn&#8217;t fare well with clustered environment.<\/p>\n<p>Sling Job mechanism takes care of these shortcomings and is persistent(persisted under\u00a0\/var\/eventing\/jobs). It guarantees delivery and comes with lots of configurable options like number of retries and retry interval. In <a href=\"https:\/\/docs.adobe.com\/docs\/en\/aem\/6-1\/release-notes\/deprecated-removed-features.html\">AEM 6.1 release notes<\/a>, Adobe recommends using this new Eventing mechanism as old eventing has deprecated.<\/p>\n<p>To implement it, you need a publisher and a consumer:<\/p>\n<p>Publisher makes use of JobManager to add a job:<\/p>\n<p>[java]<br \/>\n@Reference<br \/>\nprivate JobManager jobManager;<\/p>\n<p>@Override<br \/>\npublic void publishJob() {<br \/>\n    final Map&lt;String, Object&gt; jobProperties = new HashMap&lt;String, Object&gt;();<br \/>\n    jobProperties.put(&quot;jobName&quot;, &quot;some dummy job&quot;);<br \/>\n    jobProperties.put(&quot;count&quot;, 3);<br \/>\n    jobProperties.put(&quot;job location&quot;, &quot;yet another city&quot;);<br \/>\n    jobManager.addJob(&quot;my\/sling\/job&quot;, jobProperties);<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Here addJob method takes 2 parameters(same as event service) &#8211; job topic and properties.<\/p>\n<p>Consumer subscribes to that job topic and processes it. In <a href=\"http:\/\/www.tothenew.com\/success-stories\/time-warner-cable\">case of successful execution<\/a>, JobResult.OK should be returned. In case there is some issue in execution but you need to keep job for retry, use JobResult.FAILED, else use JobResult.CANCEL that cancels the job right away.<\/p>\n<p>[java]<br \/>\n@Component<br \/>\n@Service(value={JobConsumer.class})<br \/>\n@Property(name=JobConsumer.PROPERTY_TOPICS, value=&quot;my\/sling\/job&quot;)<br \/>\npublic class MyJobConsumer implements JobConsumer {<\/p>\n<p>    public JobResult process(final Job job) {<br \/>\n        System.out.println(&quot;==========================================&quot;);<br \/>\n        System.out.println(job.getProperty(&quot;count&quot;));<br \/>\n        System.out.println(&quot;==========================================&quot;);<br \/>\n        \/\/ process the job and return the result<br \/>\n        int count = (Integer)job.getProperty(&quot;count&quot;);<br \/>\n        if(count &gt; 3){<br \/>\n           return JobResult.FAILED;<br \/>\n        }<br \/>\n        return JobResult.OK;<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>&#8220;Apache Sling Default Job Queue&#8221; maintains default configuration of the queue. \u00a0We can also maintain a separate queue for a pool of job topics that we create. \u00a0&#8220;Apache Sling Job Queue Configuration&#8221; is a factory configuration that takes care of this.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-22669\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/07\/queue.png\" alt=\"queue\" width=\"1173\" height=\"644\" \/><\/p>\n<p>Hope this helps.. \ud83d\ude42<\/p>\n<p>Vivek Sachdeva<\/p>\n<p><a href=\"https:\/\/github.com\/viveksachdeva\">Github Repo<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So far we have\u00a0used OSGi Event Admin for event publishing that uses publisher subscriber model. Though it works really well, there is\u00a0a downside in using this: &#8211; No guarantee of delivery As soon as an event is published, job of the publisher is done. No matter whether any subscriber has worked on it or not, [&hellip;]<\/p>\n","protected":false},"author":48,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":96},"categories":[1],"tags":[1581,1971,4850,1521,1970],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22668"}],"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\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=22668"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22668\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=22668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=22668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=22668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}