Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

27
Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting

Transcript of Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Page 1: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Using Event Gateways with CFMX7

By Jeff Tapper

Tapper.net Consulting

Page 2: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Agenda

Introduction to Event Gateways Working with Events Configuring Gateways Listener Applications Broadcaster Applications Summary Questions

Page 3: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Who Am I

Jeff Tapper ([email protected]) Chief Technologist Tapper.net

Consulting Working with CF since 1995 (v 1.5) Authored 6 books about CF and/or

Flash Certified MM Instructor for all CF,

Flashand Flex courses

Page 4: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Event Gateways

Allow access to CF without HTTP Call CF code from anything Java can react to Such as:

• Instant Messages

• Text Messages from Phones

• Java Messaging Service

• File System Changes

• Etc.

Page 5: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

How it works

Message Generator Event Gateway (Java) Event Gateway Services (CF) Listener CFC

Page 6: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Understanding the Architecture

Message Generator/Receiver•SMSC Server•IM Provider•Etc.

Event

Event

Event Gateway

CFEvent

GatewayServices

ListenerCFC

CFEvent

CFEvent

CFEvent

Message ListenerCFC or

CFM Page

<CFML>

Page 7: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Gateway Types

Several gateway types are provided• SMS• XMPP• CFML• JMS• Directory Watcher• Etc.

Custom Gateway types can be built as needed

Page 8: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Gateway Types

Event Listeners Creates an event structure and queues

it in the Gateway

Page 9: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Gateway Instance

Relates a gateway type to a Listener CFC

Each instance properties set with Configuration file

Page 10: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Application Types

Listener Applications• CFC reacts to outside events

Broadcaster Application• CFML initiates a message to be sent

Page 11: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Listener Applications

Listener CFC called by gateway instance

Method specified in Configuration is called

CFEvent Structure passed as argument

Page 12: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Listener CFC

Contains methods for handling events Optionally responds to sender with

CFReturn Method is passed CFEvent Struct

RFAFileWatch.cfc

Page 13: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

CFEvent Structure

Java Object provided by gateway• gatewayId – reference to Instance• data – struct of incoming message

• customized for each Event Type• originator – who sent message

• SMS phone #• IM handle• Etc.

• gateway Type – reference to gateway which heard the event

Page 14: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Configuring a Gateway

Create config file Build CFC with appropriate methods Add Gateway Instance to CFAdmin Start the Gateway

CFAdmin

Page 15: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Setting Config file

DirectoryWatcherGateway settings are defined in a Config file --{cfroot}\gateway\config\directory-watcher.cfg

Specifies:• directory • recurse• extensions• Interval• addFunction• changeFunction• deleteFunction

RFAUploadDirectory-watcher.cfg

Page 16: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Config File gateway/config/directory-watcher.cfg

# The directory you want to watch. If you are entering a Windows path either use forward slashes (C:/mydir) or escape the back slashes (C:\\mydir).

directory=# Should we watch the directory and all subdirectories too Default is no. Set to 'yes' to do the recursion.

recurse=no# The interval between checks, in milliseconds Default is 60 seconds

interval=60000# The comma separated list of extensions to match. Default is * - all files

extensions=*# CFC Function for file Change events Default is onChange, set to nothing if you don't want to see these events

changeFunction=onChange# CFC Function for file Add events Default is onAdd, set to nothing if you don't want to see these events

addFunction=onAdd# CFC Function for file Delete events Default is onDelete, set to nothing if you don't want to see these events

deleteFunction=onDelete

Page 17: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

DirWatch.cfc

<cfcomponent><cffunction name="onAdd" output="no"> <cfargument name="CFEvent"

type="struct" required="yes"> <cfset data=CFEvent.data><cflog file="MydirWatcher" application="No" text="ACTION: #data.type#; FILE: #data.filename#; TIME: #timeFormat(data.lastmodified)#">

</cffunction> </cfcomponent>

Page 18: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Customizing Directory Watcher

Problem: Needed easier way to remotely add songs to “Radio Free Astoria” play list

Solution: Using a modified Directory Watcher, I am now able to ftp songs to the server, have them moved to the appropriate directory and automatically added to play list

Page 19: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

RFA Directory Watcher Flow

New mp3ftp’d to server Directory

Watchertriggers CFC

File moved

Added toplaylist

ConfirmationSent

Page 20: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Broadcaster Applications

CFML can be used to broadcast out custom events

Some examples• Send CFML Event to start new thread• SMS site admin about system errors• Send new events to JMS messaging

queue• Send Instant Message about application

Page 21: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Sending an Event

CFMX7 introduced new sendGatewayMessage() function

Takes two arguments• gatewayID - gateway instance configured in

CFAdmin

• Message – struct containg message data Return type varies based on gateway. Often

true or false indicating success

Page 22: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

sendGatewayMessage

<cffunction name="sendConfirm" access="private" output="no"><cfargument name="songName" type="string"><cfset var struct = structNew()><cfset struct.buddyId = "theAstorian"><cfset struct.message = "Song #songName# added"><cfset result =

sendGatewayMessage("RFAXMPP",struct)><cfif result neq 'ok'> <cflog file="rfafileupload" type="Information" text="#result#"></cfif>

</cffunction>

Page 23: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

getGatewayHelper()

Returns an object containing details about the gateway

Instant Message Helper ships with CFMX7 Example:

myHelper = getGatewayHelper("rfaxmpp");

status = myHelper.getBuddyInfo("[email protected]");

Page 24: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Communicating with IM

Gateway Instance

XMPP Gateway

Jabber Network

AIM/MSN/Yahoo

Transport

AIM/MSNor YahooNetwork

Or, you can use JBuddy CF from Zion Software*

Page 25: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Where to learn more?

CFUnited• Damon Cooper - CFMX 7 Mobile SMS Applications Made

Easy • Tom Jordahl – Advanced ColdFusion Event Gateways

Lots of new docs JBuddyCF – http://www.zionsoftware.com Blogs:

• boyzoid.com• jeff.mxdj.com• weblogs.macromedia.com/mxna/

Page 26: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Summary

Gateways allow for CFML to be triggered by things other than HTTP requests

Gateways react to certain events and calls methods in a CFC

Gateway settings are specified in a Config File

Gateways can be registered using the ColdFusion Administrator

Page 27: Using Event Gateways with CFMX7 By Jeff Tapper Tapper.net Consulting.

Questions

?