You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

DirectoryWatcher Event Gateway: Ditching the Scheduler

A textbook case for using the gateway

I remember sitting at a ColdFusion conferences (probably CFUN) back when CFMX 7 was still in its pre-beta stage and watching as Ben Forta revealed some of the new features. There was a mixed reaction when he talked about event gateways.

While the idea of having messaging built into ColdFusion seemed appealing to most of the audience, the idea of watching a directory for its content to change wasn't as convincing. As I looked around at people's faces, I saw a mixture of excitement, inspiration, and blank stares.

I would guess the blank stares belonged to those for whom directories are relatively static things. Files on the server only change at the behest of a developer, so what's the big deal about having the server watch to see when they change? Let's face it: directory listings are pretty boring on first consideration.

I, on the other hand, couldn't wait to get my hands on CFMX 7 in production, because the DirectoryWatcher event gateway promised to solve a problem that had become all too familiar at that point in my career. I'll bet it's a situation that you may have run into yourself at some point, though I'm sure the implementation details will be a bit different in your environment.

The project we were working on at that time involved interfaces with two other systems. For the first system, we had a very smooth interface that employed a Data Migration Zone (DMZ) database to allow our Oracle database to exchange data with the other system's Oracle database without either of us needing authorization to the other system. It all ran beautifully and automatically.

The second system, though, was a bit less elegant. Our interface on that end was done through the exchange of XML datasets between the other system (Microsoft SQL Server) and ours (Oracle). While the XML part of the equation was quite nice, the channel that had been chosen for transfers was FTP. So the other system was allowed to upload files to our server using an FTP script. To import the XML data from an uploaded file into our system we ran a CFML template that would read any files in the directory, parse the XML, insert the data into our system, and move the file to a permanent directory on the server. This template was scheduled to run once every 15 minutes, using the CF Scheduler. Figure 1 shows how the process worked.

Whenever the customer system needed to send us data, it would generate an XML dataset, save it according to an agreed-on naming convention, and send it to our server via FTP. Once the dataset was on our server, the scheduled job would detect the new file the next time the scheduled job ran, and handle it accordingly.

Now, considering possible alternatives (like e-mail or even snail mail), our scheduled template solution wasn't bad at all. It worked. However, the schedule meant that inbound data might sit in a queue of sorts (the FTP directory) for up to 15 minutes, waiting on the scheduled template to run. That's why I was immediately interested in the DirectoryWatcher event gateway. I wanted a way to get rid of the time lag between when we got a dataset and when it got processed into the database.

In fact this situation turned out to be a textbook case for using the gateway. We already had the process in place and tested; the only thing we had to do was set up the gateway to kick off the process instead of having it wait for the next scheduled time. All we had to do was set up the gateway and write a configuration file for it, and a CFC to handle the events.

Setting up the DirectoryWatcher is an exercise in simplicity. In the ColdFusion Administrator, select Gateway Instances under the Event Gateways menu item on the left, and fill out the form for the new gateway instance. An example is shown in Figure 1.

The Gateway ID is an arbitrary string. I used "ftpSentry" since I wanted this instance to watch over my FTP directory. The Gateway Type is, of course, DirectoryWatcher. CFC Path and Configuration File are full paths to the gateway's handler CFC and configuration file, respectively. In the example, I used DirectoryWatcher.cfc and DirectoryWatcher.conf, respectively.

After clicking OK, the new instance shows up in the list of Configured ColdFusion Event Gateway Instances, as shown in Figure 2 (the new instance is highlighted there). Note that it's not yet running - we'll take care of that after we write the handler CFC and configuration file. Also, I named the configuration file with a .conf extension - a bit of Linux influence sneaking in there - where the examples use .cfg. ColdFusion doesn't care what extension we use.

The configuration file for a DirectoryWatcher gateway is very simple - just name=value pairs, each on its own line. At a minimum, it contains the directory we want to watch in the form directory=c:/ftpInbox. The other values are optional and we didn't need any of them for this example, but I'll mention them briefly for reference purposes.

The recurse option allows the gateway to watch all sub-directories of the specified directory. Its default value is no. If it's set to yes, all sub-directories of the specified directory will be watched.

The interval option specifies how long to wait between checks of the directory. Its default is 60000 (60 seconds). The extensions option specifies which filename extensions to watch. Its default is * for all extensions. If we want to watch more than one extension, the value should be a comma-delimited list. So if we wanted to watch for XML and CSV files, we would need extensions=xml,csv.

The xFunction options specify which function in the gateway's CFC will be called in response to an action in the directory. They are changeFunction, addFunction, and deleteFunction and their default values are onChange, onAdd, and onDelete, respectively.

For simplicity's sake, we used all the defaults, so our configuration file only has one line: directory=c:/ftpInbox. The only thing left to do now is set up the handler CFC for the gateway. Since we already had the code in place to handle our import tasks, the CFC really only needed one function - onAdd - to call that code. But I liked the idea of monitoring the FTP directory for all file activities and keeping a log, so I started with the sample CFC from the DirectoryWatcher example that comes with CFMX and added a bit of code to the onAdd function to suit our needs. The code is in Listing 1.

Each event triggers its corresponding function, and each function writes a line to the ftpSentry log. In addition, the onAdd event uses <cfmodule> to call the desired template, and writes an error message to the log if any errors are thrown. Some example entries from the log are shown in Listing 2. You can see how the file ASDO200607112876.xml was added to the directory then deleted a minute later. This is due to the automated process reading the file, adding its data to the database then moving the file to an archive directory. (see Figure 3)

So using the DirectoryWatcher event gateway improved our customer response time by reducing the scheduled wait from a 15-minute maximum to one-minute maximum. The other benefit is that our handler code doesn't fire unless a change is made to the directory. Under the scheduler scenario, our process read the directory every 15 minutes and ran some logic to determine if there were new files present. If there were, it would call the template to read and insert the data and move the file to the archive directory. The new approach with the event gateway is much simpler in terms of the code we had to write and more efficient in processing terms.

Of course, this is just one application of the DirectoryWatcher event gateway. You can use instances of it in your own system to trigger all manner of processes based on the activity on your server. For example, you could monitor a directory for file-size limits, and remove any new files after the directory reaches the limit. Or maybe you want to remove older files first, providing space for new files. Really the sky's the limit in terms of file manipulation. The DirectoryWatcher just gives you a window into your filesystem so you can automate these tasks.

More Stories By Jeff Peters

Jeff Peters works for Open Source Data Integration Software company XAware.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

@ThingsExpo Stories
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
"Cloud Academy is an enterprise training platform for the cloud, specifically public clouds. We offer guided learning experiences on AWS, Azure, Google Cloud and all the surrounding methodologies and technologies that you need to know and your teams need to know in order to leverage the full benefits of the cloud," explained Alex Brower, VP of Marketing at Cloud Academy, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clar...
In his session at 21st Cloud Expo, Carl J. Levine, Senior Technical Evangelist for NS1, will objectively discuss how DNS is used to solve Digital Transformation challenges in large SaaS applications, CDNs, AdTech platforms, and other demanding use cases. Carl J. Levine is the Senior Technical Evangelist for NS1. A veteran of the Internet Infrastructure space, he has over a decade of experience with startups, networking protocols and Internet infrastructure, combined with the unique ability to it...
"IBM is really all in on blockchain. We take a look at sort of the history of blockchain ledger technologies. It started out with bitcoin, Ethereum, and IBM evaluated these particular blockchain technologies and found they were anonymous and permissionless and that many companies were looking for permissioned blockchain," stated René Bostic, Technical VP of the IBM Cloud Unit in North America, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Conventi...
Gemini is Yahoo’s native and search advertising platform. To ensure the quality of a complex distributed system that spans multiple products and components and across various desktop websites and mobile app and web experiences – both Yahoo owned and operated and third-party syndication (supply), with complex interaction with more than a billion users and numerous advertisers globally (demand) – it becomes imperative to automate a set of end-to-end tests 24x7 to detect bugs and regression. In th...
Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to work together. The number of software platforms, apps, hardware and connectivity standards is creating paralysis among businesses that are afraid of being locked into a solution. EdgeX Foundry is unifying the community around a common IoT edge framework and an ecosystem of interoperable components.
"MobiDev is a software development company and we do complex, custom software development for everybody from entrepreneurs to large enterprises," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Large industrial manufacturing organizations are adopting the agile principles of cloud software companies. The industrial manufacturing development process has not scaled over time. Now that design CAD teams are geographically distributed, centralizing their work is key. With large multi-gigabyte projects, outdated tools have stifled industrial team agility, time-to-market milestones, and impacted P&L stakeholders.
"Akvelon is a software development company and we also provide consultancy services to folks who are looking to scale or accelerate their engineering roadmaps," explained Jeremiah Mothersell, Marketing Manager at Akvelon, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
"Space Monkey by Vivent Smart Home is a product that is a distributed cloud-based edge storage network. Vivent Smart Home, our parent company, is a smart home provider that places a lot of hard drives across homes in North America," explained JT Olds, Director of Engineering, and Brandon Crowfeather, Product Manager, at Vivint Smart Home, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Coca-Cola’s Google powered digital signage system lays the groundwork for a more valuable connection between Coke and its customers. Digital signs pair software with high-resolution displays so that a message can be changed instantly based on what the operator wants to communicate or sell. In their Day 3 Keynote at 21st Cloud Expo, Greg Chambers, Global Group Director, Digital Innovation, Coca-Cola, and Vidya Nagarajan, a Senior Product Manager at Google, discussed how from store operations and ...
"There's plenty of bandwidth out there but it's never in the right place. So what Cedexis does is uses data to work out the best pathways to get data from the origin to the person who wants to get it," explained Simon Jones, Evangelist and Head of Marketing at Cedexis, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
SYS-CON Events announced today that Telecom Reseller has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.
It is of utmost importance for the future success of WebRTC to ensure that interoperability is operational between web browsers and any WebRTC-compliant client. To be guaranteed as operational and effective, interoperability must be tested extensively by establishing WebRTC data and media connections between different web browsers running on different devices and operating systems. In his session at WebRTC Summit at @ThingsExpo, Dr. Alex Gouaillard, CEO and Founder of CoSMo Software, presented ...
WebRTC is great technology to build your own communication tools. It will be even more exciting experience it with advanced devices, such as a 360 Camera, 360 microphone, and a depth sensor camera. In his session at @ThingsExpo, Masashi Ganeko, a manager at INFOCOM Corporation, introduced two experimental projects from his team and what they learned from them. "Shotoku Tamago" uses the robot audition software HARK to track speakers in 360 video of a remote party. "Virtual Teleport" uses a multip...
A strange thing is happening along the way to the Internet of Things, namely far too many devices to work with and manage. It has become clear that we'll need much higher efficiency user experiences that can allow us to more easily and scalably work with the thousands of devices that will soon be in each of our lives. Enter the conversational interface revolution, combining bots we can literally talk with, gesture to, and even direct with our thoughts, with embedded artificial intelligence, whic...
SYS-CON Events announced today that Evatronix will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Evatronix SA offers comprehensive solutions in the design and implementation of electronic systems, in CAD / CAM deployment, and also is a designer and manufacturer of advanced 3D scanners for professional applications.
Leading companies, from the Global Fortune 500 to the smallest companies, are adopting hybrid cloud as the path to business advantage. Hybrid cloud depends on cloud services and on-premises infrastructure working in unison. Successful implementations require new levels of data mobility, enabled by an automated and seamless flow across on-premises and cloud resources. In his general session at 21st Cloud Expo, Greg Tevis, an IBM Storage Software Technical Strategist and Customer Solution Architec...
To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...