I have an onclick javascript function (invoked from DOJO components through .jsp file) within which two UI components are loaded at the same time in js file (through an AJAX call to the Spring Controller (java file) and back to the JSP/Javascript file.)
The problem is that the second component loads before the first component and is not displaying the prescribed data . I would like to know how to set a delay before calling the code in javascript to delay the second component in milliseconds.
Note :
I used Thread.sleep(msecs) in my controller which seems to work fine and resolves my issue. But I dont want to use that knowing the risk it poses. So request an alternative instead of this.
Also I used setTimeOut() function but setTimeOut() requires a function as an argument. I need to just delay the code within the function for a few milliseconds the first time only. From second time onwards I dont want to delay the code being called.
Request ur valuable inputs.
JavaScript doesn't allow blocking threads, so the only option you can use is setTimeOut() function.
Or you can implement mechanism when the first component notifies the second when it loads and at this moment the second component reloads itself.
Maybe your Problem can be solved by using http://dojotoolkit.org/reference-guide/1.8/dojo/domReady.html?highlight=domready to make sure tho dom is entirely loaded
or by using dojo/deffered
http://dojotoolkit.org/reference-guide/1.8/dojo/Deferred.html?highlight=domready
regards
Delaying the execution of code is not a good idea. In development, the calls might be executed and returned in the order you want. But in a production environment with a system under load, the timing of the server calls being executed may not be consistent.
I am assuming that you are using dojo/xhr to make the ajax calls and your solution is to use a DeferredList.
http://dojotoolkit.org/reference-guide/1.9/dojo/DeferredList.html
var d1 = xhr(...);
d2 = xhr(...);
var dl = new DeferredList([d1, d2]);
dl.then(function(result){
// Execute the code that requires both calls to be completed.
});
Related
I am trying to build something, which requires multithreading functionality. The desired work was not done using javascript i.e. Web-Workers.
So I changed focus from javascript to JSP. Now I want to call one method which will execute series of some queries, and at the same time I want to show the affected table rows on other hand. And when first process done with it's execution, I want to stop other process also. My work is done but statically. Now I want to share some resource between this two threads. So that when first thread done with it's execution, I will set some value to that resource(variable,flag), and check that resource in another thread. Is it possible to access variable of one thread in another while it running.
Thanks
JavaScript in a browser is per design not capable of multithreading. You can simulate it a little bit when using setTimeout or setInterval methods.
But, as with the introduction of HTML5, there are now so called WebWorkers available. They run completely separate, spawn a real OS thread, do not have access the DOM but can interact with your UI application e.g. via events.
I'm doing a program in Struts2 that is parsing some xml from some list of urls and store the list of beans in HttpSession. This is a long running process, because it has to download xml from url, parse it, and create bean. I want to design this task such a manner so that when it will get request from client, it will start parsing and store the result in session. When it finishes parsing some xml it notify the page or Struts from java so that it able to show some results before all parsing the rest of url being completed. I'm already using this parsing in different thread. But it has risk, because web manages different thread pool for each different client request and it has the possibility of memory leak. I already have a look in execAndWait
, but it is not possible to display some result bean during parsing process.
Any one can tell better way or better solution.
I solved my problem. First of all create an interceptor project and extends ExecuteAndWaitInterceptor.java instead of Interceptor interface and override doIntercept method. Just we have to use getNewBackgroundProcess() in doIntercept method. This method mainly create a background thread. If you control this interceptor, then we don't need to wait for uncertain delay. If we control this mechanism then we can call intermediate page when one bean is ready to display before long process being finished. All control will be in my hand, when it should be called. And we don't need to use delay and interval delay, because any bean might not be ready to display after this delay.
I have an interesting testing issue.
My requirement is to do performance test on a web site.
Issue here to get the REAL time taking to load the web page (i am testing). The word REAL time i refer here as the total time taken to finished all the (initial) AJAX calls (with some response). I thought of using Page Load Event.
But this approach does not give the REAL time.
Any suggestions how to do it any reference to the information is also great!
My environment: Java and WedDriver.
Tag your request objects with a timestamp and subtract this from the systime when the corresponding response is received.
Depending on your methodology you could have generic Request/Response object with a <T> payload and a demuxer on the server side. These generic objects could then contain code to automagically register the time when sent and received and provide the time spent to the client. Aggregating time spent for an entire page to load is a bit trickier with this approach, but not near impossible I should think - the RPC proxy might keep track of that if requests are also tagged with the originating page.
if you were to provide some concrete example of your setup/code I might be able to be more precise.
Cheers,
As everyone said, WebDriver is not meant for Performance. However, see the following method that I use sometimes.
Following code will work if you can capture the performance against some particular webelement, can be a div or a span block.
1 Use any stopwatch. I am using apache stopwatch.
import org.apache.commons.lang3.time.StopWatch;
2 Initialize the variable
StopWatch timer = new StopWatch();
3 Start the timer and in next line navigate to your page. I prefer driver.navigate().to() than driver.get() as I feel it reduces other processing time to invoke a fresh URL.
timer.start();
driver.navigate().to(URL);
4 Wait for a particular element that can specify page loading finished. Once it appear, in next line stop the timer.
new WebDriverWait(driver, 10).until(ExpectedConditions. presenceOfElementLocated(By.className("demoClass")));
timer.stop();
System.out.println("timer.getTime() + " milliseconds");
Additional Note: If you want to know the loading time of Network calls of any particular webapge load, use webkit like phantomJS or thing like browsermobproxy. It will return network calls in HAR/JSON file. Parse those files and you may get your time.
Hope this helps.
I am confused about the applicability of multi threading in general...
I am creating an application which executes some code which has been saved in xml format. The work is to use apache http client and retrieve some data from websites...More than 1 website can be visited by one block of code in xml...
Now I want that if 2 users have created their own respective codes and saved them in XML, then each user's 'job' (ie block of code in xml format) runs in a separate thread.
I have with me code to execute one user's code...Now I want that multiple persons' code can be run in parallel. But I have some doubts--
(1) The Apache HTTP client provides a way of multithreaded communication, currently I am simply using the default HTTP client- this same client can be made to visit multiple websites, one after the other- as per code block in xml. Am I correct in thinking that I do not need to change my code so that it uses the recommended multithreaded communication?
(2) I am thinking of creating a servlet that when invoked, executes one block of xml code. So to execute 2 blocks of code as given by 2 different users, I will have to invoke this servlet twice. I am going to deploy this application using Amazon Elastic Beanstalk, so what I am confused about is, do I need to use multi threading at all in my program? Can I not simply invoke the existing code (which is used to execute one block of code at a time) from the servlet? And I do want to keep processing of the different blocks of XML code separate from each other, so I dont think I should use multi threading here.. Am I correct in my assumption?
Running it one after the other as per your 1st option will not be considered 'concurrent' .
Coming to the servlet method , the way you describe it will work concurrently , but you also need to think about how many users concurrently ? Since for each user , there would be a separate request , there would be some network latency involved for multiple calls. You need to think about all these factors before going ahead with this option
Since you have the code for one user's job , you can define a thread class which has userid as an attribute. In the run() method call the code for a particular user's job.
Now create two threads and set the appropriate userid for each thread and spawn them off.
If the number of users are more , you can look at using Java's Thread Pool Executor .
Since you are going to use a servlet container then it's going to manage multithreading for you. Every servlet request will be executed in a different thread. In that scenario one servlet call would execute on block of code from provided XML in a single threaded manner. If there are several sites declared per block of code they would be visited serially. Other user in the same time may call the same server with other block of code running in parallel with the first one.
I've been playing with Java Servlets and Ajax a bit, and I've got
a situation on which I would really appreciate advice.
Let's say I have HTML page with a start and stop buttons, and as a result of clicking start button,
overridden doGet (or doPost) method on a servlet is invoked which computes something that takes a long time to complete.
(e.g. a giant loop, or even Infinite loop, doesn't matter, I'm interested in concepts here).
So, I'm asking you:
1.What would be my options to kill / shut down / halt / exit
doGet method whan I hit stop button on a web page?
Do I use threading here, or there is simpler way?
I take it that using System exit is not a very good idea, right? ;)
2.So, let's say I implement code for stopping doGet method.
What would happen If I hit start on one browser(e.g.IE), and while this long
computation takes place open new tab or other browser(e.g.Firefox) and open same url
and hit stop? Would that stop my original computation? Is there any easy way to avoid this?
I know that questions are a bit off, as I'm just starting with server-side of things. :)
Any suggestions would be greatly appreciated!
your stop handler can set a flag in the session context, which the long-running thread will occasionally check and exit if necessary.
you can avoid the multiple browser issue, by generating a unique task id each time the page is loaded. then you can only start or stop a specific task. this id can be a key in the session.
I think you need some kind of new process started after you submit request and this process should answer responses during runtime (showing progress for example via AJAX). Also it should check if there is new request with stop command. Page should be AJAX one with progress/result/stop button.