Im trying to do following:
Send long executing request (I use here Events.echoEvent)
Show modal dialog (Wait...) with "cancel" button
If user press "cancel", dialog is hidded and event method should not be executed.
If user don't press button and wait. event method is called and close wait dialog
How can I do that?
ZK normally does all it's work for a single request on the server in a single thread. Events.echoEvent lets you get around that by finishing the request, and then calling back to the server immediately in a brand new request (and thread). The user's interaction with the client fires off a new request, so that will be a new thread also.
So, in your example above, you will be working with three threads:
for the original request which calls echoEvent
for the new request created by the echoEvent
for the request created when the user clicks 'cancel'
Obviously, the first will be long gone by the time the last two get called, but this is what you need to be thinking about in order to solve your problem.
You'll find plenty of discussion on StackOverflow about getting two threads to interact, or more specifically, getting one thread to interrupt another.
I'll refer you to 'How to stop threads in Java?' where the accepted answer favored sharing some sort of 'stop flag' over directly calling interrupt on a thread.
In your scenario this would play out with the long running process doing it's work while periodically checking the stop flag (a simple boolean). When the user clicks 'Cancel', you just need to flip the flag to true.
You can try
Create a thread to do the long operation as Sean mentioned above
(this is an independent thread, not ZK request thread)
Create a timer to check the status of that thread periodically.
(this will create a javascript timer to send ajax request periodically at client side)
And customize the busy mask by ZK Client Side programming to add the cancel button.
(the button click perform another ajax request)
Please refer to the similar article at stackoverflow:
Override “Processing” in ZK
Edit:
There are some related articles at my blog:
ZK: Customize the mask for showBusy
ZK: Adding abort button to busy mask
ZK: Mask page manually
Related
I have a simple action listener in a Swing app in Java Web Start (believe me it works great 99.99% of the time) that is getting called multiple times from the UI. Here is the scenario
the action listener calls a server side (EJB) method which populates
some tables in the database
the server side can take a long time (upto 1 minute) to process the
request
the action listener is meant to call the back end just once
in 99% of the cases (in production) the action listener works as
expected
there is 1% of cases where the back end method is called multiple
times resulting in data corruption in the database
tried to simulate with 2 UI's calling the back end with the same
inputs, but couldn't replicate
I am thinking there could be 2 hypotheses
the action listener is calling the back end method multiple times (possibly double clicks)
the back end is too overloaded to maintain thread integrity
I have seen the multiple click action in the case of HTML buttons in a browser and handled it with javascript/jquery. Does Swing have a similar hyper sensitivity?
I would like to use Eclipse debugger to diagnose thread problems, but don't know how to simulate this scenario. Stopping one thread is tripping the whole thing from launching another thread.
In any event, the Swing application is hyper sensitive, if anything. It does the job of passing the payload to the DB, but it is doing multiple times in 1% of the cases. I can write some DB code to filter the data, but I prefer a fix in Swing, if that is the root cause.
I tried to recreate the bug scenario with Sikuli by sending multiple clicks
to the button but in vain. Can you suggest me a way to simulate this possible UI/Threading bug scenario so that I can apply relevant fix? This has to be feasible as Swing has been around for decades now and lots of legacy apps use Swing and EJB.
Your help will be gratefully acknowledged.
Thank you
the action listener calls a server side (EJB) method which populates some tables in the database
A long running task should not execute on the Event Dispatch Thread (EDT). So I would suggest that when you click your button you would:
disable the button to indicate it is processing the task
start a SwingWorker to update the database on a separate thread.
when the SwingWorker finishes executing you enable the button.
This would prevent the user from clicking the button twice while the task is running.
Read the section from the Swing tutorial on Concurrency for more information on the EDT and on the SwingWorker.
I have currently the following problem:
I got a server that provides and API via Google Protobuf. A Program (in this case an Android App) can request informations. I have an Activity in Android that will request Informations in the Background via a Runnable, which will then process the network request and dispatch to a function on the mainthread to update the UI with the new informations. Therefore I give the runnable a final interface with the function to dispatch which is implemented by the Activity. Now as the network stuff happens in the background and the UI should not block (e.g. with a Loading Spinner) I dont know what happens when the user switches to another Activity while the Runnable is still active. Will it still dispatch properly even if there is another Activity active? What is good design practice to solve this.
Of course I thought about adding a variable to the Runnable to mark it as dont dispatch, but there still could be race conditions (it gets dispatched while the user hits the back button, which would already have the dispatched function in the queue for the mainthread before marking the Runnable as aborted).
If you need any additional informations, please ask, I am happy to provide them.
My application is using Struts 1.x and it's running on WAS..
All action classes are working fine except one wherein I click on one button and one action(which is expected to complete in 1hour) is called and then it starts executing ..the issue comes when same action is called after few minutes without any button trigger or any change of code.This happens after every few minutes for n number of times...
If anyone has any idea about this please let me know.
A request that takes 1 hour to complete is not normal: you should redesign this functionality.
Briefly, you have this problem because the request takes too much time to complete. For a technical explanation of the cause of your problem see Why does the user agent resubmit a request after server does a TCP reset?
Solution: create a separate thread (or a pool of parallel threads, if possible) to handle the long-running computation and send immediately a response page saying "Request accepted". This page could also use JavaScript to send periodically an "is it completed?" request to the server. You should also provide a mechanism to inquiry for pending requests, so users that close the browser without waiting for the final "Yes, completed!" response can get the result when they want.
I have a web application deployed in Tomcat 7 and one of the transactions there is loading a text file to the application then the application will read it line by line then insert all the records to the database.
I would like to put a cancel button on the page to let the users decide if they would like to cancel the transaction especially if they're uploading big files. I'm thinking to just put a link that goes to another page to serve as cancel button. Will this work?
Would the thread continue if the client uploads a 15MB file, which takes about 10 minutes to be processed, then decides to cancel it by clicking the link I provided as a cancel button?
Once the file is uploaded to the server, start a thread to do the processing and store a reference to it somewhere (like a Hashtable) and give it a unique ID. Then render the page back to the user with the cancel button, and store the ID you used earlier to keep a reference to the thread in the page.
When the user clicks cancel, go back to the Hashtable, get the thread with the ID and send it a message to make it stop the import.
Of course, your method that will stop the thread from importing data will need to take into account current database transactions to be sure you dont leave your database in an invalid state.
I think it is not possible to stop a request that is under transmission to the server. Once you click upload you make a HTTP request, and the process begins and clicking "cancel/link u provided" at a later point, would be a different HTTP request and since HTTP is stateless, it would not know anything what happened earlier or what to stop. So as expected it is not possible implicitly. But there may be something that can be done to do this which I am unaware of now. But if you press "esc" key that would terminate the request sent to the server. And since this is the only solution till now then on UI you can write this javascript code:
$('.your_link').click(function(){
window.stop();
});
window.stop() will not work on IE. for IE you can use:
document.execCommand("Stop");
I am a Java newbie and an Android newbie too. I am working on a game and trying to understand the exact nature of events in Java and Android. I have a few questions to help understand the correct way to do event handling in my app.
Its a network game and so I need to check if the user made a move or not to update the view. Also I need to prompt the user to make a move if he takes too long. For this I have two threads -
Timer thread expires every 10 seconds and calls updateview if needed or prompts user to make a move.
Event thread gets created when user clicks on the screen to make a move or clicks on menu etc.
Is this the correct approach? These two can be fired at any time.
Here are the issues I see with this -
What happens when one thread gets run when the other one is active.
Which thread has precedence if both are started at the same time.
Do events in the timer thread get queued up?
If so can I pick which one in the queue to use?
Can I cancel events in the queue? For e.g. if I have 2 updateview events lined up in the queue I only have to call it once.
Thanks for any inputs.
P
I would suggest reading up on Android AsyncTask.
Consider that you can implement a timer WiTHOUT using a thread. Use a single Handler switching on what and send a postMessageDelayed(what 0,milliseconds) to the handler say every one second. You could set a counter variable to zero and check the flag every one second in the what 0 handler, incrementing the counter by one. If the value is >= ten, post a message and reset the variable to zero. If the user selects an action, reset the instance variable to zero.
A time consuming action can be run in a separate thread that messages the handler, perhaps using what 1, on completion. Or you could run a time consuming action in a separate asyncTask.
JAL