7 Replies Latest reply: Jun 22, 2008 11:45 PM by 796215 RSS

    Threads to synchronize AJAZ calls

    807589
      I am using AJAX .
      My html layout is like -
      1. Field Processing Org
      2. Department
      3. Users
      4. Department Email Id
      I am using AJAX calls to get departments as per the selected PO (Processing Org)
      Users and Email Id are both dependent on Department value.
      On my jsp , at Onchange of department I call two functions as
      onChange="javascript:retrieveUsers(),retrieveEmail();"
      ----------------------------------------------
      function retrieveUsers() {
      flag = "retrieveUsers";
      reqToGetUsers = newXMLHttpRequest();
      var department = document.getElementById("department").value;
      var po = document.getElementById("assigneePO").value;
      if ((department == null) || (department == "")) return;
      if ((po == null) || (po == "")) return;
      var urlToGetUsers ="/MainServlet/WIM/Investigations/RETRIEVE_USERS?department=" + escape(department)+ "&po=" + escape(po) + "&flag=" + escape(flag);
      var encodedUrl = encode(urlToGetUsers);
      reqToGetUsers.open("GET", urlToGetUsers, true);
      reqToGetUsers.onreadystatechange = updatePage;
      reqToGetUsers.send(null);
      }
      -----------------------------------------------
      function updatePage() {
      if (reqToGetUsers.readyState == 4) {
      var response = reqToGetUsers.responseText;
      if(response.indexOf('RESPONSE_STATUS_SUCCESS')!=-1) {
      document.getElementById("divUsers").innerHTML = response;
      } else {
      document.write(req.responseText);
      displayexception();
      }
      }
      -------------------------------------
      I have similar retrieve and update functions for retrieve email also.

      In the handler I have two methods for handling both these requests.But AJAX is asynchronous sometimes the users list that I retrieve from one function is put in the email id text box as response.

      1 .I have tried putting delay at client side using javascript but it fails intermittently.

      2. Also, I am setting a session attribute in function 1 - opscoSession.setAttribute("userListRetrieved","true");
      and in function 2 -
      while(opscoSession.getAttribute("userListRetrieved")!=null && opscoSession.getAttribute("userListRetrieved").equals("true")){
      ......................
      ............................
      opscoSession.setAttribute("userListRetrieved","false");

      }
      But this also gives unpredicatable results.

      3. I am trying to use threads now . but I am not very familiar with it.
      in function 1 , now I have put
      opscoSession.setAttribute("userListRetrieved","true");
      notify();
      and in function 2 , now
      while(opscoSession.getAttribute("userListRetrieved")!=null && opscoSession.getAttribute("userListRetrieved").equals("true")){

      try {
      //wait for retrieveUsers to be executed first
      wait();
      } catch (InterruptedException e) {
      cat.error("doRetrieveEmailId : InterruptedException", e);
      }
      ..................................
      ..................................
      opscoSession.setAttribute("userListRetrieved","false");
      }

      I want to know if this will suffice to get accurate results or not.
        • 1. Re: Threads to synchronize AJAZ calls
          796215
          hi,

          the following code change may helps you...

          //reqToGetUsers.open("GET", urlToGetUsers, true);
          reqToGetUsers.open("GET", urlToGetUsers, false);
          whenever you pass "true" here, AJAX will send an asynchronous call, else AJAX will send synchronous call

          hope this will help you..


          dhaval
          • 2. Re: Threads to synchronize AJAZ calls
            807589
            yes , I thought about doing that.But I am not sure if the purpose of AJAX might be served then.
            Will the page section be refreshed only the first time as in AJAX and then for subsequent calls caching be performed ?
            • 3. Re: Threads to synchronize AJAZ calls
              796215
              yes...It wont refresh the page again. and works as AJAX works..

              Edited by: Dhaval.Yoganandi on 21 Jun, 2008 6:56 PM
              • 4. Re: Threads to synchronize AJAZ calls
                807589
                Making synchronous calls does help.
                But I am getting an issue now that when I scroll through the department very qucikly then rather then refreshing the section , a new page loads up...showing only the department drop down list
                • 5. Re: Threads to synchronize AJAZ calls
                  807589
                  I can see that there is an error in updatePageWithEmailId() when I scroll throug the department list very quickly
                  function updatePageWithEmailId() {

                  if (reqToGetEmailId.readyState == 4) {
                  var response = reqToGetEmailId.responseText;
                  if(response.indexOf('RESPONSE_STATUS_SUCCESS')!=-1) {
                  } else {
                  document.write(req.responseText);
                  alert("email id exception here !!") *//This alert pops up at the times when I scroll through the department* displayexception();
                  }
                  }

                  }

                  Email Id is a new field I have introduced , when I had only users dependent on department , nothing like above happened.I could scroll through the department list very quickly then.
                  • 6. Re: Threads to synchronize AJAZ calls
                    807589
                    I chaged my function as -
                    function updatePageWithEmailId() {

                    if (reqToGetEmailId.readyState == 4) {
                    var response = reqToGetEmailId.responseText;
                    if(response.indexOf('RESPONSE_STATUS_SUCCESS')!=-1) {

                    document.getElementById("departmentEmailField").innerHTML = response;
                    // document.getElementById('deptDetailsLoadMessage').style.display='none';
                    if ((document.getElementById("departmentEmailField")!=undefined && document.getElementById("departmentEmailField").value == 'Not Available') || ((document.userForm.departmentEmailField !=undefined) && (document.userForm.departmentEmailField.value == 'Not Available'))) {
                    document.getElementById('userSelectMandate').style.display = 'block';
                    }

                    } else {
                    //document.write(req.responseText);
                    retrieveEmail; //alert("email id exception here !!")
                    //displayexception();
                    }
                    }

                    }
                    It seems to work fine now ...touchwood !!
                    • 7. Re: Threads to synchronize AJAZ calls
                      796215
                      is it working now... or are you getting any error ?