<< February, 2019 >>
SMTWTFS
12
3456789
10111213141516
17181920212223
2425262728
Related Links
Search Blog

Categories
Archives
Photo Albums
RSS

Powered by
BlogCFM v1.1

16 February 2006

Compiling Custom Event Gateways For Dummies

First off, let me say that I am not a java programmer.  I've done a lot of java integration in coldfusion, but that has little to do with actual java programming.

I recently had a need for a very simple event gateway - one that would basically make a CFC method call every XX seconds.  Kind of like a scheduled task, except scheduled tasks unnecessarily tax the web server and run no more often than once every minute.

There were many "GOTCHYAS" along the way in this experience and I want to share some of them.  If you, like me, are not a real java programmer, you may find some of these tips useful.

I chose to start with the DirectoryWatcher gateway - an actual working gateway that read a config file where things occurred on an interval.  In the section where it read the properties file, I removed all the properities I didn't want, leaving only two... "interval" and "callFunction" (which was originally named "changeFunction"). Then I removed all the unnecessary code that involved checking for the existence of directories and getting directory listings and such.  Then I tried to compile:

Problem #1 - no java compiler

Coldfusion includes the Java Runtime Environment (JRE), but not the SDK, which is what you need to compile things.  The solution here was simple - download the JDK from http://java.sun.com.  Make sure you get the same JDK version that coldfusion is running - in my case, 1.4.2 (technically, 1.4.2_05_b04, but Sun only made 1.4.2_10 available, the latest release, and that seemed to work).

So I got it installed and tried to compile.  And I got 24 errors.  I didn't even realize there were that many lines in the code!  Most of them said "Can't resolve symbol" or "Unable to resolve symbol" or something.  I did a little googling and found out that I needed a class path.

Problem #2 - defining your class path

Well, the java compiler is kinda stupid, and doesn't know where all the tools on your system are located, so you have to tell it by setting a classpath environment variable, or putting the classpath into the command line.  To figure out what jar files had to be included, I looked at $CFHOME/gateway/src/build.xml and saw 4 jars that had to be included, so I set my path like this:

PLATFORM NOTE:  I'm using Linux, and my shell is bash.  I'm not here to tell you how to set environment variables in UNIX shells or windows.  If you need help properly setting environment variables, you might find this classpath tutorial helpful.

export CFHOME="/opt/coldfusionmx7"
export CLASSPATH=".:/root/j2sdk/lib/tools.jar:$CFHOME/lib/cfusion.jar:$CFHOME/lib/log4j.jar:$CFHOME/runtime/lib/jrun.jar:$CFHOME/gateway/lib/examples.jar"


A couple important things to note:

  • each entry in the classpath is separated by a colon, and there should be no colon at the beginning or the end of the classpath
  • the first entry should always be a period, telling java and the java compiler to always look in the current directory.
  • The classpath must always include the tools.jar file, or pretty much nothing will work!  I forgot this part initially and it took me a bit to figure it out.

Okay, so my classpath is now set and I tried again.

Problem #3 - weird bug involving abstracting and overriding the accept() method

The DirectoryWatcher gateway example is extends the EmptyGateway class - but it also implements the FilenameFilter class.   Since i'm not doing any work with filtering filenames, I had to remove the "implements FilenameFilter" from the public class declaration in the source file.

After doing this, I compiled successfully.  Yay!  The documentation says to put it into a jar file and then place it in the gateway lib directory.

jar -cf SimpleGateway.jar SimpleGateway.class


Worked fine and I moved it to the appropriate lib folder, and restarted Coldfusion.  Consult the CF documentation for the appropriate folder, it's different if you're using CF standalone or j2ee.

So I then logged into the CF Administrator to add the Gateway Type.

Problem #4 - full java class?  What the heck does that mean?

I had no idea what to put here.  The documentation didn't help either, so I looked at the DirectoryWatcher class in the CF Admin, and it said the class was "examples.watcher.DirectoryWatcherGateway".  I looked at the source code for that class, and saw "package examples.watcher" and figured that this must be where it came from.  So I changed it from "examples.watcher" to "package com.opensourcecf.gateways", then moved my files around a bit, so that my source code path was $CFHOME/gateway/src/com/opensourcecf/gateways/SimpleGateway.java

I then recompiled without error again, copied it to the gateway lib folder, and restarted coldfusion again.

I was able to successfully add the Gateway Type using the full java class "com.opensourcecf.gateways.SimpleGateway"

What fun!

I'll be releasing my SimpleGateway class as open source, as people might find it useful, and when I do (maybe tomorrow), I'll blog about it at www.opensourcecf.com and I'll post a direct link to the blog entry here.

Posted by rickroot at 8:19 PM | Link | 7 comments
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: Compiling Custom Event Gateways For Dummies
I am having trouble creating an event gateway type based on your SimpleGateway.jar file you provided on www.opensourcecf.com

Steps I took to create the Event Type:
1. Download simplegateway.zip
2. Unzip simplegateway.jar to C:\CFusionMX7\gateway\lib (Non-J2EE edition of ColdFusion)
3. Attempted to add Event Gateway Type as:
Name: SimpleGateway
Desc: Simple Gateway
Class: com.opensourcecf.gateways.SimpleGateway
Timeout: 30

Error received:
Error creating gateway type.
Unable to find or load Gateway class com.opensourcecf.gateways.SimpleGateway

Any thoughts?
Thx,
Posted by tova10 on February 17, 2006 at 11:13 AM

Re: Compiling Custom Event Gateways For Dummies
Did you restart coldfusion? It loads the jars into the classpath on startup.
Posted by rickroot on February 17, 2006 at 11:31 AM

Re: Compiling Custom Event Gateways For Dummies
Hi Rick, I just had a similar experience creating a custom gateway. I'll be posting an example in the next few day's if you're interested in having a look.

http://www.jmpj.net/jason/index.cfm?mode=entry&entry=9172BDDE-CF1D-76B8-A792B315CE369DFB
Posted by Jason Sheedy on February 22, 2006 at 1:08 PM

Re: Compiling Custom Event Gateways For Dummies
P.S. I like the anti-spam key image thingy. Are those images generated on the fly or are you using a pre-defined library? How does it work?
Posted by Jason Sheedy on February 22, 2006 at 1:10 PM

Re: Compiling Custom Event Gateways For Dummies
Thanks jason.. it helps keep the automated spammers off my blog, and has worked pretty well so far.

It watermarks some text onto an existing image, randomly adjusting the height above the baseline.... uses java. Creates the img then calls it via a cfm that uses to return it (and theoretically delete it).. the value itself is stored in a session variable on the server.
Posted by rickroot on February 22, 2006 at 1:33 PM

Re: Compiling Custom Event Gateways For Dummies
Have just had to compile a simplified version of the socket gateway on Windows. Your post was immensely useful but there were a couple of Windows tricks required to get it to wokr. I posted my findings on the Coldfusion forums and figured I'd attach them here in case anyone comes along in future in the same sitaution as me.

http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=1&catid=21&threadid=1201832&enterthread=y

Now I just need to figure out how to change some of the control characters that the java serversocket class uses.
Posted by Steve Powell on October 5, 2006 at 10:06 AM

Re: Compiling Custom Event Gateways For Dummies
Make *sure* that the JRE version that you use to compile your java code is the same JRE that the coldfusion server is running, otherwise when you try to add the gateway type it won't be able to find the class you point it to, even though the jar is in the right spot and all the packaging is correct.

To check the JRE that colfusion is running, go to Settings Summary in the CF admin and look on the line that says Java Version.

If you need to change the JRE that CF runs, I would stop the CF Server, rename the CFusion/runtime/jre folder to something else like jreOLD (in case you need it later), and then copy over another jre folder (e.g. C:\j2sdk1.4.2_15\jre, assuming you've installed a JRE there) to replace jreOLD. Restart CF, check in the Settings Summary to make sure it's running the new JRE, and that's it.
Posted by Alan on June 28, 2007 at 11:03 AM

Post a comment (login required)