Trace:
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision Next revision Both sides next revision | ||
workflow:custom_function [2019/03/13 14:28] admin created |
workflow:custom_function [2019/03/13 14:46] admin |
||
---|---|---|---|
Line 2: | Line 2: | ||
~~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 41: | Line 41: | ||
} | } | ||
</file> | </file> | ||
+ | The interface already has an abstract implementation and so it's really super simple to create a new function. Our example will send an email, so it't 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("The SMTP server configuration was not found in the application zone (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 straight forward. 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. 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 you new function. | ||
+ | |||
+ |