-2

Hi all and thanks for reading

Got this code:

public class SendingZabbixServices{  
    
    @future(callout = true)
    public static void zabbixDoPostFuture(String jsonString, String endpoint){
        
        HttpRequest req = new HttpRequest();
        String method = 'POST';
        JSON2Apex payload = JSON2Apex.parse(jsonString);
        req.setBody(JSON.serialize(payload));
        req.setEndpoint(endpoint);
        req.setMethod(method);        
        req.setHeader('Content-Type', 'application/json'); 
        Http http = new Http();
        HTTPResponse res = http.send(req);
        String response = res.getBody();
        //SendingZabbixServices.getZabixxHost(response);
    }
    
    public static String zabbixDoPost(String jsonString, String endpoint){
        
        HttpRequest req = new HttpRequest();
        String method = 'POST';
        JSON2Apex payload = JSON2Apex.parse(jsonString);
        req.setBody(JSON.serialize(payload));
        req.setEndpoint(endpoint);
        req.setMethod(method);        
        req.setHeader('Content-Type', 'application/json'); 
        Http http = new Http();
        HTTPResponse res = http.send(req);
        String response = res.getBody();
        return response;
        //SendingZabbixServices.getZabixxHost(response);
    }
    
    
    public static String getZabbixAuth(){
        
        String payload = '{'+
            '   \"jsonrpc\": \"2.0\",'+
            '   \"method\": \"user.login\",'+
            '   \"params\": {'+
            '       \"user\": \"xxxxxxx\",'+
            '       \"password\": \"xxxxxxxxx\"'+
            '   },'+
            '   \"id\": 1,'+
            '   \"auth\": null'+
            '}';
        
        String endpoint = 'xxxxxxxxxxxx';
        
        String result = SendingZabbixServices.zabbixDoPost(payload, endpoint);
        Map<String, Object> hostMap = (Map<String, Object>) JSON.deserializeUntyped(result);
        String authNumber = (String) hostMap.get('result');
        system.debug(' authNumber' + authNumber  );
        return authNumber;
    }
    
    public static void getZabixxHost(){
        
        String authNumber = getZabbixAuth();
        String endpoint = 'xxxxxxxxxxxx';
        String client = '4:CLIENTES.Balearia';
        
        String payload = JSON.serialize(
            new Map<String, Object> { 
                'jsonrpc' => '2.0',
                    'method' => 'hostgroup.get',
                    'params' => 
                    new Map<String, Object> {
                        'output' => 'extend',
                            'filter' => new Map<String, String[]>{'name' => new String[] { client } }
                    },
                        'auth' => authNumber,
                        'id' => 1
                        }
        );
        
        SendingZabbixServices.zabbixDoPostFuture(payload, endpoint);
        getZabixxHostGroups(authNumber);
    }
    
    
    public static void getZabixxHostGroups(String authNumber){
        String endpoint = 'xxxxxxxxxxxxx';
        
        String payload = JSON.serialize(
            new Map<String, Object> { 
                'jsonrpc' => '2.0',
                    'method' => 'host.get',
                    'params' => 
                    new Map<String, Object> {
                        'output' => new String[] { 'host' },
                            'groupids' => new Integer[] { 1235 }
                    },
                        'auth' => authNumber,
                        'id' => 1
                        }
        );
        
        SendingZabbixServices.zabbixDoPostFuture(payload, endpoint);  
        getZabixxTrafficItems(authNumber);
    }
    
    
    public static void getZabixxTrafficItems(String authNumber){
        
        String endpoint = 'xxxxxxxxxxxxxxxxxx';
        String traffic = 'TRAFFIC_IN';
        
        String payload = JSON.serialize(
            new Map<String, Object> { 
                'jsonrpc' => '2.0',
                'method' => 'application.get',
                    'params' => 
                    new Map<String, Object> {
                        'search' => new Map<String, String> {'name' => traffic },
                        'output' => 'extend',
                        'hostids' => '15914'
                    },
                        'auth' => authNumber,
                        'id' => 1
                        }
        );

        SendingZabbixServices.zabbixDoPostFuture(payload, endpoint);  
    }
    
}

that is throwing this Read time out error when running from an annonymous console. I've tried making the calloout method asyncronymous so I copied the original method (zabbixDoPost) and made it @future (zabbixDoPostFuture) just for testing purposes but it's till not working because of this same error.

If you follow the logic I need to chain the method calls with one another because I need to do many callouts but with the information that I've got from the callout before that one, that's why I'm chainning all the methods.

Is there any way to achieve this ?

PS: It was working fine until I added the last method

public static void getZabixxTrafficItems(String authNumber){

        String endpoint = 'xxxxxxxxxxxxxxxxxx';
        String traffic = 'TRAFFIC_IN';

        String payload = JSON.serialize(
            new Map<String, Object> { 
                'jsonrpc' => '2.0',
                'method' => 'application.get',
                    'params' => 
                    new Map<String, Object> {
                        'search' => new Map<String, String> {'name' => traffic },
                        'output' => 'extend',
                        'hostids' => '15914'
                    },
                        'auth' => authNumber,
                        'id' => 1
                        }
        );

        SendingZabbixServices.zabbixDoPostFuture(payload, endpoint);  
    }

Actually, if I cut this last method the code is working as expected, but if I add it it throws the error .... big problem because I still need to chain like 5 more method after this last one

7
  • If you look at the documentation for HttpRequest you may notice the setTimeout method. Have you tried that yet?
    – Adrian Larson
    Dec 1 '20 at 17:33
  • yes I did ... actually I did set the timeout to 120000 and didn't worked Dec 1 '20 at 17:35
  • I would love to know why I've got two downvotes in my question .... is it a bad question ? Dec 1 '20 at 17:35
  • Primarily because your code samples do not call the above method and therefore it appears at face value to just be lack of research.
    – Adrian Larson
    Dec 1 '20 at 17:39
  • 1
    Yeah that's clear now. It just wasn't based on the code samples. The good news is you're doing nothing wrong.
    – Adrian Larson
    Dec 1 '20 at 19:55
1

If you're getting a Read Time Out, it means the service is not returning data in a timely manner. There's nothing you can do on this end specifically; you need to contact the provider of this API. You may need to increase the selectivity of your search, optimize the search on the far end (e.g. add an index), or something else. You can't extend the timeout beyond two minutes, so it needs to respond within that time.

1
  • Thanks a lot for the answer Sir, you're always saving me ! Much love Dec 1 '20 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.