API Java SDK: Issue with code and StreamPosition

Anonymous
Not applicable

API Java SDK: Issue with code and StreamPosition

I have the following code using the JAVA JDK but the issue is that I iterate always on the first page I cannot move to the second page of the results. My code is the following one:

    URL marketoSoapEndPoint = new URL(context.parameter_marketo_endpoint + "?WSDL");
    String marketoUserId = context.parameter_marketo_client_id;
    String marketoSecretKey = context.parameter_marketo_encryption_key;

    QName serviceName = new QName("http://www.marketo.com/mktows/", "MktMktowsApiService");
    MktMktowsApiService service = new MktMktowsApiService(marketoSoapEndPoint, serviceName);
    MktowsPort port = service.getMktowsApiSoapPort();

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    String text = df.format(new Date());
    String requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);         
    String encryptString = requestTimestamp + marketoUserId ;

    SecretKeySpec secretKey = new SecretKeySpec(marketoSecretKey.getBytes(), "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(secretKey);

    byte[] rawHmac = mac.doFinal(encryptString.getBytes());
    char[] hexChars = Hex.encodeHex(rawHmac);

    String signature = new String(hexChars);

    // Set Authentication Header
    AuthenticationHeader header = new AuthenticationHeader();
    header.setMktowsUserId(marketoUserId);
    header.setRequestTimestamp(requestTimestamp);
    header.setRequestSignature(signature);

    //System.out.println(signature);
    //System.out.println(requestTimestamp);

    // Create Request
    ParamsGetMultipleLeads request = new ParamsGetMultipleLeads();
    ObjectFactory objectFactory = new ObjectFactory();
    SuccessGetMultipleLeads resultLeads;
    StringWriter sw = new StringWriter();
    int leadsRemainingCount = 1;
    String leadsStreamPosition = "";

    while(leadsRemainingCount != 0) {
        // Put Stream position if exists
        if(globalMap.get("StreamPosition") != null) {
            //System.out.println("Stream Position GLOBALMAP : "+(String)globalMap.get("StreamPosition"));
            request.setStreamPosition(objectFactory.createStreamPositionOffset((String)globalMap.get("StreamPosition")));
            //System.out.println("Le steam position c'est quoi : "+request.getStreamPosition().getValue());
        }

        // We use a StaticListSelector to get leads from a specific list
        StaticListSelector staticListSelector = new StaticListSelector();
        JAXBElement<String> listName = objectFactory.createStaticListSelectorStaticListName(context.parameter_marketo_list);
        staticListSelector.setStaticListName(listName);
        request.setLeadSelector(staticListSelector);

        JAXBElement<Integer> batchSize = new ObjectFactory().createParamsGetMultipleLeadsBatchSize(50);
        request.setBatchSize(batchSize);

        ArrayOfString attributes = new ArrayOfString();

        attributes.getStringItems().add("AnnualRevenue");
        attributes.getStringItems().add("AnonymousIP");
        attributes.getStringItems().add("BillingCity");
        attributes.getStringItems().add("BillingCountry");
        attributes.getStringItems().add("BillingPostalCode");
        attributes.getStringItems().add("BillingState");
        attributes.getStringItems().add("BillingStreet");
        attributes.getStringItems().add("City");
        attributes.getStringItems().add("Company");
        attributes.getStringItems().add("Country");
        attributes.getStringItems().add("DateofBirth");
        attributes.getStringItems().add("Department");
        attributes.getStringItems().add("DoNotCall");
        attributes.getStringItems().add("DoNotCallReason");
        attributes.getStringItems().add("Email");
        attributes.getStringItems().add("Fax");
        attributes.getStringItems().add("FirstName");
        attributes.getStringItems().add("Industry");
        attributes.getStringItems().add("InferredCompany");
        attributes.getStringItems().add("InferredCountry");
        attributes.getStringItems().add("LastName");
        attributes.getStringItems().add("LeadRole");
        attributes.getStringItems().add("LeadScore");
        attributes.getStringItems().add("LeadSource");
        attributes.getStringItems().add("LeadStatus");
        attributes.getStringItems().add("MainPhone");
        attributes.getStringItems().add("Marketo Jigsaw Contact Id");
        attributes.getStringItems().add("Marketo Jigsaw Contact Status");
        attributes.getStringItems().add("MarketoSocialFacebookDisplayName");
        attributes.getStringItems().add("MarketoSocialFacebookId");
        attributes.getStringItems().add("MarketoSocialFacebookPhotoURL");
        attributes.getStringItems().add("MarketoSocialFacebookProfileURL");
        attributes.getStringItems().add("MarketoSocialFacebookReach");
        attributes.getStringItems().add("MarketoSocialFacebookReferredEnrollments");
        attributes.getStringItems().add("MarketoSocialFacebookReferredVisits");
        attributes.getStringItems().add("MarketoSocialGender");
        attributes.getStringItems().add("MarketoSocialLastReferredEnrollment");
        attributes.getStringItems().add("MarketoSocialLastReferredVisit");
        attributes.getStringItems().add("MarketoSocialLinkedInDisplayName");
        attributes.getStringItems().add("MarketoSocialLinkedInId");
        attributes.getStringItems().add("MarketoSocialLinkedInPhotoURL");
        attributes.getStringItems().add("MarketoSocialLinkedInProfileURL");
        attributes.getStringItems().add("MarketoSocialLinkedInReach");
        attributes.getStringItems().add("MarketoSocialLinkedInReferredEnrollments");
        attributes.getStringItems().add("MarketoSocialLinkedInReferredVisits");
        attributes.getStringItems().add("MarketoSocialTotalReferredEnrollments");
        attributes.getStringItems().add("MarketoSocialTotalReferredVisits");
        attributes.getStringItems().add("MarketoSocialTwitterDisplayName");
        attributes.getStringItems().add("MarketoSocialTwitterId");
        attributes.getStringItems().add("MarketoSocialTwitterPhotoURL");
        attributes.getStringItems().add("MarketoSocialTwitterProfileURL");
        attributes.getStringItems().add("MarketoSocialTwitterReach");
        attributes.getStringItems().add("MarketoSocialTwitterReferredEnrollments");
        attributes.getStringItems().add("MarketoSocialTwitterReferredVisits");
        attributes.getStringItems().add("MiddleName");
        attributes.getStringItems().add("MobilePhone");
        attributes.getStringItems().add("NumberOfEmployees");
        attributes.getStringItems().add("Phone");
        attributes.getStringItems().add("PostalCode");
        attributes.getStringItems().add("Rating");
        attributes.getStringItems().add("Salutation");
        attributes.getStringItems().add("SICCode");
        attributes.getStringItems().add("Site");
        attributes.getStringItems().add("State");
        attributes.getStringItems().add("Street");
        attributes.getStringItems().add("Title");
        attributes.getStringItems().add("Unsubscribed");
        attributes.getStringItems().add("UnsubscribedReason");
        attributes.getStringItems().add("Website");

        request.setIncludeAttributes(attributes);

        resultLeads = port.getMultipleLeads(request, header);

        JAXBContext context = JAXBContext.newInstance(SuccessGetLead.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(resultLeads, sw);

        System.out.println("Remaining Count : "+resultLeads.getResult().getRemainingCount());
        System.out.println("Stream Position returned : "+resultLeads.getResult().getNewStreamPosition());

        //globalMap.put("LeadsRemainingCount : ", resultLeads.getResult().getRemainingCount());
        //globalMap.put("StreamPosition retournés : ", resultLeads.getResult().getNewStreamPosition());

        leadsRemainingCount = resultLeads.getResult().getRemainingCount();
        leadsStreamPosition = resultLeads.getResult().getNewStreamPosition();
    }

The results is an infinite loop that write in the console the following thing:

Remaining Count : 5181
Stream Position returned : id:2767671:ln:CyrilTalendCloudProgram.TalendCloudConnectorList:os:50:rc:5181
Remaining Count : 5181
Stream Position returned : id:2767671:ln:CyrilTalendCloudProgram.TalendCloudConnectorList:os:50:rc:5181
Remaining Count : 5181
Stream Position returned : id:2767671:ln:CyrilTalendCloudProgram.TalendCloudConnectorList:os:50:rc:5181
...

Thank you for your help.
Tags (1)
1 REPLY 1
Anonymous
Not applicable

Re: API Java SDK: Issue with code and StreamPosition

Issue fixed

I did not use the objectfactory to create the streamposition parameter

Correct code is :
paramsRequestLeads.setStreamPosition(objectFactory.createParamsGetMultipleLeadsStreamPosition("mystreamposition"));