queueHandler = new QueueUpdateHandler(queueHandlerThread.getLooper()); queueHandler.setMaximumMutationExecutionTime(maxMutationExecutionTime); queueHandler.postDelayed(new Runnable() { @Override public void run() {
Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Got message to take action on the mutation queue."); if (msg.what == MessageNumberUtil.SUCCESSFUL_EXEC || msg.what == MessageNumberUtil.FAIL_EXEC) { if (!isMutationInProgress()) { checkAndHandleStuckMutation();
@Override public void onResponse(@Nonnull InterceptorResponse response) { callBack.onResponse(response); if ( callbackForPersistentMutation != null) { JSONObject jsonObject; try { jsonObject = new JSONObject(response.clonedBufferString.get()); callbackForPersistentMutation.onResponse(new PersistentMutationsResponse( jsonObject.getJSONObject("data"), jsonObject.getJSONArray("errors"), request.operation.getClass().getSimpleName(), object.recordIdentifier)); } catch (Exception e) { callbackForPersistentMutation.onFailure(new PersistentMutationsError( request.operation.getClass().getSimpleName(), object.recordIdentifier, new ApolloParseException(e.getLocalizedMessage())) ); } } appSyncOfflineMutationManager.setInProgressPersistentMutationAsCompleted(object.recordIdentifier); queueHandler.clearInMemoryOfflineMutationObjectBeingExecuted(); queueHandler.clearPersistentOfflineMutationObjectBeingExecuted(); queueHandler.sendEmptyMessage(MessageNumberUtil.SUCCESSFUL_EXEC); }
queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC); return; queueHandler.setMutationInProgressStatusToFalse(); return; queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC); return; queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC); return;
if (queueHandler.setMutationInProgress()) { Log.d(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Processing next from persistent queue"); PersistentOfflineMutationObject p = persistentOfflineMutationManager.processNextMutationObject(); queueHandler.setPersistentOfflineMutationObjectBeingExecuted(p); if (queueHandler.setMutationInProgress()) { Log.v(TAG, "Thread:[" + Thread.currentThread().getId() + "]: Processing next from in Memory queue"); currentMutation = inMemoryOfflineMutationManager.processNextMutation(); queueHandler.setInMemoryOfflineMutationObjectBeingExecuted( currentMutation); setInProgressMutationAsCompleted(currentMutation.recordIdentifier); inMemoryOfflineMutationManager.removeCancelledMutation((Mutation) currentMutation.request.operation); queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC);
sendEmptyMessage(MessageNumberUtil.FAIL_EXEC); sendEmptyMessage(MessageNumberUtil.FAIL_EXEC);
message.obj = new MutationInterceptorMessage(); message.what = MessageNumberUtil.SUCCESSFUL_EXEC; queueHandler.sendMessage(message);
appSyncOfflineMutationManager.setInProgressMutationAsCompleted(identifier); queueHandler.clearPersistentOfflineMutationObjectBeingExecuted(); queueHandler.clearInMemoryOfflineMutationObjectBeingExecuted(); queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC);
@Override public void onResponse(@Nonnull ApolloInterceptor.InterceptorResponse response) { Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: onResponse()"); if(shouldRetry && ConflictResolutionHandler.conflictPresent(response.parsedResponse)) { //Set shouldRetry to false. Conflicts will only be attempted once. shouldRetry = false; //Found Conflict String conflictString = new JSONObject((Map)((Error) response.parsedResponse.get().errors().get(0)).customAttributes().get("data")).toString(); //Send a message to the Queue handler to retry Message message = new Message(); MutationInterceptorMessage msg = new MutationInterceptorMessage(originalMutation, currentMutation); msg.serverState = conflictString; msg.clientState = clientState; msg.requestIdentifier = recordIdentifier; msg.requestClassName = currentMutation.getClass().getSimpleName(); message.obj = msg; message.what = MessageNumberUtil.RETRY_EXEC; queueHandler.sendMessage(message); return; } //Call the customer's callback customerCallBack.onResponse(response); //Set the mutation as completed. appSyncOfflineMutationManager.setInProgressMutationAsCompleted(recordIdentifier); //Send a message to the QueueHandler to process the next mutation in queue Message message = new Message(); message.obj = new MutationInterceptorMessage(); message.what = MessageNumberUtil.SUCCESSFUL_EXEC; queueHandler.sendMessage(message); }
@Override public void handleMessage(Message msg) { if (msg.what == MSG_CHECK) { // remove messages of the same type networkUpdateHandler.removeMessages(MSG_CHECK); // set shouldProcess to true Log.d(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Internet CONNECTED."); synchronized (shouldProcessMutationsLock) { shouldProcessMutations = true; } // Send EXEC message to queueHandler if (queueHandler != null ) { Message message = new Message(); message.obj = new MutationInterceptorMessage(); message.what = MessageNumberUtil.SUCCESSFUL_EXEC; queueHandler.sendMessage(message); } //Trigger DeltaSync to handle Network up events. AWSAppSyncDeltaSync.handleNetworkUpEvent(); } else if (msg.what == MSG_DISCONNECT) { // Network Disconnected, pause mutations Log.d(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Internet DISCONNECTED."); synchronized (shouldProcessMutationsLock) { shouldProcessMutations = false; } //Propagate network down event to DeltaSync AWSAppSyncDeltaSync.handleNetworkDownEvent(); } } }
@Override public void onFailure(@Nonnull ApolloException e) { Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: onFailure() " + e.getLocalizedMessage()); if (e instanceof ApolloNetworkException ) { //Happened due to a network error. Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Network Exception " + e.getLocalizedMessage()); Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Will retry mutation when back on network"); queueHandler.setMutationInProgressStatusToFalse(); return; } shouldRetry = false; //Call the customer's callback customerCallBack.onFailure(e); //Set the mutation as completed. appSyncOfflineMutationManager.setInProgressMutationAsCompleted(recordIdentifier); //Send a message to the QueueHandler to process the next mutation in queue Message message = new Message(); message.obj = new MutationInterceptorMessage(originalMutation, currentMutation); message.what = MessageNumberUtil.FAIL_EXEC; queueHandler.sendMessage(message); return; }
void handleMutationCancellation(Mutation canceledMutation ) { Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Handling cancellation for mutation [" + canceledMutation +"]"); //Check if the mutation being cancelled is the one currently being executed. if (currentMutation != null && currentMutation.request != null && canceledMutation.equals(currentMutation.request.operation)) { Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Mutation being canceled is the one currently in progress. Handling it "); setInProgressMutationAsCompleted(currentMutation.recordIdentifier); queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC); return; } //Otherwise, it is further down in the queue. Add it to the cancelled Mutations tests so that it can be handled when it reaches the front //of the queue. Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: Lodging mutation in cancelled mutations list "); inMemoryOfflineMutationManager.addCancelledMutation(canceledMutation); //Remove it from the persistent queue InMemoryOfflineMutationObject inMemoryOfflineMutationObject = inMemoryOfflineMutationManager.getMutationObject(canceledMutation); if ( inMemoryOfflineMutationObject != null ) { persistentOfflineMutationManager.removePersistentMutationObject(inMemoryOfflineMutationObject.recordIdentifier); } }
@Override public void onFailure(@Nonnull ApolloException e) { callBack.onFailure(e); if ( callbackForPersistentMutation != null) { callbackForPersistentMutation.onFailure( new PersistentMutationsError(request.operation.getClass().getSimpleName(), object.recordIdentifier, e)); } appSyncOfflineMutationManager.setInProgressPersistentMutationAsCompleted(object.recordIdentifier); queueHandler.clearPersistentOfflineMutationObjectBeingExecuted(); queueHandler.clearInMemoryOfflineMutationObjectBeingExecuted(); queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC); }
void setInProgressMutationAsCompleted(String recordIdentifier) { persistentOfflineMutationManager.removePersistentMutationObject(recordIdentifier); inMemoryOfflineMutationManager.removeFromQueue(recordIdentifier); queueHandler.setMutationInProgressStatusToFalse(); queueHandler.clearInMemoryOfflineMutationObjectBeingExecuted(); queueHandler.clearPersistentOfflineMutationObjectBeingExecuted(); }
void setInProgressPersistentMutationAsCompleted(String recordIdentifier) { persistentOfflineMutationManager.removePersistentMutationObject(recordIdentifier); queueHandler.setMutationInProgressStatusToFalse(); queueHandler.clearInMemoryOfflineMutationObjectBeingExecuted(); queueHandler.clearPersistentOfflineMutationObjectBeingExecuted(); }
@Override public void run() { Log.v(TAG, "Thread:[" + Thread.currentThread().getId() +"]: processing Mutations"); Message message = new Message(); message.obj = new MutationInterceptorMessage(); message.what = MessageNumberUtil.SUCCESSFUL_EXEC; queueHandler.sendMessage(message); queueHandler.postDelayed(this, QUEUE_POLL_INTERVAL); } }, QUEUE_POLL_INTERVAL);