How to send periodically data from client to server - java

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.

Related

What is the correct way to receive data from a server?

Ok so to explain I have server in Java that receives data from a C++ program that I have no control over. So the data is transferred over TCP/IP and the Java server just receives it. The volume of incoming data is huge and it comes at a very fast rate, just imagine a terminal printing loads of lines really fast.. I don't need to get all of it, just as much as I can would be great..
Now I need to send that data to an android device and display it.
I have no idea what is the best approach to so this.
I am currently pinging the server from my android device every 50ms using an executor. Then I use a handler to display the data. I have already posted a question with code here: related question.
Obviously the android app will need to ping it while the app is running so I can't avoid the while(true) scenario.
I have read about 100000000 ways to do concurrency in Android, and I do not know what to choose for this.
I tried simple Threads, they were to hard to manage.
I am now running a ExecutorService
I also read about ScheduledExecutorService, but they need a time
duration right ? My app can be running for 5 hours or 5 minutes or 5
years.
I read about HandlerThreads too, but I am not sure they are ideal for
this scenario.
Any help would be great.

JavaFX Transition Equivalent

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

Continuously-running code in Java Play! Framework

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 ;)

Web applications and multi-threading

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

Scheduling a time in the future to send an email in Java or Python

I'm writing an application and I'd like it to somehow schedule an email to be sent at a later date (likely an hour after it is run). The programming language will be Python or Java.
Any open-source tools available for that purpose?
EDIT: I forgot to mention it's to be run after a test run, so the application will already be down and I believe the Quartz solution wouldn't work. Would this be possible?
Ideally, I'd like to hear that SMTP protocol has some hidden stuff that allows this, and would just require adding some flag to the message and email providers would interpret as to having to send them later.
Quartz Scheduler can be user for this kind of asynchronous jobs.
Quartz is a great Java library for functions that you want to run at a certain time, after a certain time interval, etc.
There is also the Timer class in the JDK.
If you are to use Java, try Quartz, an open source job scheduling framework.
You can build the actual email to send, using JavaMail (with attachments and all), save it to disk, and then delegate a "mail foo#bar.com < textfilefromjavamail" to the Linux batch system.
There is an "at" command which will most likely do exactly what you want.
I don't think standard SMTP protocol has such a feature, so if you want to be platform-independent, you will have to search for another solution.
How about writing your message to a queue (local database, for example) with a timestamp and then have some program watching it periodically and send pending emails out?
Is the delay an exact timedelta or is it "1-2 hours later"? If it is the latter, than you can have an hourly job (cronjob starting every hour or a background job sleeping for an hour), which would then send out the emails.
Answer 1:
In Python, use threading.Timer to schedule in the future; use smtplib to send an email. No external library needed.
Answer 2:
Sounds like you want the sending program to quit rather than having it wait in the background. You may use cron for this. Alternative just use the unix command sleep and mail:
$ { sleep 3600; echo "hello world" | mail -s the-subject destination-email; } &
P.S. I don't believe SMTP have anything for you in this case. You are really looking for an MTA that has scheduling feature. Though I'm not familiar with it to make a recommendation.

Categories