Salesforce WSC added support for Session Timeout handling!
Retrying web service call failures/exceptions is important because not every exception means it’s the “end of the world." There is always some hope for some exceptions, like Session timeouts or Invalid session IDs. One can always renew the session (if credentials are available) and make those failing web service calls work again.
SFDC WSC API & Session Renewal!
Salesforce WSC (Force.com Web Service Connector) recently, in release 20, introduced the same concept of renewing sessions on timeout or invalidation. Unfortunately, I can’t find this anywhere in any official WSC documentation. I learned about this new feature when I was debugging some errors and stumbled upon the WSC ConnectorConfig class. This class has a new attribute called “sessionRenewer”, SessionRenewer is a Java interface, implementations of which can be used to renew sessions in case of session timeout. These SessionRenewer implementations can be optionally set by calling the relevant setters in ConnectorConfig, i.e. setSessionRenewer(SessionRenewer sessionRenewer).
My “FAILED” experiment with WSC SessionRenewer!
I thought of giving this new feature a try and created the following test class. I was not able to make this code work well with SessionRenewer. I think SessionRenewer just handles session timeouts, not a generic INVALID_SESSION_ID fault raised because of many other reasons. So, to know where I was going wrong, I raised the issue on the SFDC WSC Google Code project. If anyone was able to successfully use SessionRenewer, please let me know.
public class WSCSessionTest {
public static void main(String[] args) throws Exception {
ConnectorConfig cfg = new ConnectorConfig();
cfg.setUsername("sfdcusername");
cfg.setPassword("sfdcpass");
// set the SessionRenewer implementation
cfg.setSessionRenewer(new WSCSessionRenewer());
PartnerConnection binding = Connector.newConnection(cfg);
// force logout, to see if session timeout,
// because of "invalid session" comes
binding.logout();
// Try querying now
QueryResult qr = binding.query("Select Id, Name from Contact limit 1");
System.out.println(qr.getRecords()[0].getField("Name"));
}
public static class WSCSessionRenewer implements SessionRenewer {
@Override
public void renewSession(ConnectorConfig config)
throws ConnectionException {
System.err.println("Renewing Session for Config : " + config);
config.setManualLogin(true);
PartnerConnection binding = Connector.newConnection(config);
LoginResult lr = binding.login(config.getUsername(), config.getPassword());
// I am updating the config, back as I don't have reference to the original
// PartnerConnection object.
config.setSessionId(lr.getSessionId());
// If using Partner or Enterprise WSDL
config.setServiceEndpoint(lr.getServerUrl());
}
}
}
Tolerado (WSC or Apache Axis) users already have all this!
For those who don’t know what is Tolerado,
“Project “Tolerado” is a Java-based WS client framework for better and fault-tolerant use of Web Service APIs given by Salesforce. Tolerado-WSC works on top of the highly performant SFDC Web Service Connector API. In case your project depends on Apache axis 1.4, you can use Tolerado for Axis”
The Tolerado framework already gives all this session renewal and many other forms of error recovery transparently. So developers who ported their Java WS projects to Tolerado, don’t need to worry about retryable/recoverable web service error handling. They can just focus on grooming the integration and business logic with Tolerado.
To simplify developers' lives even more, I requested the WSC team to integrate Tolerado into the WSC library. I raised this issue http://code.google.com/p/sfdc-wsc/issues/detail?id=25 long back on the WSC Issue list. I am still waiting to hear from the WSC team; please let me know.
Conclusion
It's good to see this effort from WSC team, to add recovering abilities to the API. But renewing a session on session timeout or invalidation is one thing; many other failures can be retried and recovered in the same way. So, I expect more of such returnable/recoverable error handling coming to WSC in the near future.
Also, SFDC WSC team: Please add an API “release notes” page somewhere on the WWW!
References:
Let's Talk
Drop a note below to move forward with the conversation 👇🏻