A Practical Use for ColdFusion 8’s Object Serialization
ColdFusion 8 added the ability for objects to be serialized. Although you can not serialize/un-serialize objects with CFML, you can use underlying Java functions to do so as described by Pete Freitag’s post Serializing CFC’s in ColdFusion 8. In this post a commenter asked whether there is a use-case where this ability would be useful. While contemplating a problem at work, I think I may have found one.
The problem involves building an asynchronous process queue which sits between a “real-time” web service and the system’s database. The web service will be used to import large amounts of data and the queue is required due to the length of time required to process and then store the data in the database. The idea is to store the data, which will be in the form of CFML beans, in an array in application scope and then have a CFTHREAD process the beans into the database.
Where does serialization come in? Well suppose something happens to the server while there are items in the queue. A restart will lose all the data in the queue. Although this is an edge case, the data is critical to the business and cannot be lost. So, when an item is added to the queue it is serialized and stored to the file system. When an item is removed, the serialized file is removed. On start up, any serialized files on the file system are read and the queue is repopulated without data loss.
This is purely theoretical at this point, but I think the idea has enough merit to give it a try.
