November 14, 2010

Salesforce WSC added support for Session Timeout handling !

Retrying web service call failures/exceptions is important, because not every exception means its “end of world”, their is always some HOPE, for some exceptions like Session Timeout / Invalid Sessions Id. One can always renew the session (if credentials are available), and make those failing web service calls work again.

Want to know more about such recoveries, its explained in this previous post “Why WS-Client should RETRY Web Service Exceptions ?”.

SFDC WSC API & Session Renewal !

Salesforce WSC(Force.com Web Service Connector) recently in release 20 introduced the same concept of renewing session on timeout or invalidation. Unfortunately, I can’t find this anywhere in any official WSC documentation, I came to know about this new feature, when I was debugging some error and stumbled upon WSC ConnectorConfig class. This class has a new attribute called “sessionRenewer”, SessionRenewer is a Java interface, implementations of which can be used to renew session 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 Sad smile !

I thought of giving this new feature a try and created 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 SFDC WSC Google code project. If any one of you 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 highly performance SFDC Web Service Connector API. In case your project is depending on Apache axis 1.4, you can use Tolerado for Axis

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 even more simplify developers life, I requested WSC team, to integrate Tolerado into the WSC library. I raised this issues http://code.google.com/p/sfdc-wsc/issues/detail?id=25 long back on WSC Issue list. I am still waiting to hear from WSC team, please let me know.

Conclusion

Its good to see this effort from WSC team, to add recovering abilities to the API. But renewing session on session timeout or invalidation is one thing, there are many other failures (explained in this post), that can be retried and recovered in same way. So, I expect more of such retryable/recoverable error handling coming to WSC in near future.

Also, SFDC WSC team : Please add API “release notes” page some where on WWW !