Java How to show timer via Server? - java

I would like to use a timer that counts up like: 00:00:00 -> 00:00:01 and so on , I tried it with
Long start = System.currentTimeMillis();
Long end = System.currentTimeMillis() - start;
but didn't worked. This is how i get my data from my localhost. I would like to use this to calculate the timer via my Server , but it doesnt work.
I just would like to see a timer alongside my uploaded file in my client GUI
Hope someone can help, if you have more questions ask me :)
ResponseEntity<String> response = restTemplate.postForEntity(format("http://localhost:8080/file/upload?uid={0}&time={1}", uid , time)
EDIT: This is how my Client JTable looks like Its not my idea but my supervisor just want to see what the average time is that the server needs to run the (.bat) File. And u cant calc how long a (.bat) file needs so i need a timer that counts up.
And i was thinking this should be possible via the server.

Without going into details if it is smart to do a server-side timer for uploads, you can't make a second-step with this
Long start = System.currentTimeMillis();
Long end = System.currentTimeMillis() - start;
because it will just execute at whatever speed your system is running at.
Something like the code below will trigger once every second, but you'll have to run that in its own thread as not to block the main-thread with the while:
while(true){
System.out.printLine("tick");
Thread.sleep(1000);
}

Related

RxJava. How do I make the Observable timer run in the background?

I need to make sure that after pressing one button, the other is not available for 15 minutes. To do this, I use a method like this:
disposableTimer = Observable.timer(15,TimeUnit.MINUTES)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(aLong -> {
buttonSimpleAct.setEnabled(true);
});
This works well when the program is active, but I need this timer to work even when the program is closed.
That is, if the first button was pressed, the application was closed and returned after 15 minutes, the second button should already be active.
Are there any ways to make the Observable work in the background, or alternatives to this?
What I'd do is save (in a persistent manner) the timestamp for when the button should be re-enabled and restart the timer with the remaining time when the app comes back.
// when the first button is clicked:
long reenableAfter = System.currentTimeMillis() + 15 * 60 * 1000L;
save(reenableAfter);
disposableTimer = Observable.timer(15 ,TimeUnit.MINUTES, AndroidSchedulers.mainThread())
.subscribe(aLong -> buttonSimpleAct.setEnabled(true));
// when the app starts
long reenableAfter = load();
disposableTimer = Observable.timer(reenableAfter - System.currentTimeMillis(),
TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
.subscribe(aLong -> buttonSimpleAct.setEnabled(true));

Waiting for elements to load does not work until I pause the script for few seconds

Using Selenium webdriver with java
Example we have lot of fields like the image below
I tried using explicit wait like
i) waiting for elements to load (but the elements are dynamically loaded and does not populate until i click and wait for couple of seconds)
but does not work until I pause for couple of seconds.
I am using the below method
I am creating a framework and wanted to know if in other organisations programmers do use similar methods to pause the script or is it not used ? because I need to use it so much.
Using below method as I did not want to use Thread.sleep courtesy stack overflow.
public static void customewait(int seconds){
Date start = new Date();
Date end = new Date();
while(end.getTime() - start.getTime() < seconds * 1000){
end = new Date();
}
}
You can use waits provided by selenium as following:
WebDriverWait wait = new WebDriverWait(driver, 5000);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("idOfElement")));
This code will wait for element to be visible for 5000ms and when it finds that element it initializes the object.
5000 is timeout time in milliseconds (ms)
you can read more at this link
Thread.sleep will stop the execution of the thread for the given time while Webdriver wait only waits till the mentioned condition is not met. So, the Best practice is to use web driver wait.
By ByLocator = By.xpath("Any XPath");
int seconds = 5 * 1000; // seconds in milli.
WebDriverWait wait = new WebDriverWait(driver, seconds);
wait.until(ExpectedConditions.presenceOfElementLocated(ByLocator));

Java - showing splash screen while application performs setup

I'm writing a program using Java and I need to use a splash screen to let the user know that the application is starting. I have tested how long the application is to set up using the following code:
long startTime = System.currentTimeMillis();
frame = new CFSMainFrame(); // main app
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("Total time: " + ((float)totalTime/1000) + " seconds!");
and it returns a total time of 2.66 seconds in average. I want to show a splash screen during this time. I have looked around and even checked the splash-screen API, but I don't find it very "noob"-friendly and was wondering if someone in here could give me a very simple example for showing an image during this time, there is no need for a fancy loading bar or anything..

How can I stop webdriver execution till my condition satisfy

I am doing automation for one application and need little help .
Scenario is in our application when we update/add something we are showing Loading image till it complete process. And in my webdriver it is not able to find next element until this loading image completes.
So I want to set condition like till there is loading image , stop execution and dont try to find next element , once loading image hide then find for next element. so How here I can stop web driver execution till loading image there.
I am trying something like :
WebElement loading = driver.findElement(By.id(AppConstants.loadingimg));
while(loading.isDisplayed()){
//What to put here to stop web driver execution for a while
}
I already tried all type of WAIT but not working for me in proper way so I want solution with above condition.
//What to put here to stop web driver execution for a while
As you said you already tried all type of WAIT and it's not working , So
In order to stop execution for a while you can make your thread to SLEEP
try{
Thread.sleep(timeYouWantToWait); // time in millis
}catch(InterruptedException e){
e.printStackTrace();
}

How can I schedule a particular thread in Blackberry

I want to auto-schedule a thread with a particular time interval. I also need to execute this in the background continously without hangs to the device.
I have tried this with Application Manager Class but it's for application scheduling and I need to schedule thread within the application.
I would use TimerTask:
public class MyScreen extends MainScreen {
private Timer mTimer;
public MyScreen() {
mTimer = new Timer();
//start after 1 second, repeat every 5 second
mTimer.schedule(mTimerTask, 0, 5000);
}
TimerTask mTimerTask = new TimerTask() {
public void run() {
// some processing here
}
};
}
see BlackBerry API Hidden Gems (Part Two)
use UiApplication.getUiApplication().invokeLater()
it accepts a delay and repeat parameters and will perform exactly what you need.
EDIT
I know this post is old but this is by far the best option to schedule repeating events and I would like to add that to stop the scheduled event the following is required:
//Start repeating "runnable" thread every 10 seconds and save the event ID
int eventId = UiApplication.getUiApplication().invokeLater(runnable, 10000, true);
//Cancel the repetition by the saved ID
UiApplication.getUiApplication().cancelInvokeLater(eventId);
Assuming you want it to run a thread on device startup:
Create a second project and list it as an alternate entry point.
In your UiApplication or Application main(), check the argument passed to the project. Do your periodic stuff there via Thread.sleep and don't call enterEventDispatcher.
search for "autostart":
http://docs.blackberry.com/en/developers/deliverables/1076/development.pdf
Or if you want to do something once a user "starts" it, then look into creating a new thread to do your timing stuff. Override your screen's onClose() and use Application.getActivation().deactivate() to throw the screen into the background.
Or there's a other ways to do something like this like invokeLater, etc. Maybe eventlisteners may do what you need, but you didn't give a lot of details.
As long as the application is running - just create the thread and after each bit of work call Thread.sleep for as long as you need it to stay dormant.
If you need it to wake up at a particular time, rather than just sleep for a particular time, then you can do something like the following:
Date wakeUpAt = ...; // Get this however
Date now = new Date();
long millisToSleepFor = wakeUpAt.getTime() - now.getTime();
Thread.sleep(millisToSleepFor);

Categories