In the last three posts, we examined calling a webservice from a visualforce page's controller. We explored calling them as synchronous apex callouts and asynchronous apex callout (Continuation pattern) -
So in our example, if we want to take a value from the user and convert it to Celcius by calling the webservice but dont need the user to wait for the result, we can use a future apex method. The user will be free to proceed with other work as the callout happens in a different thread completely. That thread will receive the results of the callout and process it.
Note that it has no way to communicate back to the main thread. So your user never sees the results unless you use some other means of doing this. For ex: - the result could be written to a Object field value in your future method. Next time the user visits the page layout of that object, he will the field as populated with the result. This makes it different from the continuation pattern which involves a visualforce page to show the results.
Future methods can be used effectively to avoid governor limits related to concurrency from long running callouts. However they have limits too. Following post calls out these limits very well - 4 gotchas with Apex Future Calls
So use Future calls carefully. In my example we are using a visualforce page. We will change it to use a future call. However the result cannot be sent back to the visualforce page, so i will show you the result via System debug logs. Remember based on your business requirement, you may choose to write the result to a field or capture the result in a different way to inform your users.
In my visualforce page in the post - Integrating Salesforce with Soap web service Part 1 I already had a button Convert Future that was calling a controller method - callFutureMethod. We are going to use this button now.
In the controller, lets code to take the input Fahrehiet value and call a future method -
In the above code -
- callFutureMethod is just calling the future call and passing the value of the input parameter to it. It calls the the future method and returns control to the user in the visualforce page.
- getCelciusFuture is annotated with @Future to indicate it executes in a future thread. It is also marked as a callout as it will be invoking a webservice callout. Now when this method is called, it will execute in the future in a separate thread. Here all we do is get the result and log it in the system log. We could have probably written it to a object field if required. However we have no way to pass it back to the visual force page
The UI doesnt change as I explained. But if you look at the logs, there will be two -
- The actual apex call triggered by the button click where you see the actual callout does not occur.
- A FutureHandler log where you will see the actual callout and result being fetched.
So thats a future call. Now you can use it as your business use case determines it.
That covers all the posts I set out to write on synchronous and asynchronous apex callouts for now. I definitely enjoyed it and hope some of you get some value of it as well!
Comments
Post a Comment