Access by one client at a time - java

My requirements are as follows.
I have a web application developed in java.
I have a link in the html page. When the first client clicks submit button in the html page the batch file should run.
Meanwhile when the other client clicks the submit button he should get message that the page is busy.
If the first client clicks release button then the other clients must be able to run the batch file.
How can I do that?

Set a flag when the submit button is clicked and clear it when the release button is clicked.
Depending on your architecture, the flag can be anything from a boolean variable in your code, a special entry somewhere in your database to a temporary file.
When the submit button is clicked, check whether the flag is already set. When it is, return an error message stating that the page is busy.
In pseudo code:
if (flagIsSet()) {
showPageIsBusy();
} else {
setFlag();
startBatchFile();
}
Remember to clear the flag when the batch file finishes or the release button is clicked and somewhere in a finally block.
You also have to make sure that only the client who started the batch file can release it and clear the flag.
As pointed out by Avi in the comments, you should also remember to synchronize access to the flag.

Pseudo algorithm:
Modify the batch file to run a servlet before and after batch operations.
Before running batch file, set a servlet or flag that will register a file or database or a session variable that I am being executed and at completion of batch file, set a servlet or flag that will register a file or database or a session variable that I am being released
When user clicks release button, run the same servlet which will relase the flag.
When User submits submit button, check for that flag whether it is executed or released, depending upon you alert your client.

you could use a field in servlet class (a big nono otherwise) for a flag.

If your application happens to run on multiple load-balanced servers, then you face the same problem I do :) I submitted a question earlier about it, but unfortunately I didn't get any response. In any case, you can see my solution for the problem as an answer to my question here.

That is a Singleton or better an Application-Bean. This Bean should kick of your long running background task and provide information about it, e.g. if it is running and maybe stuff like when did it start, who started it and if you could the progress, so you could display a progress bar as well.
If you would tell us, what web application framework you use, we could give better tips, I think.

Related

Stop running method in managed bean when user stops listening for response

I have a web application built using JSF 2.2, where a user can upload a file which will then be validated.
The validation can sometimes take an extremly long time so that the user gets tired of listening and clicks something else on the site. When this happens I want the validation to stop but it just keeps running eventhough the user is no longer listening for a response.
I have been googleing for a while but can't find any suggestions on how to handle this. My validation method is in a viewscoped managed bean.
Any suggestions on how to solve this or what to look into?
Depending on your website and on what you want to achieve, I would suggest:
If the fileupload is within a dialog, you can attach a listener to the dialog itself. When the listener is called just stop the validation progress in your bean.
You can try to make the dialog modal, it prevents the user form clicking anywhere else on the page (except the experience users which are able to remove the modal, and the dialog from the document DOM by using e.g., Firebug or Developer Tools)
The same applies for a block ui script.
Imho the best approach would be to attach a window.beforeunload listener or something similar. This listener can then call a backing bean method to stop the validation.

How to intercept Windows startup event in Swing

I created a JFrame with a few labels. I need to display a JOptionPane with the message "welcome user" once. If the frame is re-opened before rebooting Windows, the JOptionPane should not appear, but if Windows is rebooted, the JOptionPane should appear again.
How am I able to detect whether the system has been rebooted, since the last time my application ran?
So, as #Kayaman mentioned, there isn't an isRebooted() method in Java... but you can make one.
Assuming you are working on Windows platforms (you would need to implement a separate version for other OSes), you could query the system event log. With all the stuff that gets put in there I reckon it would include an event for "logged in", so all you need to do is find a way to look up the last "logged in" time and see if it has changed since the last time you checked.
Accessing the event log is a windows specific trick, and so won't be in the native java api, however there is a question on how to access the event log from Java which you could use as a base.
Drop a BAT script in the Autostart folder of the user which creates a file in a specific location:
echo JUST_STARTED > C:\NAME_OF_YOUR_APP.TXT
In your app, check whether the file exists. If it does, delete it and display the message.

Vaadin and Liferay IPC Out of Sync error

I'm using 2 Vaadin Portlets on the same page in Liferay. The first one shows a Table of entries and each row has a button to show details about this entry.
When the button is clicked an IPC event is send which is received by the second portlet, which then switches also to a table view showing the content.
By clicking a close button on the first portlet, the second one will receive again an IPC Event and go back to its original state, which is a blank view that has only the Liferay IPC listener attached to it.
My problem is that after the third click I get an out-of-sync error by Liferay. When the view is changed I always attach it to the main window. So I don't create additional windows that have the same name.
When I use only one portlet on the page I can switch back and forth without any problem. Could it be that the at some point the browser want to fire an event on the client side, but the IPC is already gone on the server side ?
Its really hard to determine the root of this problem.
You are on the right track. IPC works on client side and out-of-sync is caused by non existing component being called from the client to the server.
There might be a few thins causing this, but some scenarios to check:
You say you have a close button that clears the display. Calling Application.close() maybe? This actually might cause a new (server-side) application instance to created and called instead of the original.
You might be creating a new instance of the IPC component, but the old one is still registered and tries to send something (to its non-existing server part).
JavaScript timing issues could cause the IPC events to be sent in different order that you might expect. I see this unlikely if it the behavior is always consistent, but still a possibility.
Hope this helps you to narrow down the case a bit and find a solution. Keep this question updated.

How to remove all file when app is removed by user?

I have blackberry app (java). My app stores some informations in files (file:///store/home/user/app_name/). When user removes app, these files don't delete.
I want to delete files when app is removed by user.
What is solution?
Don't store files this way - use Persistable for your app data instead: http://docs.blackberry.com/en/developers/deliverables/17952/Storing_objects_persistently_1219782_11.jsp
There are two "standard" ways to uninstall an application. The first, via Options - Applications menu. And the second, via javaloader -erase -f command.
Theoretically you can intercept the uninstall event for the first way (when you use "Options - Applications" menu), but you need additional application that receives this event and does the cleanup action.
To intercept and recognize an unknown event I do the following.
I write a simple BlackBerry app, that implements and uses GlobalEventListener and runs in background, and prints every intercepted global event guid to the system output and/or log with timestamps.
Now, uninstall your app from Options menu and inspect logged global event guids. After that install/uninstall another applications. And then inspect log/system output window to find out which events are related to uninstall action.
It does not guarantee 100% result, but at least you will get fun with coding, logging and exploring.

how to disable Windows shortcuts using java?

Am developing an online examination system using the jsf framework. My requirement is that the candidate should not be able to use the windows shortcuts like alt+f4, alt+tab, window key and come out of the system. Once he starts the test he should finish. Is there any way to disable these shortcuts? Like running a batch or. Something?
That's not possible.
You should look for an alternate approach. Register in the DB when an user starts the test. Register in DB when an user finishes the test. On every start of the test, check the registration in DB. If there's an entry indicating that the test has started, but nowhere an entry indicating that the particular test has finished, then handle accordingly. Show an error message telling the user that s/he did it wrong previous time and that s/he should contact the admin, etc.

Categories

Resources