Documentation

Trace:

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revision Both sides next revision
workflow:custom_function [2019/03/13 14:28]
admin created
workflow:custom_function [2021/03/18 23:10]
admin
Line 1: Line 1:
 ~~NOTRANS~~ ~~NOTRANS~~
-~~Title: Custom ​function~~+~~Title: Custom ​Function~~
  
-A function is behind ​the code block behind a task. It's very simple to create a new function because a function is defined as a single ​Java interface:+A function is the code block behind a task. It's very simple to create a new function because a function is defined as simple ​Java interface:
  
 <file java IFunction.java>​ <file java IFunction.java>​
Line 8: Line 8:
 { {
     /**     /**
-     ​* ​The current workflow step. +     ​* ​Gets the current workflow step. 
-     * @return ​The current workflow step.+     ​* ​ 
 +     * @return ​the current workflow step.
      */      */
     public IWorkflowStep getWorkflowStep();​     public IWorkflowStep getWorkflowStep();​
Line 16: Line 17:
      * Initializes the function.      * Initializes the function.
      ​* ​      ​* ​
-     * @param ​pWorkflow ​the current workflow step+     * @param ​pStep the current workflow step
      */      */
-    public void init(IWorkflowStep ​pWorkflow);+    public void init(IWorkflowStep ​pStep);
  
     /**     /**
      * Runs the function.      * Runs the function.
-     * If the function returns true, the function is fully done, and the workflow jumps to the next step. +     * If the function returns ​<​code>​true</​code>​, the function is fully done, and the workflow jumps to the next step. 
-     * In case there are more then one next steps, the finish function of IWorkflowStep has to be called. +     * In case there are more than one next step, the finish function of IWorkflowStep has to be called. 
-     ​* ​An exception will be thrown ​otherwise+     ​* ​Otherwise, an exception will be thrown. 
-     * If finish is already called, it does not matter whether true or false is returned.+     * If finish is already called, it does not matter whether ​<​code>​true</​code> ​or <​code>​false</​code> ​is returned.
      ​* ​      ​* ​
-     * @return true, if the function is done.+     * @return ​<​code>​true</​code>​, if the function is done.
      * @throws Throwable if function fails.      * @throws Throwable if function fails.
      */      */
Line 33: Line 34:
  
     /**     /**
-     * Notifiesthat the workflow is cancelled.+     * Notifies that the workflow is cancelled.
      ​* ​      ​* ​
      * @throws Throwable if canceling fails.      * @throws Throwable if canceling fails.
Line 41: Line 42:
 } }
 </​file>​ </​file>​
 +The interface already has an abstract implementation,​ so it's really simple to create a new function. Our example will send an email, so it's the EmailFunction:​
 +
 +<file java EmailFunction.java>​
 +public class EmailFunction extends AbstractFunction ​
 +{
 +    @Override
 +    public boolean run() throws Throwable ​
 +    {
 +        XmlNode smtp = getApplicationZone().getConfig().getNode("/​application/​mail/​smtp"​);​
 +
 +        if (smtp == null) 
 +        {
 +            throw new IllegalArgumentException("​SMTP config not found (path /​application/​mail/​smtp)!"​);​
 +        }
 +
 +        String host = smtp.getNodeValue("​host"​);​
 +        String port = smtp.getNodeValue("​port"​);​
 +        String username = smtp.getNodeValue("​username"​);​
 +        String password = smtp.getNodeValue("​password"​);​
 +        boolean tlsEnabled = Boolean.parseBoolean(smtp.getNodeValue("​tlsenabled"​));​
 +
 +        String defaultsender = smtp.getNodeValue("​defaultsender"​);​
 +
 +        String from = getNotNullEvaluatedParameter("​From"​);​
 +        String to = getNotNullEvaluatedParameter("​To"​);​
 +        String cC = getEvaluatedParameter("​CC"​);​
 +        String bCC = getEvaluatedParameter("​BCC"​);​
 +        String subject = getNotNullEvaluatedParameter("​Subject"​);​
 +        String body = getEvaluatedParameter("​Body"​);​
 +
 +        log("​Mailserver:​ " + host + ":"​ + port + "/"​ + username);
 +
 +        Mail mail = new Mail(host, port, username, password);
 +        mail.setTLSEnabled(tlsEnabled);​
 +
 +        mail.send(CommonUtil.nvl(from,​ defaultsender),​ to, cC, bCC, subject, body, null, null);
 +
 +        log("​From:​ " + CommonUtil.nvl(from,​ defaultsender));​
 +        log("​To:​ " + to);
 +        log("​Subject:​ " + subject);
 +
 +        return true;
 +    }
 +
 +}
 +</​file> ​
 +
 +The implementation is straightforward. It extends the AbstractFunction and implements run method. The email settings will be read from the [[jvx:​common:​setup:​zones|application configuration]],​ and the email settings will be read from the input parameters. Here's an example:
 +
 +<file xml>
 +<​application>​
 +  ...
 +  <​mail>​
 +    <​smtp>​
 +      <​host>​smtp.yourserver.com</​host>​
 +      <​port>​587</​port>​
 +      <​username>​username</​username>​
 +      <​password>​password</​password>​
 +      <​tlsenabled>​true</​tlsenabled>​
 +      <​defaultsender>​Noreply &​lt;​noreply@yourserver.com&​gt;</​defaultsender>​
 +    </​smtp>​
 +  </​mail>​
 +</​application>  ​
 +</​file>​
 +
 +Finally, the email will be sent.
 +
 +But there'​s still one open question: how do we define the input parameters?
 +
 +Simply use the functions screen and enter all required information:​
 +
 +{{:​workflow:​wf_emailfunction.png?​nolink|}}
 +
 +That's it! Start using your new function.
This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information