Issue
We have an android tablet. We use a third party app to submit electric form. After submitting, a pdf file is generated at a location. We develop our own app to listen for created files in this location. We name it “ObserveFileService”. A pdf created along with a csv file. PDF is readonly and csv contains the pdf form fields-values. Our internal app will upload this pdf to AWS S3. We use android Service to fire scheduler every 2 minutes.
public class CloudUploadService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(() -> { uploadDoc() }, 0, 2, TimeUnit.MINUTES);
}
}
We use TransferObserver to track the state of loading process.
public void uploadDoc(String fileName, String path){
final TransferObserver observer = transferUtility.upload(
bucket,
key,
file
);
observer.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
}
@Override
public void onError(int id, Exception ex) {
}
});
}
Brainstorm
But somehow the methods of TransferListener never called. I thought that the uploading process has problem. Maybe TransferUtility is not working correctly, problem with AWS etc. However, other pdf files working, only some specific pdf file not working. What is the differences? I try to paste only the pdf to listening location, it worked! But if I paste this pdf along with csv, it didn’t work. The problem might be from csv file. I started looking at code block to process csv file and I found the infinity loop. It’s not about while (true){} but the condition is always true because it checks for the splitted files existing in other location. We no longer use these files but the other developer who modified the code, he didn’t remove this redundant code.
File checkExists = new File(dir + fileName);
boolean fileExists = false;
do {
if (checkExists.exists()) {
fileExists = true;
} else {
TimeUnit.SECONDS.sleep(10);
}
} while (!fileExists);
it’s suck.
What I learned.
- If the app hangs, it means there is a process somewhere stuck. Mostly the reason is infinity loop.
- If we debugging at somewhere but got no clue => we should look at other places, other processes.
- Always ask what is the differences?
- App is very special. It has services at the background. Sometime it hangs, we have to check all running services.
- Always look around the facing problem if can’t see the root cause at current place.