I've been working with Amazon Webservices (AWS) and needed to connect to it from some of our railo servers.
Getting Setup
Here is some documentation for reference and to help others:
1. Get the SDK from Amazon
2. Get the HTTPComponents Client
- You will also need get the HTTPComponents Client from apache as it depends on this. http://hc.apache.org/downloads.cgi I downloaded the 4.1.1 release and this worked a treat
- Extract the 6 jars and drop them in your tomcat/lib folder.
3. Restart Tomcat and write some code
Comparison of techniques
I played with a few different techniques of using the AWS with tomcat and found the following:
1. CFHTTP direct API calls
This was based on a modified version of the
AWS CF Console code. This has the benefit of being simple (none of the jar files were needed) and it took approx 900ms to do a query from Australia to US East AWS. From our US datacenter this took less than 30ms). However, this method had the significant drawback or requiring lots of code to be written to handle each function. Not good.
2. Java AWS SDK from Railo
I then did the steps above and used the AWS SDK from a test page using the following code:
<cfset awscreds = createObject("java","com.amazonaws.auth.BasicAWSCredentials").init(accessKeyId,secretAccessKey)>
<cfset sdb = createObject("java","com.amazonaws.services.simpledb.AmazonSimpleDBClient").init(awscreds)>
<cfset selectReq = createObject("java","com.amazonaws.services.simpledb.model.SelectRequest").init("select * from #domainName# where testid = '10000000'")>
This worked well, but each request took on average of 1500ms from AU to US East AWS. This was disappointing, so I tried caching in the the application scope as below.
3. Java AWS SDK cached in App Scope
<cfif NOT structKeyExists(application,'inited')>
<cfset application.awscreds = createObject("java","com.amazonaws.auth.BasicAWSCredentials").init(accessKeyId,secretAccessKey)>
<cfset application.sdb = createObject("java","com.amazonaws.services.simpledb.AmazonSimpleDBClient").init(application.awscreds)>
<cfset application.inited = true>
</cfif>
<cfset selectReq = createObject("java","com.amazonaws.services.simpledb.model.SelectRequest").init("select * from #domainName# where testid = '10000000'")>
The first request took ~ 1500ms but then subsequent requests were about 300ms, which was 1/3 of the time of the raw CF code. With the added benefit of having full access to all the API's, this is definitely the way we'll be using it going forward.
Cheers, Mark
References
2 Comments
this is EXACTLY what i've been looking for. AWS Java SDK in coldfusion. is there any more sample code for interacting with the java library? i'm unable to get anything to work...list domains, put attributes, nothing.
Hi Brandon,
There are loads of examples in the Java SDK. Look in the samples folder.
For example to list the domains from SimpleDB you would call:
sdb.listDomains();
When you dump the object you'll probably see a method call getDomainNames(). If you call that method you should get a list of the domain names.
Cheers,
Mark