I am working on an android application, which is sends and gets data from MS SQL server via java servlets.
I need to update the UI of the application when Database has been updated.
I was thinking of implementing a looper class which will call a servlet via HTTP asyncTask, and call the servlet every few minutes. But it will be lots of work for the application and will slow down and also the UI needs to be updated as soon as the database has been updated.
Is there any way I can invoke the android application, from the servlet as soon as the Database has been updated? I cannot wait for the android application to make a call to servlet and check if database has been updated.
Any help would be greatly appreciated.
This is the first time I am writing a question on SO, please pardon my mistakes and suggest me how to improve.
Big thanks,
Ashley
A java servlet is stateless. This means that the connection is not maintained. The java servlet knows nothing of your application and cannot communicate directly with it.
If you must maintain the servlet setup then you can use Google Cloud Messaging to push the update to the user's phone. This is not exactly instantaneous though and there is a delay.
The thing that would work best for you is to change your java servlet into a java application that has a socket. You would then communicate with your java application via the socket and keep it open in a background thread. This thread will hold a callback to your activity which can populate it when a response is sent back over the thread to say data has been received.
Related
This question already has answers here:
How to run a background task in a servlet based web application?
(5 answers)
Closed 1 year ago.
Sorry if asking sth dumb but I'm completly beginner. I'm trying to write simple web app using java webapp archetype (servlets/maven). In app clients will be sending some data to server, then that data will be precessed on server side and send back to clients after constant interval. My problem is that I don't know how to create method that runs continously on server side (something like main class). Is it even possible to create sth like that?
At a low level, this is what a server does: it listens on a port for client requests and when a request arrives, the server processes it. But if you want to build a web application, you don't have to work at this level, but at a much higher level: you run your code in a web server like Tomcat or Netty, that handles all the low level work, and just invokes your code when a client connects to it. So I would advise you to first choose a server and follow a tutorial on how to run a web app, for example read the Netty docs.
If you want to have some background threads that do work independently of the client requests, you can register a ServletContextListener implementation, that let you do work when the context is initialized (your application starts) and destroyed (your application ends). For example see the tutorial at https://mkyong.com/servlet/what-is-listener-servletcontextlistener-example/
I am developing a servlet based application. One situation is that a client requests some data from a database which is sent back in the form of html. The client will modify this data and then sent it back to the server. Now the twist starts. There is not a single client. So multiple clients can request the same data. So what I am doing is that when the first client makes a request, this request is stored somewhere so that when the next user makes the same request he is denied the data.
Now suppose the first user gets the data and 2nd is denied. Now while the first user is on the html page which allows him to modify the data. I want to send continuous javascript async post requests at a fixed interval to inform the server that the client is active.
At the server side I need a thread or something which can keep waiting in a loop for the javascript async requests and if the request is not received within the fixed time then the thread removes the saved request so that future requests to the data will be accepted.
I have searched the entire day and looked at things like async servlets, ServletContext listener and scheduledExecutorservice. I dont want to use scheduledExecutorService as it is invoked at app startUp which I dont want to do since this specific situation is a minor part of the code and to handle it I dont want something running all the time. I need some background service which keeps running even after the server has returned requested data.
Servlets won't fulfill your requirements, therefore you should use WebSockets.
As per my understanding, you are trying to push data from the server, therefore you need to a push architecture instead of pull architecture (Servlets are based upon pull architecture).
Java has native support of WebSockets
You can find several tutorials on how to use WebSockets in a Java Web Application.
Here is a link to a basic WebSockets Tutorial.
Hope this helps
I just got started with Java Web Application Development and I've been developing a Web Application with Java Servelts using Apache Tomcat where customers can book appointments with hair stylists. Now I want to add a feature where stylists are notified by a push notification in real time once a client makes an appointment. I did my research and I found out the following ways:
Long Polling
Server Sent Events/ Event Source
Web Sockets
Since the necessary communication is asynchronous from the server, I feel that the Server Sent Events would be the right choice.
I came up with an idea where the book event generates a notification which is stored in a separate table. A thread monitors the data source for any new notifications and notifies the targeted receiver once a new nofication is found.
The issue I am facing here is how to identify the necessary active client out of several clients and target the data to it.
I've been looking everywhere in the internet and I couldn't find any Java Servlet and Tomcat implementations for reference.
Is it possible to implement my idea using just Servlets in a Tomcat container?
If there is a better approach to this problem, please let me know.
Maybe I'm overthinking this but I'd like some advice. Customers can place an order inside my GWT application and on a secondary computer I want to monitor those submittals inside th eGWT application and flash an alarm every time an order is submitted, provided the user has OK'd this. I cant figure out the best way to do this. Orders are submitted to a mysql database if that makes any difference. Does anyone have a suggestion on what to do or try?
There are two options: 1) polling or 2) pushing which would allow your server (in the servlet handling the GWT request) to notify you (after the order is successfully placed).
In 1) polling, the client (meaning the browser you are using to monitor the app) will periodically call the server to see if there is data waiting. It may be more resource intensive as many calls are made for infrequent data. It may also be slower due to the delay between calls. If only your monitoring client is calling though it wouldn't be so resource intensive.
In 2) pushing, the client will make a request and the request will be held open until there is data. It is less resource intensive and can be faster. Once data is returned, the client sends another request (this is long polling). Alternatively, streaming is an option where the server doesn't sent a complete request and just keeps sending data. This streaming option requires a specific client-/browser-specific implementation though. If it's just you monitoring though, you should know the client and could set it up specifically for that.
See the demo project in GWT Event Service
Here is the documentation (user manual) for it.
Also see GWT Server Push FAQ
There are other ways of doing it other than GWT Event Service of course. Just google "GWT server push" and you'll find comet, DWR, etc., and if you are using Google's App Engine the Channel API
I have two threads, each of which handle syncing data one way either from the server or to the server. The thread for getting data off the server needs to run once a day. The other sending data to the server needs to run every 15 mins. I am currently using an Alarm Manager to create repeating alarms for each of these threads. This is then received by a BroadcastReceiver, from which i call an activity, which then according to the data passed into the activity either runs the to server syncing thread or the from server syncing thread. I am using the activity to display a dialog box, to prevent the user from using the application until the syncing has been completed as they both access the database required by the application. Is this the correct way to accomplish this task, or are there better alternatives?
Thank you in advance
This question is not really fit for SO... This is more a debate without any details on how your app works.
Anyway I would use an Android Service to do so. You do not need to bother the user just to upload data. Also why block the use of the app for uploading? Since for uploading you only need to read, just make a snapshot of the current data and upload it. Any changes the user is making right now will be uploaded in next upload, so that's not a problem.
For downloading, you most likely do need to block the app use, but maybe not. This depends on how the app works. You could start DB transactions to avoid doing that.