Android Open Source - Curio_android_SDK D B Request Processor






From Project

Back to project page Curio_android_SDK.

License

The source code is released under:

Apache License

If you think the Android project Curio_android_SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Turkcell/* w  ww .  j a v  a2  s.co  m*/
 * 
 * Created by Can Ciloglu on 10 Haz 2014
 *
 */
package com.turkcell.curio;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.turkcell.curio.model.OfflineRequest;
import com.turkcell.curio.utils.CurioDBHelper;
import com.turkcell.curio.utils.CurioLogger;

/**
 * Processor thread for all requests (periodic/offline/online).
 * Requests pushed to queues and then polled and processed from those queues.
 * 
 * @author Can Ciloglu
 *
 */
public class DBRequestProcessor implements Runnable {
  private static final String TAG = "CurioRequestProcessor";

  private static final BlockingQueue<OfflineRequest> offlineQueue = new LinkedBlockingQueue<OfflineRequest>();
  private static final BlockingQueue<OfflineRequest> periodicDispatchQueue = new LinkedBlockingQueue<OfflineRequest>();
  
  /**
   * Pushes request to offline cache DB queue.
   * 
   * @param offlineRequest
   */
  public static void pushToOfflineDBQueue(OfflineRequest offlineRequest) {
    offlineQueue.add(offlineRequest);
  }
  
  /**
   * Pushes request to periaodic dispatch DB  queue.
   * 
   * @param offlineRequest
   */
  public static void pushToPeriodicDispatchDBQueue(OfflineRequest offlineRequest) {
    periodicDispatchQueue.add(offlineRequest);
  }

  public void run() {
    try {
      while (true) {
        processOfflineQueue();
        processPeriodicDispatchQueue();
        Thread.sleep(250);
      }
    } catch (InterruptedException e) {
      CurioLogger.e(TAG, e.getMessage());
    }
  }

  /**
   * Stores offline request at DB.
   * 
   * @param offlineRequest
   */
  private void storeOfflineRequest(OfflineRequest offlineRequest) {
    /**
     * Before storing any offline requests, move all periodic dispatch requests to offline request table
     * to guarantee ordered dispatch of all requests. 
     */
    CurioDBHelper.getInstance().moveAllExistingPeriodicDispatchDataToOfflineTable();
    
    if (!CurioDBHelper.getInstance().persistOfflineRequestForCaching(offlineRequest)) {
      CurioLogger.e(TAG, "Could not persist offline request.");
    }
  }
  
  /**
   * Stores periodic dispatch request at DB.
   * 
   * @param offlineRequest
   */
  private void storePeriodicDispatchRequest(OfflineRequest offlineRequest) {
    if (!CurioDBHelper.getInstance().persistOfflineRequestForPeriodicDispatch(offlineRequest)) {
      CurioLogger.e(TAG, "Could not persist periodic dispatch request.");
    }
  }

  /**
   * Processes offline queue.
   */
  private void processOfflineQueue() {
    if (offlineQueue.size() > 0) {
      storeOfflineRequest(offlineQueue.poll());
    }
  }
  
  /**
   * Processes offline queue.
   */
  private void processPeriodicDispatchQueue() {
    if (periodicDispatchQueue.size() > 0) {
      storePeriodicDispatchRequest(periodicDispatchQueue.poll());
    }
  }
}




Java Source Code List

com.turkcell.curio.CurioClient.java
com.turkcell.curio.CurioRequestProcessor.java
com.turkcell.curio.DBRequestProcessor.java
com.turkcell.curio.ICurioResultListener.java
com.turkcell.curio.INetworkConnectivityChangeListener.java
com.turkcell.curio.model.OfflineRequest.java
com.turkcell.curio.model.OnlineRequest.java
com.turkcell.curio.model.Screen.java
com.turkcell.curio.utils.Constants.java
com.turkcell.curio.utils.CurioClientSettings.java
com.turkcell.curio.utils.CurioDBContract.java
com.turkcell.curio.utils.CurioDBHelper.java
com.turkcell.curio.utils.CurioLogger.java
com.turkcell.curio.utils.CurioUtil.java
com.turkcell.curio.utils.NetworkUtil.java
com.turkcell.curio.utils.ParameterLoader.java
com.turkcell.curio.utils.PushUtil.java
com.turkcell.curio.utils.UUIDGenerator.java
com.turkcell.curio.utils.VisitorCodeManager.java
com.turkcell.curiosample.BlankActivity.java
com.turkcell.curiosample.MainActivity.java
com.turkcell.curiosample.PushNotificationBroadcastReceiver.java
com.turkcell.curiosample.PushNotificationIntentService.java