Page tree
Skip to end of metadata
Go to start of metadata


Define a custom create Log work item handler (task)

  1. Update WorkItemDefinitions.wid file as following:

    WorkItemDefinitions.wid
    import org.drools.core.process.core.datatype.impl.type.StringDataType
    
    [
       //Email notification task
       [
         "name" : "sendEmail",
         "displayName" : "SendEmailNotification"
       ],
       //Log notification task
       [
         "name" : "createLog",
         "displayName" : "CreateLogNotification"
       ]   
    ]


Update the custom workflow

  1. Add the java.lang.Exception type. Open the Properties panel, go to the Data Items tab and expand the Local Variable section. Press the + button to add a new variable, then press the + button to add a new Data Type and press the + button to add a new structure. Search for java.lang.Exception and press OK.
  2. Add the exception variable. Finish by setting the variable name to exception. You should see the following screen:




  3. Add the Error boundary event. In the editor palette, open the folder Boundary Events and drag & drop the Error component on the SendEmailNotification task. Like this:




  4. Map the error type. Select the Error boundary event and open its Properties panel. Go to the Event tab and click on the predefined event type. Click on the + button to add a new error mapping. Enter as a Name "Notification_failed", as an Error Code the following value: org.jbpm.bpmn2.handler.WorkItemHandlerRuntimeException and as Data Type java.lang.Exception. Finish by setting the Target to the exception variable.




  5. Complete the workflow by adding the CreateLogNotification task. Drag & drop the  CreateLogNotification task and an untyped End event on the diagram. Link the Error boundary event to the CreateLogNotification task and CreateLogNotification task to the second End event. We should have the following diagram:




  6. Map the exception variable as input data of the CreateLogNotification. We should see the following screen:


Implement the CreateLogNotification task


  1. Implement the work item handler definition.

    info.magnolia.services.training.workflow.task.CreateLogNotificationWorkItemHandlerDefinition
    /**
     * This file Copyright (c) 2017 Magnolia International
     * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
     *
     *
     * This program and the accompanying materials are made
     * available under the terms of the Magnolia Network Agreement
     * which accompanies this distribution, and is available at
     * http://www.magnolia-cms.com/mna.html
     *
     * Any modifications to this file must keep this entire header
     * intact.
     *
     */
    package info.magnolia.services.training.workflow.task;
    
    import info.magnolia.module.workflow.jbpm.workitem.handler.definition.ConfiguredWorkItemHandlerDefinition;
    
    /**
     * Definition of the task {@link CreateLogNotificationWorkItemHandler}.
     *
     * @author Magnolia International Ltd.
     */
    public class CreateLogNotificationWorkItemHandlerDefinition extends ConfiguredWorkItemHandlerDefinition {
    
        /**
         * Constructor.
         */
        public CreateLogNotificationWorkItemHandlerDefinition() {
            // Assign of the default implementation
            this.setImplementationClass(CreateLogNotificationWorkItemHandler.class);
        }
    }
  2. Implement the work item handler.

    info.magnolia.services.training.workflow.task.CreateLogNotificationWorkItemHandler
    /**
     * This file Copyright (c) 2017 Magnolia International
     * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
     *
     *
     * This program and the accompanying materials are made
     * available under the terms of the Magnolia Network Agreement
     * which accompanies this distribution, and is available at
     * http://www.magnolia-cms.com/mna.html
     *
     * Any modifications to this file must keep this entire header
     * intact.
     *
     */
    package info.magnolia.services.training.workflow.task;
    
    import org.kie.api.runtime.process.WorkItem;
    import org.kie.api.runtime.process.WorkItemHandler;
    import org.kie.api.runtime.process.WorkItemManager;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * Implementation of the create log notification task.
     *
     * @author Magnolia International Ltd.
     */
    public class CreateLogNotificationWorkItemHandler implements WorkItemHandler {
        /** Logger. */
        private static final Logger LOG = LoggerFactory.getLogger(CreateLogNotificationWorkItemHandler.class);
    
        /** The exception variable name. */
        public static final String EXCEPTION = "exception";
    
        @Override
        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            // Gets the local exception variable from the process instance
            Exception error = (Exception) workItem.getParameter(EXCEPTION);
    
            LOG.error("-------------------------------------------------------");
            LOG.error("The following exception occurred while sending email notification: {}", error.getMessage());
            LOG.error("-------------------------------------------------------");
        }
    
        @Override
        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
    
        }
    }
  3. Open the class SendEmailNotificationWorkItemHandler and replace the block of code calling the send email command with the following one:

    info.magnolia.services.training.workflow.task.SendEmailNotificationWorkItemHandler
    // Executes the send email command
    try {
    	boolean ko = this.commandsManager.executeCommand(this.definition.getMailCommand(), mailParameters);
    	if (ko) {
    		throw new WorkItemHandlerRuntimeException(new Exception("Could not send email"));
    	}
    } catch (Exception e) {
    	throw new WorkItemHandlerRuntimeException(e);
    }


Configure Magnolia

  1. Add a new work item handler definition. Still in the config app, browse to the node /modules/business-workflows-scenarios. Open the folder workItemHandlers and create a sub node called createLog (same name as the one defined in the the .wid file). Enter the following properties:



Test the workflow

  1. Go the pages app and select the page called "Other".
  2. Press Publish. Follow the publication procedure. At the end, you should have received an email in your mail box. (big grin)
  3. Now go to Admin Central > Tools > Mail tools and change the port for instance.
  4. Try to publish your page again and check your logs. You should see 3 more lines.
  • No labels