Day 10: ColdFusion 10 and Enhanced Java Integration – Part 3

Day 10 of my ColdFusion 10 month, seems that lot more functionality added in ColdFusion 10 and will not able to cover all in within 30 days with current busy schedule. Yesterday cover CFCProxy which you can use in java class to invoke ColdFusion component function but seems limitation is you can’t call for coldfusion instance. This will be overcome with createDynamicProxy function. This is coldfusion function takes two arguments.

  1. Fully qualified coldfusion component path (e.g. cfc.mycomponent) or  coldfusion instance.
  2. Interface name (Yes we need to create interface in java that match with coldfusion component).

Below is sample code (extend code used in previous two days post) to say Hello to employee.


<cfcomponent output="false" displayname="Employee">
<cfproperty name="firstName" default="">
<cfproperty name="lastName" default="">
<cfset variables.instance = structNew()>
<cffunction name="init" access="public" output="false" returntype="CF10.day10.Employee">
<cfargument name="firstName" required="false" type="string" default="">
<cfargument name="lastName" required="false" type="string" default="">
<cfset variables.instance.firstName = arguments.firstName>
<cfset variables.instance.lastName = arguments.lastName>
<cfreturn this>
<cffunction name="getFullName" access="public" output="false" returntype="String">
<cfreturn instance.firstName & " " & instance.lastName>

Simple component to store employee first name and last name. Init function will initialize values and return component and getFullName function return full name of employee. To use employee.cfc in java we need to create interface in java which match with our Employee.cfc definition

public interface EmployeeInterface 

public EmployeeInterface init();
    public String getFullName(); 

And here is to say hello employee.

public class HelloEmployee{
private EmployeeInterface employee;
public HelloEmployee(EmployeeInterface emp){
this.employee = emp;
public String sayHello(){
return "Hello " + this.employee.getFullName() + " !!!";}

Simply created instance of EmployeeInterface and constructor will take receive proxy object from ColdFusion. sayHello will get full name from employee object and say hello to him. 
And now my CFM page which call HelloEmployee java class to say hello.


<!— Create Object of employee —>
<cfset employee = createObject("CF10.day10.Employee").init("Pritesh","Patel")>
<!— Create Dynamic proxy instance  —>
<cfset dEmp = createDynamicProxy(employee,["EmployeeInterface"])>
<cfset obj = createObject("java","HelloEmployee").init(dEmp)> 

First line we create object of Employee.cfc and stored name of employee. Now call createDynamicProxy function with my employee instance variable and need to pass Interface name so java can match with our coldfusion component in array and create java class object which will use our ColdFusion component to sayHello. 
And here is my output:

Hello Pritesh Patel !!!

Hope this help.