Monday 16 March 2015

Introduction to Scheduler in AEM6/CQ5


Scheduler in Apache Sling comes in a picture when there is need to run specific job after a regular internals. Intervals we can specify via cron expressions. Here Job we can refer a simple logic that need to perform. Below is the simple demonstration, how to create a scheduler in AEM6.0 that write the content over the node or on a file in crxde.

USE-CASE : Create a sitemap.xml file under /content folder.

Here cron expression  value = "0 * * * * ?" defines scheduler will execute after one minute.
XMLStreamWriter is use to write a xml stream that will get write over the node which wrap the ByteArrayOutputStream.  I did’t used the whole code to create a sitemap as you can get it via acs-commons project.

https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/main/java/com/adobe/acs/commons/wcm/impl/SiteMapServlet.java


=================================================================
Service
@Component(name = "Delta Job", label = "Training Schedular Delta", description = "Produce the file after hour", metatype = true, immediate = true)
@Properties({
        @Property(label = "Quartz Cron Expression", description = "Quartz Scheduler specific cron expression. Do not put unix cron expression", name = "scheduler.expression", value = "0 * * * * ?"),
        @Property(
                label = "Allow concurrent executions",
                description = "Allow concurrent executions of this Scheduled Service",
                name = "scheduler.concurrent",
                boolValue = false,
                propertyPrivate = true
        )
})
public class SimpleSchedular implements Runnable {
    private static Logger logger = LoggerFactory.getLogger(SimpleSchedular.class);
    private PageManager pageManager = null;
    private Page page = null;
    @Reference
    private ResourceResolverFactory resourceResolverFactory;
    private static final String NS = "http://www.sitemaps.org/schemas/sitemap/0.9";
    public void run() {
        try {
            ResourceResolver adminResourceResolver = null;
            adminResourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
            XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
            try {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                Session session = adminResourceResolver.adaptTo(Session.class);
                XMLStreamWriter stream = outputFactory.createXMLStreamWriter(bos);
                stream.writeStartDocument("1.0");
                stream.writeStartElement("", "urlset", NS);
                stream.writeNamespace("", NS);
                // Here you can write a logic for nested xml elements.
                stream.writeEndElement();
                stream.writeEndDocument();
                ValueFactory vf = session.getValueFactory();
                Binary binary = vf.createBinary(new ByteArrayInputStream(bos.toByteArray()));
                Node rootNode = session.getRootNode();
                Node contentNode = rootNode.getNode("content");
                            if(!contentNode.hasNode("sitemap.xml")) {
                    Node sitemapNode = contentNode.addNode("sitemap.xml", "nt:file");
                    Node resNode = sitemapNode.addNode("jcr:content", "nt:resource");
                    resNode.setProperty("jcr:data", binary);
                } else {
                    Node resNode = contentNode.getNode("sitemap.xml").getNode("jcr:content");
                    resNode.setProperty("jcr:data", binary);
                }
                logger.info("End Document");
                session.save();
            } catch (XMLStreamException e) {
                logger.error("Error is :: " + e.getMessage());
            } catch (Exception ioex) {
                logger.error("Error is ioe :: " + ioex.getMessage());
            }
        } catch (Exception ex) {
            logger.error("Error is general" + ex.getMessage());
        }
    }

}


Refernces :

http://sling.apache.org/documentation/bundles/scheduler-service-commons-scheduler.html
http://adobe-consulting-services.github.io/acs-aem-commons/features/simple-sitemap.html

1 comment:

  1. how to create sitemap.xml within the "sitemap" folder under the /content/geometrixx/
    using java

    ReplyDelete