I have a non-gui application where I want to asynchronously interpolate some data over time.
I had a look at the JavaFX Transition API and it looked ideal. However, for the transitions API to work it has to be run as part of a JavaFX application context - which I don't really want to add the overhead unnecessarily for.
I was wondering if anyone could suggest something equivalent? Or is this something I'm going to have to write myself?
I'd use java.util.Timer for that. You can give it a TimerTask (basically a Runnable) that it will execute every x ms (its period). It also runs on a background thread, which I assume is what you meant with asynchronously?
API: http://docs.oracle.com/javase/8/docs/api/java/util/Timer.html
Related
In the Play!-framework, code is triggered by requesting a URL from the server. The thing is, I need to have some code in the background running continuously from start-up, polling a database for new entries every few minutes, as if it were a normal program with a main() function. As far as I can tell the only way to run code is to navigate to a URL, but that's not what I want here. Any way to accomplish this?
Sounds like you want to use some kind of cron task (correct me if I'm wrong)
In Play 2.x it's done with Akka's scheduler mentioned in the docs
Even more informations and/or samples you can find on the original Akka's docs
In general: in Play you can just schedule some task in the onStart() method of the Global class and then repeat in desired duration as long as required.
Edit: of course Akka is built-in the Play 2.x from the very beginning, actually we could say that Play is built on top of the Akka ;)
I am doing a school java project using the NetBeans IDE. It includes some basic database manipulations. We were taught at school to use the following for linking one form to another:
new <form_name>().setVisible(true)
But this seem to slow down the whole application and there is a small lag for going from one form to another. I heard that using JDialog boxes is a solution to this problem.
So what's the right way to do it?
Better to not swap in and out of different JFrames. How many professional applications such as word processors do you use that do this that throw different windows at the user? Better to use one main JFrame and swap views (usually JPanels) in it via a CardLayout and occasionally show a dependent Window as a dialog when needed, especially when you need to get information in a modal way.
some basic database manipulations. .. But this seem to slow down the whole application
Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling Thread.sleep(n) implement a Swing Timer for repeating tasks or a SwingWorker for long running tasks. See Concurrency in Swing for more details.
(But also see #Hovercraft's advice re. CardLayout..)
I'm working on porting a desktop application (WinForm) to a web application (Java/Spring/JPA). The problems are many and I'm struggling a bit...
Now the problem is threading!
In the original application, that performs the export of certain data from the DB, there is a progress-bar indicating the progress of the process.
I want to port this progress-bar in the new web application. To do this I thought of using AJAX and use a separate thread to run the data export.
The main concerns are:
Am I following the right approach? Are there problems using multi-threading in web applications?
If during the export process F5 or refresh button are pressed what exactly happens? How can I stop the process?
How do I update the progress bar periodically? Do I have to make calls via ajax to the server?
I'm primarily an ASP.Net developer but from what I know of the HTTP protocol this just isn't the way to go about it. I've seen a lot of fairly clever solutions for this but in the end what becomes clear is that the HTTP protocol simply isn't designed to work like this.
Obviously you're aware that a flash or silverlight app would be able to do this but that comes with it's own set of issues.
Myself I prefer to keep all the weirdness on the server. In the past I've had to come up with a way to deliver several thousand emails through a web application and update the user on how it's coming along. I designed a set of tables to act as a queue. The web application would simply place any delivery requests in this queue and the progress bar would be determined by a request that checks the status of the items in the queue. Running in the background was a windows service which would also check this queue and was actually responsible for delivering the mail and setting the status of each item as it completed or failed.
It was a bit difficult to develop since windows services can be tricky but once it was up and running it was extremely smooth and reliable. Depending on your circumstances perhaps a simple scheduled task set to run every few minutes would do the trick for you.
I wouldn't necessarily jump straight to running a separate thread explicitly for the export. While it would be ideal to do this, the capability of the web container to do this is going to be a limiting factor. Your traditional Java EE app server generally discourages spawning threads for this (though you can hook up to a thread pool for this). Some containers are great at freeing up the threads from blocking until the work is done (Karaf with Jetty and Camel, for instance) so that they can service other web requests while the export is occurring. But my guess is that you're probably okay with the "start export" thread blocking until it receives a response.
How long does this export take? A couple of seconds, or are we talking closer to minutes here? If it's shorter, I'd think that just putting a little "Waiting" icon with the little circular spinner on it (using your favorite Ajax library, whatever that is) would be sufficient.
If you really want a true status bar that periodically refreshes itself, then yes you'd have to poll for it at some frequency. Presumably that could be a simple request that would load some kind of progress for the job from a database table for that job ID.
Find my answers Inline
I am following the right approach? Are there problem in using multi-threading in web applications?
-Yes you are on correct path. No there is no such problem in multi-threading in web application and its as easy as you do it in WinForm. Instead of using Dispatcher to update the UI, you would be making AJAX calls and with javascript DOM manipulation would take place.
If during the export process F5 or refresh button are pressed what exactly happens? How an I stop the process?
-Unfortunately there is no easy way. The standard way is, when such kind of processing is done and the user hits F5, you would show a dialog(with help of javascript) and inform user that the job is still running. If the user still wants to refresh then you have make another request to the server for cancelling the task.(You need to store thread id or cancellation token some where to cancel the task)
How do I update the progress bar periodically? Do I have to make calls via ajax to the server?
-The standard way is, generally you show show a loading image. IF you want to show a context senstive progress bar, it would mean you have to do polling. Here is an example by Dino Espito. Though its in ASP.NET, you could understand the underlying principle
Dino Espito
How can we create a separate thread to perform an operation until it is stopped (using say, a button) in Eclipse? I read that you need to use asyncExec() function (because I need the UI to be updated simultaneously). But for some reason, the Display class is not recognized in my IDE.
You should read this tutorial: Eclipse Jobs and Background Processing. The website also has a lot of good tutorials for other eclipse-related topics.
A part of my program needs to simulate a GPS. So I am setting up a client-server connection. Where on server my main application would run and on client it would send the GPS string periodically after a particular time interval. I am using JAVA for programming it and I am a bit new to networking area, so if someone can just give me an idea about How do i send my data periodically? The emphasis is on just one part. Periodically after a time interval.
you can use TimerTask Class for your solution. Here is a very useful link for its example.
In its run method you need to deploy your uploading code.
I am also working on same kind of project right now.
Add a java timer to your code that triggers at the interval you specify. In the timer handler, just run some code to send data to the server.
Use a TimerTask with a Timer.
Although your requirement might be simple enough, but i suggest you take a look into quartz scheduler.
It supports from plain simple timer tasks (like every minute or every xx seconds) to the more complex timing scenarios.
Here is one simple example that you can dive more deeply from the source code.