How to simulate different server responses, including offline? - java

An app I am working on relies on connecting to a private, remote server to login via an API.
The other day, I got an ANR message and I think it was due to the server being offline (its a Windows Server that was being updated).
In my app, I am using the following code:
new ReadJSONFeedTask().execute(url).get();
Which turns an AsyncTask into a synchronous task (I believe!)
And because the server was offline, I think the app got hung up, causing the ANR (it's my only lead at the moment, and all seems to make sense, despite the code above is inside a Service class, which shouldn't cause an ANR, should it?)
So, I want to try an simulate the server being offline. I tried replacing the URL with one that simply doesn't work, by removing some letters from the hostname, but that only triggered an "UnknownHostException".
Is there any way to achieve this kind of simulation? I know there are tools for browsers to simulate low bandwidth etc, but have no idea what could be done when my app is running on Android and is trying to connect to a remote server that I can't really reboot at will.

Related

get notifications when kafka producer is online or offline

I'm building a website using springboot as RestAPI and angular in frontend. Also, i will be getting data in my rest(consumer) from around 40-50 swing standalone apps(producers) through kafka. I need to implement online status for those swing apps. Basically, i need to know when and which one of swing apps went offline or back online. Is there a way to implement this? If this is impossible with kafka can you recommend something else for communication?
Clarification:
By online status i mean whether device on which swing app is installed has internet connection or not.
Swing app will be installed on around 40-50 devices on remote locations.
Eventually i got rid of kafka and used websockets. Using WebSocketHandler i had information when certain standalone app is connected or disconnected. When standalone app looses internet connection server is notified relatively quickly (within 60 seconds). Keep in mind that in my case standalone app will be sending data quite often. If this is not the case for you then i recommend looking for some other solutions and taking a look at #OneCriketeer comment regarding Nagios.

Android Networking Exception: Connection reset by peer

I have an Android application where I connect frequently to a web service that I control using SSL exclusively. The first thing a user does that results in network activity is login and I currently use the Ion library for this although I tend to think my issue is not specific to Ion. This works as expected except for one specific user that has limited all internet access on the Android device to only work with my web service. They have done this by using their router to create a firewall that blocks all traffic except the specified domains. When this firewall is turned on they can't login via the app because of the following error:
java.net.SocketException
recvfrom failed: ECONNRESET (Connection reset by peer)
They can use the browser to go to my website and access the same web service with the firewall turned on and everything works fine. In addition, they can turn the firewall off, login successfully (i.e. successfully initial the first connection), turn it back on and subsequent network connections continue to work. The user can even log off and log back in without issue. It continues to work with the filter on until they quit the app. When they then restart the app and try to login the error returns. This seems like very odd behavior and I'm not entirely sure it is an ion issue but I'm not sure what to do. Does anyone have any ideas on what could be causing this issue?
UPDATE:
Per a comment, I tested my application with Volley. It does appear that Volley works with the filter turned on. I would prefer to stick with Ion though due to some of its other feature so if anyone has any ideas on how to make Ion work in this scenario I would greatly appreciate the help.

Firebase server side OnDisconnect

I want to monitor firebase client connections from my java server.
I have found a lot of examples on how to use onDisconnect from the client side (iOS), and it works great.
But is it possible to monitor client connections from a java server using the java server sdk? I simply want my server to know about user disconnects, but without the client having to 'tell' the firebase database that it has connected/disconnected.
The reason for this is mostly security, and a wish to trust the client as little as possible to do important tasks. Ideally most of my data should be 'read only' from the client, except for some 'write only' request queues consumed by the java server.
From the firebase java server documentation: https://firebase.google.com/docs/reference/serverreference/com/google/firebase/database/OnDisconnect
The OnDisconnect class is used to manage operations that will be run
on the server when this client disconnects. It can be used to add or
remove data based on a client's connection status. It is very useful
in applications looking for 'presence' functionality.
It seems like what I want to do is possible and supported, but I'm having a hard time figuring out how to make my server monitor 100-1000 connections realtime.
Lets say I'm monitoring a database reference that I know clients are monitoring. In that case none of the options to 'remove on disconnect' or to 'set on disconnect' are useful.
Thanks a lot for reading this, I'm REALLY loving firebase right now, just a few more issues to figure out :-) Also sorry for my english, I'm not a native speaker.

Android 3g connection delay

I'll appreciate your help with the following:
Question:
I'm looking to rush the 3g connection and make it connect immediately.
Explanation:
I built an app (for Android), using HttpClient for connecting to my server, and Amazon's AWS API to connect to various Amazon services.
I noticed that occasionally, the connection establishment in 3g takes a lot of time, and it actually seems that Android just waits a while before sending the GET/POST/whatever request, and not even trying until then.
Few Clarifications:
the 3g connection IS working (good signal) and the problem is with 3g ONLY.
when trying to connect to the internet using a browser (e.g Boat Browser), the connection seems to "wake up" and those green&orange arrows at the status bar start lighting again. And..the browser works. Sometimes my request get "awakened" then as well.
My requests DO work, but usually it takes a while (at least 1 minute) until they actually initiate.
I can put some code if needed, but it looks like it isn't related to what I do, it's just how Android works (maybe it's waiting for many requests before initiating 3g connection?), and that some apps (like Boat Browser) force the connection to send their request, or just refresh the 3g connection maybe.
Thank you so much
Ok I found enough information to clear this up!
The best thing to do with AsyncTasks as connection threads is to avoid them apparently,
And use Google Volley instead.
Please look at this link to understand the nuances concerning AsyncTasks:
http://www.jayway.com/2012/11/28/is-androids-asynctask-executing-tasks-serially-or-concurrently/
(I found this link in a separate question but I can't find it, sorry).
And look at this link to understand what is Google Volley and how it's easier to implement a multi-threaded connection with it:
https://developers.google.com/events/io/sessions/325304728
So..To answer the question:
1. On some/most cases, your AsyncTasks will NOT run concurrently.
2. In order to "force" a multi-threaded connection, use Volley (can be configured to run serially btw) or read the link above concerning AsyncTask to "enable" it.

java client server update without polling

when building a server, one sometimes performs asynchronous tasks from client to server (which responds to client in asynchronous time),
or the server needs to send the client a message
now if the client is listening at all times (meaning polling) it takes a lot of resources which is problematic
here is where I assume the operating system steps in and assumes the role of polling for the appropriate port, and letting the application know using the appropriate event (the application subscribes using the OS API)
am I right in my assumptions?
how do I subscribe to a port using the OS's API? (lets say android for the sake of argument)
how is a message from server to client work exactly?
and how does the server know the client's IP at all times?
I have seen many questions in the subject, but wasn't able to figure out the big picture
Edit:
I am using GCM in android, but have seen other apps that does not use it and still manage to do it right, also it's a more general question as to what is the right approach in java VS. any operating system it uses (ubnutu, windows, android, etc.)
Totally right - polling is typically a waste of resources. Until recently, many apps would either keep a socket open and poll every few minutes to keep it alive, or make periodic HTTP calls to a server.
Nowadays, Google Cloud Messaging is used by most apps to push data instead of constantly polling. As you correctly guessed, this is implemented by maintaining a persistent connection with Google's servers. The advantage of this is that it's very efficient for battery life, and that all apps can use this one resource to send push notifications, instead of each app having to poll a different server or create its own persistent connection.
The idea is that you send requests to GCM from your server (this can be in response to user activity, etc), which sends it to all of the client's devices. You can either send a message with a small payload (up to 4kb) or a "send-to-sync" message, which tells an app to contact the server (e.g. to sync new data from the server after user changes).
here is where I assume the operating system steps in and assumes the role of polling for the appropriate port, and letting the application know using the appropriate event (the application subscribes using the OS API)
GCM pushes messages to clients, so there isn't active waiting like you'd see in a simple polling system.
how is a message from server to client work exactly? and how does the server know the client's IP at all times?
There's no need for servers to know the client IP, as any online android device will typically maintain a connection with GCM. Targeting specific users is done via User Notifications.
(Oh, and I realize that your question is more general than just Android, which I have more experience in, but iOS has a similar system in place. Some developers I've met like to use Parse for managing push notifications).

Categories