Last active
June 23, 2016 17:24
-
-
Save arbonboy/5310711 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var IntegrationHelper = Class.create(); | |
IntegrationHelper.prototype = { | |
initialize: function() { | |
}, | |
/* | |
* getEccResponse | |
* | |
* This method will take the sysID of an output record on the ECC queue | |
* and wait up to a max number of MS for a corresponding "input" | |
* response to be generated on the queue. | |
* | |
* PARAMS | |
* outputSysId - The sys_id of the "output" record on the queue | |
* waitMS - max number of miliseconds to to keep polling for the response. | |
* | |
* RETURNS | |
* null - no response in within the max wait time; | |
* Otherwise, we return the ECC Queue Payload string from the response record | |
*/ | |
getEccResponse: function(outputSysId,waitMS){ | |
if(!waitMS) { | |
waitMS = 25000; | |
} | |
var start = new GlideDateTime; | |
//Loop waiting for a record that is created in response to your query | |
var resp = new GlideRecord("ecc_queue"); | |
resp.addQuery("response_to", outputSysId); | |
resp.addQuery("queue", "input"); | |
do{ | |
resp.query(); | |
resp.next(); | |
gs.sleep(1000); | |
if ( GlideDateTime.subtract(start, new GlideDateTime()).getNumericValue() > waitMS ) { | |
return null; | |
} | |
} while(!resp.sys_id); | |
//Found a response | |
return this.getEccPayload(resp); | |
}, | |
/* | |
* getEccPayload | |
* | |
* Large payload bodies will create an attachment on the ECC Queue record. | |
* This function will automatically determine if the response is an attachment | |
* or if it is plain text within the payload field. It will return the full | |
* payload string regardless of whether it is contained within the field or in | |
* an attachment. | |
* | |
* PARAMS | |
* ecc - The GlideRecord of the ECC Queue record | |
* | |
* RETURNS | |
* Full payload string | |
* | |
*/ | |
getEccPayload: function(ecc){ | |
if(ecc.payload != "<see_attachment/>"){ | |
return ecc.payload; | |
} | |
var SysAttachment = Packages.com.glide.ui.SysAttachment; | |
var sa = new SysAttachment(); | |
var payload = sa.get(ecc, "payload"); | |
return payload; | |
}, | |
/* | |
* Similar to the SOAPMessage "getReponse()" function. This method | |
* will wait a max time polling for responses from the ECC Queue. | |
* | |
* PARAMS | |
* s - The SOAPMessage class instance that submitted the SOAP Request | |
* waitMS - max number of miliseconds to to keep polling for the response. | |
* | |
* RETURNS | |
* null - no response in within the max wait time; or no successful retries | |
* Otherwise, we return what is in the ECC Queue Payload on the successful record. | |
*/ | |
getResponse: function(s,waitMS){ | |
var start = new GlideDateTime; | |
//GlideDateTime.subtract(start, new GlideDateTime()).getNumericValue() > waitMS | |
//attempt to get the response on the orig request | |
var k = 1; | |
var response = s.getResponse(); | |
while(response == null) { | |
gs.log("waiting ... " + k + " seconds"); | |
response = s.getResponse(1000); | |
if(response){ | |
break; | |
} | |
k++; | |
if ( GlideDateTime.subtract(start, new GlideDateTime()).getNumericValue() > waitMS ) { | |
return null; | |
} | |
} | |
return response; | |
}, | |
/* | |
* This method will wait a max time polling for responses from | |
* retried requests that take place due to a failure. | |
* Only use this method if you have ascertained that there is a retry | |
* policy triggered on original request. | |
* | |
* PARAMS | |
* s - The SOAPMessage class instance that submitted the SOAP Request | |
* waitMS - (optional) max number of miliseconds to to keep polling for the response. | |
* otherwise we just wait for the retry policy to finish before we give up. | |
* | |
* RETURNS | |
* null - no response in within the max wait time; or no successful retries | |
* Otherwise, we return what is in the ECC Queue Payload on the last retry | |
*/ | |
getRetryResponse: function(s,waitMS){ | |
var start = new GlideDateTime; | |
//GlideDateTime.subtract(start, new GlideDateTime()).getNumericValue() > waitMS | |
var succeeded = false; | |
while(!succeeded){ | |
var retry = new GlideRecord('ecc_queue_retry_activity'); | |
retry.initialize(); | |
retry.addQuery("retry_queue_entry", s.soapEnvelope.outputq); | |
retry.orderByDesc("sys_created_on"); | |
retry.query(); | |
retry.next(); | |
if( retry.status == "succeeded" ){ | |
s.soapEnvelope.outputq = retry.output_queue; | |
s.soapEnvelope.response_payload = null; | |
succeeded = true; | |
var response = s.getResponse(); | |
break; | |
} else { | |
if( retry.status == "failed" ){ | |
break; | |
} | |
Packages.java.lang.Thread.sleep(1000); | |
} | |
if(waitMS && GlideDateTime.subtract(start, new GlideDateTime()).getNumericValue() > waitMS){ | |
//taking longer than we had allotted | |
return null; | |
} | |
} | |
if(succeeded){ | |
return response; | |
} | |
return null; | |
}, | |
type: 'IntegrationHelper' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment