Tuesday, July 29, 2014

Asynchronous webservice response.

I hope you all are doing well.
After a very long time I am going to post this article. I switched to a different technology.
Within last two years I worked on android and few Java related things. Now again I moved to CF.

Recently I faced one issue with my client Process we built one webservice which accept large XML file from client and process it parallel. While processing large data 1st we faced challenges with the memory.
It was a quick fix we used Sax parser instead of DOM.

But recently we faced one issue that when client is sending large files in every 5 minutes our process is not able to process that file within 5 mins. So we are not receiving very next file.

To resolve that I change our webservice from synchronous to Asynchronous mode.
In Asynchronous mode we are sending them response after getting file and processing that file in backend so we are not missing any files.

This solution is really worked for me. If you are having same kind of issue this solution may help you to come up from your situation.
To do that I used ColdFusion event gateway feature.
Event gateway lets CFML code send a message to CFC methods asynchronously. This event gateway lets you initiate processing by a CFC method without waiting for it to complete or return a value.
By default, ColdFusion delivers the message to a CFC method named onIncomingMessage. You can specify any method name, however, in the SendGatewayMessage method's data parameter.
3 Steps to create gateway
·          Create a CFC with an onIncomingMessage method. Put the CFC in an appropriate directory for your application. For example, you can put it in the cf_root\WEB-INF\cfusion\gateway\cfc directory on J2EE configurations, in the cf_root\gateway\cfc directory on server configurations, or in a subdirectory of these directories. ColdFusion is installed with mappings to these cfc gateway directories.
·          The onIncomingMessage method must take a CFEvent structure that contains input information in its Data field, and processes the contents of the Data field as needed.
·          Use the Gateways page in the ColdFusion Administrator to add an instance of the CFML event gateway type. Specify the following:
o    A unique Gateway ID.
o    The path to the CFC that you created in step 1.
o    The startup mode. Select Automatic startup mode to start the event gateway when ColdFusion starts up.
o    Do not specify a configuration file.
·          Start the event gateway instance.

Using Gateway
·          Write CFML code that uses SendGatewayMessage functions to send messages in structures to the event gateway instance ID that you specified in CFAdmin. The SendGatewayMessage function returns true if the gateway successfully queues the message in the ColdFusion Gateway Service; false, otherwise. It does not ensure that the CFC receives or processes the message.
·          Run your CFML application.