public ApptentiveTaskManager(Context context, ApptentiveHttpClient apptentiveHttpClient, EncryptionKey encryptionKey) { dbHelper = new ApptentiveDatabaseHelper(context, encryptionKey); /* When a new database task is submitted, the executor has the following behaviors: * 1. If the thread pool has no thread yet, it creates a single worker thread. * 2. If the single worker thread is running with tasks, it queues tasks. * 3. If the queue is full, the task will be rejected and run on caller thread. * */ singleThreadExecutor = new ThreadPoolExecutor(1, 1, 30L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy()); // If no new task arrives in 30 seconds, the worker thread terminates; otherwise it will be reused singleThreadExecutor.allowCoreThreadTimeOut(true); // Create payload sender object with a custom 'retry' policy payloadSender = new PayloadSender(apptentiveHttpClient, new HttpRequestRetryPolicyDefault() { @Override public boolean shouldRetryRequest(int responseCode, int retryAttempt) { return false; // don't use built-in retry logic for payloads since payload sender listener // would handle it properly } }); payloadSender.setListener(this); ApptentiveNotificationCenter.defaultCenter() .addObserver(NOTIFICATION_CONVERSATION_STATE_DID_CHANGE, this) .addObserver(NOTIFICATION_APP_ENTERED_BACKGROUND, this) .addObserver(NOTIFICATION_APP_ENTERED_FOREGROUND, this); }
if (isSendingPayload()) { return false; sendPayloadRequest(payload); } catch (Exception e) { ApptentiveLog.e(e, "Exception while sending payload: %s", payload); handleFinishSendingPayload(payload, false, message, -1, null); // TODO: a better approach
PayloadSender sender = new PayloadSender(requestSender, new HttpRequestRetryPolicyDefault()); sender.setListener(new PayloadSender.Listener() { @Override public void onFinishSending(PayloadSender sender, PayloadData payload, boolean cancelled, String errorMessage, int responseCode, JSONObject responseData) { final MockPayload payload3 = new MockPayload("key3", "value3").setResponseCode(400); assertTrue(sender.sendPayload(payload1)); assertFalse(sender.sendPayload(payload2)); // would not start sending until the first one is complete assertFalse(sender.sendPayload(payload3)); // would not start sending until the first one is complete ); assertTrue(sender.sendPayload(payload2)); assertFalse(sender.sendPayload(payload3)); // would not start sending until the first one is complete ); assertTrue(sender.sendPayload(payload3));
if (payloadSender.isSendingPayload()) { ApptentiveLog.v(PAYLOADS, "Can't send the next payload: payload sender is busy"); return; boolean scheduled = payloadSender.sendPayload(payload);