samedi 4 avril 2015

DeferredResult with ajax request with two request

I´m trying to use DeferredResult with an ajax request, but when I return the result my ajax request keeps waiting for response.


Here the client that make the long polling request



(function poll() {
var SUCCESS = 1;
var tenantName = $("#tenantName").val();
$.ajax({
method: "GET",
url: "url.do?subStep=" + subStep + "&tenantId=" + tenantName,
success: function (response) {
alert(response)
if (response.status === SUCCESS) {
bootbox.dialog({
size:"small",
message: "<div class='col-xs-offset-4'>" +
"<i class='fa fa-user'> Page has been modify by " + response.payload + " " +
"<a href='javascript:void(0)' onclick='reload()'>reload</a>" +
"</i>" +
"</div>"
});
}
}, dataType: "json", complete: poll, timeout: 60000
});
})();


Here my controller where I make a long polling request by Ajax every 30 seconds.



@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public DeferredResult<JsonEntity> getSubStepStatus(@RequestParam("subStep") String subStep,
@RequestParam("tenantId") String tenantId) {
if (tenantsInStep.get(subStep) == null) {
initializeTenantsInStep(subStep);
}
if(!tenantsInStep.get(subStep).contains(tenantId)){
tenantsInStep.get(subStep).add(tenantId);
}
DeferredResult<JsonEntity> deferredResult = new DeferredResult<>();
tenantsDeferredInStep.put(tenantId, deferredResult);
return deferredResult;
}


And then when my application detect a POST/PUT ajax request invoke a request to this method of my controller that release the DeferredResults.



@RequestMapping(value = "/setModification", method = RequestMethod.GET)
@ResponseBody
public final Integer setModification(@RequestParam("subStep") String subStep,
@RequestParam("tenantId") String tenantId) {
for (String tenantInStepId : tenantsInStep.get(subStep)) {
if (!tenantId.equals(tenantInStepId)) {
JsonEntity<String> jsonEntity = new JsonEntity();
jsonEntity.setPayload(tenantId);
jsonEntity.setStatus(FeedbackActions.SUCCESS.getCode());
DeferredResult<JsonEntity> jsonEntityDeferredResult = tenantsDeferredInStep.get(tenantId);
tenantsInStep.remove(tenantInStepId);
jsonEntityDeferredResult.setResult(jsonEntity);
}
}
return FeedbackActions.SUCCESS.getCode();
}


This example has been done by two user with two browser to achieve this, but the first client that create the deferredResult dont receive the result when the second client invoke the deferredResult.setResult(bla)


I´m doing something wrong?


Regards.


Aucun commentaire:

Enregistrer un commentaire