Published:
Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.
If you run a ColdFusion server cluster with sticky sessions you may on occasion need to switch between CF instances in order to perform tasks such as calling an application reload. I do this all the time to switch between different versions of CF(7,8 & 9) on my dev machine. Assuming you are using J2EE sessions here is one method of doing this.
Note 1: You need to be using J2EE session's for this technique to work.
Note 2: This technique creates a new session so any sessioned data from the origin instance will no longer be accessible.
Step 1: Get The Server Instance Name
Before we go about creating our switching mechanism we first need a way to determine which server instance we are one. This is simply done by dropping down into java.
<cfset serverName = createObject("java", "jrunx.kernel.JRun").getServerName()>
<cfoutput>
You are currently on server instance: #serverName#
</cfoutput>
Step 2: Get "server.id" Values
For each CF server instance you create there is a unique server.id value created. This value can be found in the "connector.properties" file defined for each instance. Open this file and note the 4 byte server.id value.
Assuming you installed ColdFusion to the default location of "c:\jrun4" this file for each named server instance would be located at:
Step 3: Update Cookie With The JSESSIONID
In order to switch between server instances all we need to do now is update the JSESSIONID value in the web browser cookie with the server.id value for the instance we want to switch to using the cfcookie tag.
<!--- Change to alternative server instance using server.id value --->
<cfcookie name="JSESSIONID" value="#targetServerId#">
Putting It All Together
So now that we have all the required components lets put them together. Create a file "switcher.cfm" and paste in the code (remembering to substitute in your own server.id values).
<!--- Switch if a new jsession id passed in through url --->
<cfif isDefined('url.targetServerId')>
<cfcookie name="JSESSIONID" value="#url.targetServerId#">
</cfif>
<!--- Get the current server instance name --->
<cfset serverName = createObject("java", "jrunx.kernel.JRun").getServerName()>
<cfoutput>
<p>You are currently on server instance: #serverName#</p>
<p>To switch to a different CF instance click the relevant link below.</p>
<a href="/switcher.cfm?targetServerId=da30">Switch to Instance 1 (da30)</a><br/>
<a href="/switcher.cfm?targetServerId=c030">Switch to Instance 2 (c030)</a><br/>
</cfoutput>
Reader Comments