Steven Erat's Blog Steven Erat Photography
 
 
Viewing By Entry
 
 

TalkingTree  Using the CFC Proxy in a ColdFusion Cluster

 

The CFC Proxy API was introduced as a supported feature in CFMX 7.01. It allows you to call ColdFusion Components (CFCs) from Java classes such as a standalone servlet running in the same JVM. In order for this to work, the Java class must be loaded by the ColdFusion classloader rather than a higher level classloader in the J2EE container.

To load a Java class with the ColdFusion classloader, the class's jar file must be specified in ColdFusion's web.xml under the cf.class.path parameter. To avoid managing multiple copies of a custom jar file between ColdFusion instances clustered on JRun, you can put a single copy of the custom jar file under a central location outside the JRun root directory. Then modify the web.xml for each CF instance to point to that jar file in the cf.class.path entry.

Surprisingly, there is no documentation on using CFCProxy on livedocs.adobe.com, but instead you can find this reference on Ben Forta's website. The reference describes the API and provides a brief example implementation. A few details are left out such as how to compile the custom Java class, so I'll provide a quick walk through of how I set all this up...

A Sample CFC saved as C:\TEMP\CFCProxy\test.cfc

<cfcomponent>
<cffunction name="getData" returntype="struct">
<cfargument name="msg" required="Yes">
<cfscript>
st = StructNew();
st.name = "michael";
st.email = "remin@macromedia.com";
st.date = "#now()#";
st.message = msg;
return st;
</cfscript>
</cffunction>
</cfcomponent>



CFCInvoker Java source file saved as C:\TEMP\CFCProxy\CFCInvoker.java

import coldfusion.cfc.CFCProxy;
import coldfusion.bootstrap.BootstrapClassLoader;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

public class CFCInvoker
{
public coldfusion.runtime.Struct invoke()
{
coldfusion.runtime.Struct map = null;
try {

/*
   declare variable for storing reference to CFCProxy
*/

CFCProxy myCFC;

/*
   instantiate CFC
*/

myCFC = new CFCProxy("C:\\TEMP\\CFCProxy\\test.cfc");

/*
   Build arguments array
*/

Object[] myArgs = {"Java invocation working"};

/*
   invoke method from CFC. It returns a structure.
*/

map = (coldfusion.runtime.Struct) myCFC.invoke("getData", myArgs);

} catch (Throwable ex) {
ex.printStackTrace(); }

return map;
   }
}



Batch File Compiler saved as C:\TEMP\CFCProxy\Compile_and_Jar_CFCInvoker.bat. In order to import all the required classes such as BootstrapClassLoader, HttpServletRequest, and CFCProxy the cfusion.jar, cfmx_bootstrap.jar, and jrun.jar need to be pulled into the classpath. Adjust the paths to match your system (or write a shell script analog) and double click to run.

@echo off
echo This will compile and jar up the CFCInvoker Java source file
echo Press Control+C to abort.
pause

SETLOCAL

set JRUN_HOME=C:\JRun4
set CF_HOME=%JRUN_HOME%\servers\cfusion\cfusion-ear\cfusion-war
set JAVA_HOME="C:\Program Files\Java\jdk1.6.0"

set CLASSPATH=.;%CF_HOME%\WEB-INF\cfusion\lib\cfusion.jar;%CF_HOME%\WEB-INF\lib\cfmx_bootstrap.jar;%JRUN_HOME%\lib\jrun.jar;
set PATH=%JAVA_HOME%\bin\;.;%PATH%

javac -classpath %CLASSPATH% CFCInvoker.java

jar -cvf CFCInvoker.jar CFCInvoker.class

echo Done
ENDLOCAL
pause



Modify cf.class.path parameter for each ColdFusion instance's web.xml file. Add the path for the centrally located jar file:

<context-param id="coldfusion_context_88">
<param-name>cf.class.path</param-name>
<param-value>
./WEB-INF/cfusion/lib/updates,./WEB-INF/cfusion/lib,./WEB-INF/cfusion/gateway/lib,./WEB-INF/flex/jars,./WEB-INF/cfform/jars,C:/TEMP/CFCProxy/CFCInvoker.jar</param-value>
</context-param>



Restart All ColdFusion Instances



Test Calling The Java Class by putting a CFCInvoker_tester.cfm in the webroot.

<cfscript>
CFCInvokerObj = createObject("java","CFCInvoker");
</cfscript>

<cfdump var="#CFCInvokerObj.invoke()#"/>



In this example, the CFCInvoker_tester.cfm just creates an instance of the Java class and calls invoke() on it, but a real life example might be that the Java class is a Servlet and is available via URL pattern to accept requests directly.

Since the custom CFCProxy Java class is centrally located in a single jar file outside the ColdFusion or JRun roots it can be shared across multiple instances without worrying about revision differences across the cluster members.

 


Comments

Curious if you've tried to invoke same from a jsp?


Andrew, I haven't tried using CFCProxy from a JSP so far, only from a Java class loaded in the ColdFusion web application.


 

 

Calendar

 
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Search This Site

 
This is an exact search only

About This Site

 
I live west of Boston and work as a Software Engineer with ColdFusion and Flex, specializing in Linux. Recently I graduated in Professional Digital Photography from CDIA.
More about me

Recent Entries

 
A ColdFusion Trick for Lost D..
Starting ColdFusion9 Solr: Us..
Adobe LiveCycle DataServices ..

Recent Comments

 
Posted By Aaron Longnion:
Thanks Steven, I just ran into this problem, remembered your tweet about it, and found your blog on it. :)

Posted By srinyvas:
Hai, This information is very useful and i like your excellent writing skill. Can i copy this Content to my website top management colleges ...

Posted By Steven Erat:
@Wade - Glad I could help! Thanks for letting me know it worked for you too.

recently played

 
The Candid Frame #70 - Greg Gorman
by Ibarionex R. Perello
on The Candid Frame: A Photography Podcast

now playing, a plug-in for itunes

Categories

 
RSS Adobe (34)
RSS Bicycling (9)
RSS Blogging (39)
RSS Books (13)
RSS Breeze (13)
RSS CFMX Podcasts (10)
RSS ColdFusion (427)
RSS Computer Technology (51)
RSS Events (26)
RSS Flex (20)
RSS Gadgets (10)
RSS HiTech Industry (16)
RSS Java (25)
RSS Learning (57)
RSS Linux (70)
RSS Mac OS X (22)
RSS Macromedia (27)
RSS Meetup (35)
RSS New England (62)
RSS Odds & Ends (25)
RSS Outdoors (32)
RSS Personal (29)
RSS Photography (111)
RSS Photoshop (29)
RSS Podcasts (18)
RSS Rants (19)
RSS Restaurants (8)
RSS Science (34)
RSS Spain (16)
RSS Travel (42)
RSS Twitter (10)
RSS Video (20)
RSS Webcam (3)
RSS Writing (10)

Blogs I Read

 
Terrence Ryan
Ben Forta
Ray Camden
Kinky Solutions
Dan Vega
Gary Gilbert
Simeon Bateman
Red Hat Blogs
O'Reilly Digital Media
O'Reilly Radar
John Nack
The Strobist
Scott Kelby
Matt Kloskowski
Joe McNally
Digital Photography School
Engadget
Science Blog

RSS

 


Add to Google
Add to My Yahoo!

Aggregated By

 


Consumed By Feed-Squirrel.com
Aggregated by ColdFusionBlogger.org

Credits and Stuff

 
BlogCFC - Free ColdFusion Powered Blog Software
CJM Group - ColdFusion Website Hosting


 
 
blog | photos | flickr | referers | webcam | stats | about | contact
 
Copyright © 2010 Steven Erat. All rights reserved.
This is a personal weblog. The opinions expressed here represent my own and not those of my employer