Custom Event Gateways

Preface

Here you will find a short introduction into writing your own Event Gateway type.

Since you can write these in pure cfml (and Java when you want it), it is really simple to do.

There are 2 to 3 files you need to create:

  • the Gateway cfc
  • the Gateway Driver cfc
  • A listener cfc

The Gateway CFC

This is the file which contains the action you want your gateway to do.

Also, it is the file which is instantiated by Lucee when the gateway starts.

You can take the following files as an example:

  • {Lucee-install}/lib/lucee-server/context/gateway/lucee/extension/gateway/DirectoryWatcher.cfc
  • {Lucee-install}/lib/lucee-server/context/gateway/lucee/extension/gateway/MailWatcher.cfc

The example code shown underneath is a modified version of the DirectoryWatcher.cfc, which, at time of writing, is in line for reviewing at the Lucee team.

By default, you need to have the following functions:

  • An init function, which receives the necessary config data.
  • A start function, which continues to run while variables.state="running".
  • A stop and restart function
  • A getState function, which returns the current state of the gateway instance (running,stopping,stopped)
  • A sendMessage function, which will be called when the CFML sendGatewayMessage function is used.

The following is all the code you need:

<cfcomponent output="no">
    <cfset variables.logFileName="DirectoryWatcher" />
    <cfset variables.state="stopped" />
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;init&quot;</span> <span class="nv">access</span><span class="o">=</span><span class="s2">&quot;public&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;void&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;id&quot;</span> <span class="nv">required</span><span class="o">=</span><span class="s2">&quot;true&quot;</span> <span class="nv">type</span><span class="o">=</span><span class="s2">&quot;string&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;config&quot;</span> <span class="nv">required</span><span class="o">=</span><span class="s2">&quot;true&quot;</span> <span class="nv">type</span><span class="o">=</span><span class="s2">&quot;struct&quot;</span> <span class="k">default</span><span class="o">=</span><span class="s2">&quot;</span><span class="s-Interp">#structNew()#</span><span class="s2">&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;listener&quot;</span> <span class="nv">required</span><span class="o">=</span><span class="s2">&quot;true&quot;</span> <span class="nv">type</span><span class="o">=</span><span class="s2">&quot;component&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfset</span> <span class="k">var</span> <span class="nv">cfcatch</span><span class="o">=</span><span class="s2">&quot;&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cftry&gt;</span>
        <span class="nb">&lt;cfset</span> <span class="nv">variables.id</span><span class="o">=</span><span class="nv">id</span> <span class="o">/</span><span class="nb">&gt;</span>
        <span class="nb">&lt;cfset</span> <span class="nv">variables.config</span><span class="o">=</span><span class="nv">config</span> <span class="o">/</span><span class="nb">&gt;</span>
        <span class="nb">&lt;cfset</span> <span class="nv">variables.listener</span><span class="o">=</span><span class="nv">listener</span> <span class="o">/</span><span class="nb">&gt;</span>
        <span class="nt">&lt;cflog</span> <span class="na">text=</span><span class="s">&quot;init&quot;</span> <span class="na">type=</span><span class="s">&quot;information&quot;</span> <span class="na">file=</span><span class="s">&quot;#variables.logFileName#&quot;</span> <span class="nt">/&gt;</span>
        <span class="nb">&lt;cfcatch&gt;</span>
            <span class="nb">&lt;cfset</span> <span class="nf">_handleError</span><span class="p">(</span><span class="nv">cfcatch</span><span class="p">,</span> <span class="s2">&quot;init&quot;</span><span class="p">)</span> <span class="o">/</span><span class="nb">&gt;</span>
        <span class="nb">&lt;/cfcatch&gt;</span>
    <span class="nb">&lt;/cftry&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;start&quot;</span> <span class="nv">access</span><span class="o">=</span><span class="s2">&quot;public&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;void&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfset</span> <span class="k">var</span> <span class="nv">sleepStep</span><span class="o">=</span><span class="nf">iif</span><span class="p">(</span><span class="nv">variables.config.interval</span> <span class="o">lt</span> <span class="m">500</span><span class="p">,</span> <span class="s1">&#39;variables.config.interval&#39;</span><span class="p">,</span> <span class="nf">de</span><span class="p">(</span><span class="m">500</span><span class="p">))</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfset</span> <span class="k">var</span> <span class="nv">i</span><span class="o">=-</span><span class="m">1</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfset</span> <span class="k">var</span> <span class="nv">cfcatch</span><span class="o">=</span><span class="s2">&quot;&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="err">&lt;</span>--- when restart() is called, we enter this loop until the previous execution has ended. ---&gt;
        <span class="nt">&lt;cfwhile</span> <span class="na">variables</span><span class="err">.</span><span class="na">state</span> <span class="na">EQ</span> <span class="err">&quot;</span><span class="na">stopping</span><span class="err">&quot;</span><span class="nt">&gt;</span>
            <span class="nb">&lt;cfset</span> <span class="nf">sleep</span><span class="p">(</span><span class="m">10</span><span class="p">)</span><span class="nb">&gt;</span>
        <span class="nt">&lt;/cfwhile&gt;</span>
        <span class="nb">&lt;cfset</span> <span class="nv">variables.state</span><span class="o">=</span><span class="s2">&quot;running&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
        <span class="nt">&lt;cflog</span> <span class="na">text=</span><span class="s">&quot;start&quot;</span> <span class="na">type=</span><span class="s">&quot;information&quot;</span> <span class="na">file=</span><span class="s">&quot;#variables.logFileName#&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;cfwhile</span> <span class="na">variables</span><span class="err">.</span><span class="na">state</span> <span class="na">EQ</span> <span class="err">&quot;</span><span class="na">running</span><span class="err">&quot;</span><span class="nt">&gt;</span>
            <span class="nb">&lt;cftry&gt;</span>
                <span class="err">&lt;</span>--- YOUR GATEWAY ACTIONS HERE ---&gt;
                <span class="nb">&lt;cfcatch&gt;</span>
                    <span class="nb">&lt;cfset</span> <span class="nf">_handleError</span><span class="p">(</span><span class="nv">cfcatch</span><span class="p">,</span> <span class="s2">&quot;start&quot;</span><span class="p">)</span> <span class="o">/</span><span class="nb">&gt;</span>
                <span class="nb">&lt;/cfcatch&gt;</span>
            <span class="nb">&lt;/cftry&gt;</span>
            <span class="err">&lt;</span>--- sleep until the next run, but cut it into half seconds, so we can stop the gateway easily ---&gt;
            <span class="nb">&lt;cfloop</span> <span class="nv">from</span><span class="o">=</span><span class="s2">&quot;</span><span class="s-Interp">#sleepStep#</span><span class="s2">&quot;</span> <span class="nv">to</span><span class="o">=</span><span class="s2">&quot;</span><span class="s-Interp">#variables.config.interval#</span><span class="s2">&quot;</span> <span class="nv">step</span><span class="o">=</span><span class="s2">&quot;</span><span class="s-Interp">#sleepStep#</span><span class="s2">&quot;</span> <span class="nv">index</span><span class="o">=</span><span class="s2">&quot;i&quot;</span><span class="nb">&gt;</span>
                <span class="nb">&lt;cfset</span> <span class="nf">sleep</span><span class="p">(</span><span class="nv">sleepStep</span><span class="p">)</span> <span class="o">/</span><span class="nb">&gt;</span>
                <span class="nb">&lt;cfif</span> <span class="nv">variables.state</span> <span class="nv">neq</span> <span class="s2">&quot;running&quot;</span><span class="nb">&gt;</span>
                    <span class="nb">&lt;cfbreak</span> <span class="o">/</span><span class="nb">&gt;</span>
                <span class="nb">&lt;/cfif&gt;</span>
            <span class="nb">&lt;/cfloop&gt;</span>
            <span class="err">&lt;</span>--- some extra sleeping if the requested timeout is not yet completely done ---&gt;
            <span class="nb">&lt;cfif</span> <span class="nv">variables.config.interval</span> <span class="o">mod</span> <span class="nv">sleepStep</span> <span class="o">and</span> <span class="nv">variables.state</span> <span class="o">eq</span> <span class="s2">&quot;running&quot;</span><span class="nb">&gt;</span>
                <span class="nb">&lt;cfset</span> <span class="nf">sleep</span><span class="p">((</span><span class="nv">variables.config.interval</span> <span class="o">mod</span> <span class="nv">sleepStep</span><span class="p">))</span> <span class="o">/</span><span class="nb">&gt;</span>
            <span class="nb">&lt;/cfif&gt;</span>
        <span class="nt">&lt;/cfwhile&gt;</span>
        <span class="nb">&lt;cfset</span> <span class="nv">variables.state</span><span class="o">=</span><span class="s2">&quot;stopped&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;stop&quot;</span> <span class="nv">access</span><span class="o">=</span><span class="s2">&quot;public&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;void&quot;</span><span class="nb">&gt;</span>
    <span class="nt">&lt;cflog</span> <span class="na">text=</span><span class="s">&quot;stop&quot;</span> <span class="na">type=</span><span class="s">&quot;information&quot;</span> <span class="na">file=</span><span class="s">&quot;#variables.logFileName#&quot;</span><span class="nt">&gt;</span>
        <span class="nb">&lt;cfset</span> <span class="nv">variables.state</span><span class="o">=</span><span class="s2">&quot;stopping&quot;</span><span class="nb">&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;restart&quot;</span> <span class="nv">access</span><span class="o">=</span><span class="s2">&quot;public&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;void&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfif</span> <span class="nv">variables.state</span> <span class="o">EQ</span> <span class="s2">&quot;running&quot;</span><span class="nb">&gt;</span>
        <span class="nb">&lt;cfset</span> <span class="nf">stop</span><span class="p">()</span><span class="nb">&gt;</span>
    <span class="nb">&lt;/cfif&gt;</span>
    <span class="nb">&lt;cfset</span> <span class="nf">start</span><span class="p">()</span><span class="nb">&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;getState&quot;</span> <span class="nv">access</span><span class="o">=</span><span class="s2">&quot;public&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;string&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfreturn</span> <span class="nv">variables.state</span> <span class="o">/</span><span class="nb">&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;sendMessage&quot;</span> <span class="nv">access</span><span class="o">=</span><span class="s2">&quot;public&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;string&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;data&quot;</span> <span class="nv">required</span><span class="o">=</span><span class="s2">&quot;false&quot;</span> <span class="nv">type</span><span class="o">=</span><span class="s2">&quot;struct&quot;</span><span class="nb">&gt;</span>
        <span class="nb">&lt;cfreturn</span> <span class="s2">&quot;ERROR: sendMessage not supported&quot;</span><span class="nb">&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;_handleError&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;void&quot;</span> <span class="nv">access</span><span class="o">=</span><span class="s2">&quot;private&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;catchData&quot;</span> <span class="nv">required</span><span class="o">=</span><span class="s2">&quot;yes&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;functionName&quot;</span> <span class="nv">type</span><span class="o">=</span><span class="s2">&quot;string&quot;</span> <span class="nv">required</span><span class="o">=</span><span class="s2">&quot;no&quot;</span> <span class="k">default</span><span class="o">=</span><span class="s2">&quot;unknown&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nt">&lt;cflog</span> <span class="na">text=</span><span class="s">&quot;Function #arguments.functionName#: #arguments.catchData.message# #arguments.catchData.detail#&quot;</span> <span class="na">type=</span><span class="s">&quot;error&quot;</span> <span class="na">file=</span><span class="s">&quot;#variables.logFileName#&quot;</span> <span class="nt">/&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>

</cfcomponent>

I guess you noticed the comment "YOUR GATEWAY ACTIONS HERE"? That's where you add the real functionality.

The Gateway Driver

The driver is used to configure and define your Gateway.

With it, you define the form fields in the Lucee admin settings page for your gateway, and it makes sure that your gateway is listed as an available Gateway.

The Gateway Driver is a CFC file, which must be added into the directory {Lucee-install}/lib/lucee-server/context/admin/gdriver/

Gateway driver Functions

  • getClass():string. Returns the Java class name. If the gateway is Java based, then the Java class has to implement the interface "org.opencfml.eventgateway.Gateway". If it is not Java based, then this method must return an empty string or void.

  • getCFCPath():string. Returns the cfc path, when the gateway is cfc based, If it is not cfc based, then this method must return an empty string or void.

  • getLabel():string. The label (friendly name) of the gateway.

  • getDescription():string The description of the gateway

  • onBeforeUpdate(string cfcPath, string startupMode, struct custom):void. This method is invoked before the settings entered in the form are saved. This method can be used to validate the entered data.

  • onBeforeError(cfcatch):void. Invoked before an error is thrown. Can be used to throw your own error, and/or do logging.

  • getListenerCfcMode():string. Can be one of the following:

    • "none": no listener gets defined
    • "required": defining a listener is required
  • getListenerPath():string The default location of a listener cfc. (only used when the listenerCfcMode is not "none")

Example Gateway driver

<cfcomponent extends="Gateway" output="no">
    <--- The form fields which will be shown when adding a gateway instance via the Lucee admin --->
    <--- argument names (see file Gateway.cfc): displayName, name, defaultValue, required, description, type, values --->
    <cfset variables.fields = array(
    	field( "Path to file", "filepath", "", true, "The file you want to check the size for", "text"),
    	field( "Minimum file size", "minimalsize", "", true, "The minimum size of the file, in Bytes, before the Listener CFC is called", "text"),
    	field( "Interval (ms)", "interval", "60000", true, "The interval between checks, in milliseconds", "text"),
    	field( "CFC Listener Function name", "listenerFunction", "onChange", true, "Called when the file reaches the minimum file size", "text")
    ) />
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;getClass&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;string&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfreturn</span> <span class="s2">&quot;&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;getCFCPath&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;string&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfreturn</span> <span class="s2">&quot;filesizechecker.FileSizeWatcher&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;getLabel&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;string&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfreturn</span> <span class="s2">&quot;Filesize Watcher&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;getDescription&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;string&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfreturn</span> <span class="s2">&quot;Watches the filesize of a certain file&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;onBeforeUpdate&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;void&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;false&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;cfcPath&quot;</span> <span class="nv">required</span><span class="o">=</span><span class="s2">&quot;true&quot;</span> <span class="nv">type</span><span class="o">=</span><span class="s2">&quot;string&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;startupMode&quot;</span> <span class="nv">required</span><span class="o">=</span><span class="s2">&quot;true&quot;</span> <span class="nv">type</span><span class="o">=</span><span class="s2">&quot;string&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;custom&quot;</span> <span class="nv">required</span><span class="o">=</span><span class="s2">&quot;true&quot;</span> <span class="nv">type</span><span class="o">=</span><span class="s2">&quot;struct&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfset</span> <span class="k">var</span> <span class="nv">errors</span><span class="o">=</span><span class="p">[</span> <span class="p">]</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="err">&lt;</span>--- does gven file exist? ---&gt;
    <span class="nb">&lt;cfif</span> <span class="o">not</span> <span class="nf">fileExists</span><span class="p">(</span><span class="nv">arguments.custom.filepath</span><span class="p">)</span><span class="nb">&gt;</span>
        <span class="nb">&lt;cfset</span> <span class="nf">arrayAppend</span><span class="p">(</span><span class="nv">errors</span><span class="p">,</span> <span class="s2">&quot;The file [</span><span class="s-Interp">#arguments.custom.filepath#</span><span class="s2">] does not exist&quot;</span><span class="p">)</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;/cfif&gt;</span>
    <span class="err">&lt;</span>--- interval ---&gt;
    <span class="nb">&lt;cfif</span> <span class="o">not</span> <span class="nf">IsNumeric</span><span class="p">(</span><span class="nv">custom.interval</span><span class="p">)</span> <span class="o">or</span> <span class="nv">custom.interval</span> <span class="o">LT</span> <span class="m">1</span> <span class="o">or</span> <span class="nf">int</span><span class="p">(</span><span class="nv">custom.interval</span><span class="p">)</span> <span class="nv">neq</span> <span class="nv">custom.interval</span><span class="nb">&gt;</span>
        <span class="nb">&lt;cfset</span> <span class="nf">arrayAppend</span><span class="p">(</span><span class="nv">errors</span><span class="p">,</span> <span class="s2">&quot;The interval [</span><span class="s-Interp">#custom.interval#</span><span class="s2">] must be a numeric value greater than 0&quot;</span><span class="p">)</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;/cfif&gt;</span>
    <span class="err">&lt;</span>--- minimalsize ---&gt;
    <span class="nb">&lt;cfif</span> <span class="o">not</span> <span class="nf">IsNumeric</span><span class="p">(</span><span class="nv">custom.minimalsize</span><span class="p">)</span> <span class="o">or</span> <span class="nv">custom.minimalsize</span> <span class="o">LT</span> <span class="m">1</span> <span class="o">or</span> <span class="nf">int</span><span class="p">(</span><span class="nv">custom.minimalsize</span><span class="p">)</span> <span class="nv">neq</span> <span class="nv">custom.minimalsize</span><span class="nb">&gt;</span>
        <span class="nb">&lt;cfset</span> <span class="nf">arrayAppend</span><span class="p">(</span><span class="nv">errors</span><span class="p">,</span> <span class="s2">&quot;The Minimum file size [</span><span class="s-Interp">#custom.minimalsize#</span><span class="s2">] must be a numeric value greater than 0&quot;</span><span class="p">)</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;/cfif&gt;</span>
    <span class="nb">&lt;cfif</span> <span class="nf">arrayLen</span><span class="p">(</span><span class="nv">errors</span><span class="p">)</span><span class="nb">&gt;</span>
        <span class="nb">&lt;cfthrow</span> <span class="nv">message</span><span class="o">=</span><span class="s2">&quot;The following error(s) occurred while validating your input: &lt;ul</span><span class="nb">&gt;</span><span class="nt">&lt;li&gt;</span>#arrayToList(errors, &#39;<span class="nt">&lt;/li&gt;&lt;li&gt;</span>&#39;)#<span class="nt">&lt;/li&gt;&lt;/ul&gt;</span>&quot; /&gt;
    <span class="nb">&lt;/cfif&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;getListenerCfcMode&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;string&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span> <span class="nv">hint</span><span class="o">=</span><span class="s2">&quot;Returns either &#39;none&#39; or &#39;required&#39;&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfreturn</span> <span class="s2">&quot;required&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;getListenerPath&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;string&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span> <span class="nv">hint</span><span class="o">=</span><span class="s2">&quot;Returns the path to the default Listener cfc&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfreturn</span> <span class="s2">&quot;filesizechecker.FileBackuper&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>

</cfcomponent>

Also see the file Gateway.cfc, which extends the functionality of this example ()

The Listener CFC

Most gateways need a Listener CFC to respond to events occurring in the Gateway instance. For example, if the Mail watcher finds new email in the mailbox, then it needs to do something with that event; it calls a method (function) of the Listener CFC. The path to your listener CFC must be given as an argument when you create or update a gateway instance. The contents of the CFC is completely up to you, as long as it has a public function that can be called by the Gateway.

Example

Let's say our Gateway type to create is a "filesize checker", which checks a file for a minimum filesize. If the file's size has the minimum filesize, then we will call the listener CFC. First, we'll create the listener CFC:

<cfcomponent output="no">
	<cffunction name="onBigFilesize" access="public" output="no" returntype="void">
	    <cfargument name="filepath" required="true" type="string" />
	    <cfargument name="size" required="true" type="numeric" />
    <span class="err">&lt;</span>--- create a non-existing zipfile path---&gt;
    <span class="nb">&lt;cfset</span> <span class="k">var</span> <span class="nv">zipFileName</span><span class="o">=</span><span class="nv">a</span> <span class="nv">rguments.filepath</span> <span class="o">&amp;</span> <span class="s2">&quot;.zip&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfset</span> <span class="k">var</span> <span class="nv">nr</span><span class="o">=</span><span class="m">1</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nt">&lt;cfwhile</span> <span class="na">fileExists</span><span class="err">(</span><span class="na">zipFileName</span><span class="err">)</span><span class="nt">&gt;</span>
	    <span class="nb">&lt;cfset</span> <span class="nv">zipFileName</span><span class="o">=</span><span class="nv">a</span> <span class="nv">rguments.filepath</span> <span class="o">&amp;</span> <span class="s2">&quot;.</span><span class="s-Interp">#nr#</span><span class="s2">.zip&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
	    <span class="nb">&lt;cfset</span> <span class="o">++</span><span class="nv">nr</span> <span class="o">/</span><span class="nb">&gt;</span>
	<span class="nt">&lt;/cfwhile&gt;</span>
    <span class="nb">&lt;cftry&gt;</span>
        <span class="err">&lt;</span>--- zip the file ---&gt;
        <span class="nb">&lt;cfzip</span> <span class="nv">action</span><span class="o">=</span><span class="s2">&quot;zip&quot;</span> <span class="nv">source</span><span class="o">=</span><span class="s2">&quot;</span><span class="s-Interp">#arguments.filepath#</span><span class="s2">&quot;</span> <span class="nv">file</span><span class="o">=</span><span class="s2">&quot;</span><span class="s-Interp">#zipFileName#</span><span class="s2">&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
        <span class="err">&lt;</span>--- log the zip action ---&gt;
        <span class="nt">&lt;cflog</span> <span class="na">text=</span><span class="s">&quot;Backed up #arguments.filepath# to #zipFileName#&quot;</span> <span class="na">type=</span><span class="s">&quot;information&quot;</span> <span class="na">file=</span><span class="s">&quot;#variables.logFileName#&quot;</span> <span class="nt">/&gt;</span>
        <span class="err">&lt;</span>--- now delete the file ---&gt;
        <span class="nb">&lt;cffile</span> <span class="nv">action</span><span class="o">=</span><span class="s2">&quot;delete&quot;</span> <span class="nv">file</span><span class="o">=</span><span class="s2">&quot;</span><span class="s-Interp">#arguments.filepath#</span><span class="s2">&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
        <span class="nb">&lt;cfcatch&gt;</span>
            <span class="nb">&lt;cfset</span> <span class="nf">_handleError</span><span class="p">(</span><span class="nv">cfcatch</span><span class="p">,</span> <span class="s2">&quot;onBigFilesize&quot;</span><span class="p">)</span> <span class="o">/</span><span class="nb">&gt;</span>
        <span class="nb">&lt;/cfcatch&gt;</span>
    <span class="nb">&lt;/cftry&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>
<span class="nb">&lt;cffunction</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;_handleError&quot;</span> <span class="nv">returntype</span><span class="o">=</span><span class="s2">&quot;void&quot;</span> <span class="nv">access</span><span class="o">=</span><span class="s2">&quot;private&quot;</span> <span class="nv">output</span><span class="o">=</span><span class="s2">&quot;no&quot;</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;catchData&quot;</span> <span class="nv">required</span><span class="o">=</span><span class="s2">&quot;yes&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nb">&lt;cfargument</span> <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;functionName&quot;</span> <span class="nv">type</span><span class="o">=</span><span class="s2">&quot;string&quot;</span> <span class="nv">required</span><span class="o">=</span><span class="s2">&quot;no&quot;</span> <span class="k">default</span><span class="o">=</span><span class="s2">&quot;unknown&quot;</span> <span class="o">/</span><span class="nb">&gt;</span>
    <span class="nt">&lt;cflog</span> <span class="na">text=</span><span class="s">&quot;Function #arguments.functionName#: #arguments.catchData.message# #arguments.catchData.detail#&quot;</span> <span class="na">type=</span><span class="s">&quot;error&quot;</span> <span class="na">file=</span><span class="s">&quot;#variables.logFileName#&quot;</span> <span class="nt">/&gt;</span>
<span class="nb">&lt;/cffunction&gt;</span>

</cfcomponent>

We will save the file as {Lucee-install}/lib/lucee-server/context/gateway/filesizechecker/FileBackuper.cfc

Now we will add the "file size check" functionality into our gateway cfc. We'll replace "YOUR GATEWAY ACTIONS HERE" with the following:

<cfset var qFile = "" />
<--- get the file's size by using cfdirectory --->
<cfdirectory action="list" directory="#getDirectoryFromPath(variables.config.filepath)#"
	filter="#getFileFromPath(variables.config.filepath)#" name="qFile" />

<--- call the listener CFC if the file size meets the minimum requirement ---> <cfif qFile.recordcount and qFile.size gte variables.config.minimalsize> <cfset variables.listener[variables.config.listenerFunction]( qFile.directory & server.separator.file & qFile.name , qFile.size ) /> </cfif>

We will save the file as {Lucee-install}/lib/lucee-server/context/gateway/filesizechecker/FileSizeWatcher.cfc

Btw: the variables.listener and variables.config variables did not just come falling from the sky; instead, it was saved to the variables scope in the init() function.

Lastly, we need to create the Gateway driver. We can use the Gateway driver code which was shown before, and then save it as {Lucee-install}/lib/lucee-server/context/admin/gdriver/FileSizeWatcher.cfc

Now we are almost good to go! We do need to restart Lucee to have it pick up the new Gateway driver. So just go to the server admin, click on the menu-item "Restart", and then hit the "Restart Lucee" button.

We can add an instance of our new Gateway type now! You can do it by using cfadmin like this:

<cfadmin action="updateGatewayEntry" type="server" password="server-admin-password"
	startupMode="automatic"
	id="zipLargeLogFiles"
	class=""
	cfcpath="filesizechecker.FileSizeWatcher"
	listenerCfcPath="filesizechecker.FileBackuper"
	custom='#{
		  filepath = "C:/mysite/logs/failedlogins.log"
		, listenerFunction = "onBigFilesize"
		, minimalsize = 100000
		, interval = 60000
	}#'
	readOnly=false
/>

Interval: time in milliseconds to wait between each check Minimalsize: the minimum filesize in bytes

After executing the cfadmin code, or going through the admin screens, you should now have an instance of your own Event Gateway type running!

When creating a Socket gateway or an Instant messaging gateway, you will need to do a bit more coding, but hopefully this instruction helped you out!

See also