Connect two android apps via http REST and EJB3
I'm writing application for android where two devices should communicate between each other via internet. In addition to this task they also communicate with the EJB3 server via REST. So I decided to kill two birds with one stone and use REST+EJB3 for transferring data between two paired android devices. So the scenario I implemented is something like this:
- Both devices connect to the server and acquire session id.
- First device sends data to the second device
- Server gets the data but does not end the http request, instead it puts into a waiting pool
- Second device asks for data
- Server transfers the data to the second device and releases waiting connection (and thread) for first device.
- If there are no f开发者_JS百科irst or second device requests then opponent waits for a timeout on a server side, then sends the request again. We need to wait for the data on the server side to give immediate respose after data is arrived.
So in this schema I see two drawbacks: - Waiting thread on the server side - they consume server resources and as the result limit server throughput - If the server thread will not wait for an answer with timeout, then the client should repeat requests on and on and spend a lot of traffic.
What is the best practice solution for such problem?
P.S: Forgot to mention that two devices should exchange data as smoothly and quickly as possible.
You will need to use C2DM http://android-developers.blogspot.com/2010/05/android-cloud-to-device-messaging.html
When message needs to be sent from A to B - A should connect to server and depending on data kind/amount - server will either push data via C2DM or just tell device B to come back and grab data.
I would store data on server anyway. If push fails - you can retry it. No need to reinvent wheel. Most issues/problems already solved in C2DM
精彩评论