+ Start a Discussion
Jamz_2010Jamz_2010 

Callout Timeouts

Hi all,

 

I am having some problems setting the callout timeout in apex. I have a class generated by the WSDL2Apex tool which works fine but I can't seem to be able to change the timeout value. I have tried changing the value of the timeout__x variable that is generated but this doesn't seem to do anything, am I supposed to use this variable during my web service invokation? My code is:

 

 

//Generated by wsdl2apex

public class smsService {

public class sendsms_element {

public String[] sMobileNums;

  public String sMessage;

public String sFrom;

public String sOrganisationId;

private String[] sMobileNums_type_info = new String[]{'sMobileNums','http://www.w3.org/2001/XMLSchema','string','0','-1','true'};

private String[] sMessage_type_info = new String[]{'sMessage','http://www.w3.org/2001/XMLSchema','string','0','1','true'};

private String[] sFrom_type_info = new String[]{'sFrom','http://www.w3.org/2001/XMLSchema','string','0','1','true'};

private String[] sOrganisationId_type_info = new String[]{'sOrganisationId','http://www.w3.org/2001/XMLSchema','string','0','1','true'};

private String[] apex_schema_type_info = new String[]{'http://expwsms','true','true'};

private String[] field_order_type_info = new String[]{'sMobileNums','sMessage','sFrom','sOrganisationId'};

}

 

public class receiveSMSResponse_element {

public String return_x;

private String[] return_x_type_info = new String[]{'return','http://www.w3.org/2001/XMLSchema','string','0','1','true'};

private String[] apex_schema_type_info = new String[]{'http://expwsms','true','true'};

private String[] field_order_type_info = new String[]{'return_x'};

}

 

public class expwsmsHttpSoap11Endpoint {

public String endpoint_x = 'http://hosted.expw.com:8002/axis2/services/expwsms.expwsmsHttpSoap11Endpoint/';

public Map<String,String> inputHttpHeaders_x;

public Map<String,String> outputHttpHeaders_x;

public String clientCert_x;

public String clientCertPasswd_x;

public Integer timeout_x = 120000;

private String[] ns_map_type_info = new String[]{'http://expwsms', 'smsService'};

public String receiveSMS(String JobId,String SMSMessage,String MobileNo,String VMN,String Keyword,String AccountId,String Network,String RecievedDate) {

smsService.receiveSMS_element request_x = new smsService.receiveSMS_element();

smsService.receiveSMSResponse_element response_x;

request_x.JobId = JobId;

request_x.SMSMessage = SMSMessage;

request_x.MobileNo = MobileNo;

request_x.VMN = VMN;

request_x.Keyword = Keyword;

request_x.AccountId = AccountId;

request_x.Network = Network;

request_x.RecievedDate = RecievedDate;

Map<String, smsService.receiveSMSResponse_element> response_map_x = new Map<String, smsService.receiveSMSResponse_element>();

response_map_x.put('response_x', response_x);

WebServiceCallout.invoke(

this,

request_x,

response_map_x,

new String[]{endpoint_x,

'urn:receiveSMS',

'http://expwsms',

'receiveSMS',

'http://expwsms',

'receiveSMSResponse',

'smsService.receiveSMSResponse_element'}

);

response_x = response_map_x.get('response_x');

return response_x.return_x;

}

 

public String sendsms(String[] sMobileNums,String sMessage,String sFrom,String sOrganisationId) {

smsService.sendsms_element request_x = new smsService.sendsms_element();

smsService.sendsmsResponse_element response_x;

request_x.sMobileNums = sMobileNums;

request_x.sMessage = sMessage;

request_x.sFrom = sFrom;

request_x.sOrganisationId = sOrganisationId;

request_x.setTimeout(2000);

Map<String, smsService.sendsmsResponse_element> response_map_x = new Map<String, smsService.sendsmsResponse_element>();

response_map_x.put('response_x', response_x);

WebServiceCallout.invoke(

this,

request_x,

response_map_x,

new String[]{endpoint_x,

'urn:sendsms',

'http://expwsms',

'sendsms',

'http://expwsms',

'sendsmsResponse',

'smsService.sendsmsResponse_element'}

);

response_x = response_map_x.get('response_x');

return response_x.return_x;

}

}

 

public class sendsmsResponse_element {

public String return_x;

private String[] return_x_type_info = new String[]{'return','http://www.w3.org/2001/XMLSchema','string','0','1','true'};

private String[] apex_schema_type_info = new String[]{'http://expwsms','true','true'};

private String[] field_order_type_info = new String[]{'return_x'};

}

 

public class receiveSMS_element {

public String JobId;

public String SMSMessage;

public String MobileNo;

public String VMN;

public String Keyword;

public String AccountId;

public String Network;

public String RecievedDate;

private String[] JobId_type_info = new String[]{'JobId','http://www.w3.org/2001/XMLSchema','string','0','1','true'};

private String[] SMSMessage_type_info = new String[]{'SMSMessage','http://www.w3.org/2001/XMLSchema','string','0','1','true'};

private String[] MobileNo_type_info = new String[]{'MobileNo','http://www.w3.org/2001/XMLSchema','string','0','1','true'};

private String[] VMN_type_info = new String[]{'VMN','http://www.w3.org/2001/XMLSchema','string','0','1','true'};

private String[] Keyword_type_info = new String[]{'Keyword','http://www.w3.org/2001/XMLSchema','string','0','1','true'};

private String[] AccountId_type_info = new String[]{'AccountId','http://www.w3.org/2001/XMLSchema','string','0','1','true'};

private String[] Network_type_info = new String[]{'Network','http://www.w3.org/2001/XMLSchema','string','0','1','true'};

private String[] RecievedDate_type_info = new String[]{'RecievedDate','http://www.w3.org/2001/XMLSchema','string','0','1','true'};

private String[] apex_schema_type_info = new String[]{'http://expwsms','true','true'};

private String[] field_order_type_info = new String[]{'JobId','SMSMessage','MobileNo','VMN','Keyword','AccountId','Network','RecievedDate'};

}

 

Please help as I have checked loads of documentation but can't seem to find out how to do this (I have tried request_x.setTimeout(120000) and request_x.timeout__x = 120000 but these don't seem to work. How can I set the timeout for this request?

 

Thanks

 

James 

Message Edited by Jamz_2010 on 10-05-2009 08:46 AM
Message Edited by Jamz_2010 on 10-05-2009 08:46 AM
Message Edited by Jamz_2010 on 10-05-2009 08:51 AM
Best Answer chosen by Admin (Salesforce Developers) 
ThomasTTThomasTT

You're doing the right thing, except the number.

 

[From  Force.com Apex Code Developer's Guide]

The default timeout is 10 seconds. A custom timeout can be defined for each callout. The minimum is 1 millisecond and
the maximum is 60 seconds. See the following examples for how to set custom timeouts for Web Services or HTTP callouts.

 

120 seconds is not acceptable, unfortunatelly...

ThomasTT

All Answers

ThomasTTThomasTT

You're doing the right thing, except the number.

 

[From  Force.com Apex Code Developer's Guide]

The default timeout is 10 seconds. A custom timeout can be defined for each callout. The minimum is 1 millisecond and
the maximum is 60 seconds. See the following examples for how to set custom timeouts for Web Services or HTTP callouts.

 

120 seconds is not acceptable, unfortunatelly...

ThomasTT

This was selected as the best answer
Jamz_2010Jamz_2010

Thanks for that, I have tried changing the line to say:

 

 

public Integer timeout_x = 60000;

 

 But I am still getting a timeout (its taking about 30 seconds to respond). Do I have to use this variable anywhere after it has been declared?

 

Thanks

 

James 

 

ThomasTTThomasTT

Well, then I don't know what to say... Except

1. What's

request_x.setTimeout(2000);
doing? I don't even think request_x has setTimeout()...

2. The manual says

- The default timeout is 10 seconds. A custom timeout can be defined for each callout. The minimum is 1 millisecond and the maximum is 60 seconds. See the following examples for how to set custom timeouts for Web Services or HTTP callouts.
- The maximum cumulative timeout for callouts by a single Apex transaction is 120 seconds. This time is additive across all callouts invoked by the Apex transaction.

So, check if you are making any other callouts in the same transaction. They might be using up 100 seconds in total.

3. Try timeout_x = 1000, and see the parameter is working or not. If it works, maybe 2 is the cause. If it still take 30 seconds, the parameter is not working.

4. Maybe a bug in Winter '10 (I'm sure it's been working for me with Summer '09).

ThomasTT
Message Edited by ThomasTT on 10-06-2009 11:22 AM
Jamz_2010Jamz_2010
Hi,
 
Thanks for you response, I got that bit of code from the manual but found out that it was for doing requests into Salesforce. It appears to be working now - not quite sure why as the code is the same as yesterday! Anyway, thanks for you help although I still don't understand how it works as the variable timeout__x is not used anywhere.
 
Thanks again
 
James
ThomasTTThomasTT

Well, first of all, good for you.

The callouts is made by SFDC and you're certainlly giving  expwsmsHttpSoap11Endpoint object, which contains timeout_x variable, to WebServiceCallout.invoke() via "this". So when SFDC makes a callouts with the object, I'm sure it uses the timeout_x value.

and, you may notice that timeout_x is public. You don't have to change the generated Apex Class. When you make the callouts with the object, you just put the timeout value from outside like obj.timeout_x = 60000. I'm sure that you don't want to touch any of generated code.

 

ThomasTT

John DayondonJohn Dayondon
Hi,

I have a similar issue. I did tried changing the value of the time out parameter and it did changed the time the callout times out. Tried 2secs, 5secs then 10secs. But when I changed it to a value greater than 10secs, did 20secs and 30secs, the callout still times out at 10secs.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
req.setMethod('GET');
    req.setTimeout(30000);
    
    try
    {
      HTTPResponse response = m_http.send(req);
      
     if(response.getBody() != null)
        return 'Success';
     else
        return 'Failed'; 
            
    } catch(System.CalloutException e) {
      return 'Error ' + e;
    }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------