Task Queue


Source link: https://github.com/NeoLSN/TaskQueue

Task Queue

Description

An Android task queue library. Support priority, timeout, multiple execution, exclusive task and retry.

Setup

repositories {

  ...
  maven {
 url "https://jitpack.io" 
}
 
}
 dependencies {

  ...
  compile 'com.github.NeoLSN:TaskQueue:1.0.2' 
}

Usage

Step 1

Establish a task pool for app.

public class TaskQueueApplication extends Application {

private TaskPool taskPool;

@Override public void onCreate() {

super.onCreate();

...

taskPool = new TaskPool(new ExecutorDelivery(new Handler(Looper.getMainLooper())));

taskPool.start();

  
}

public TaskPool getTaskPool() {

return taskPool;
  
}
 
}
Step 2

Add task to pool

TaskQueueApplication app = (TaskQueueApplication) getApplication();
 TaskPool taskPool = app.resetTaskPool();
 Task<?, ?> task = ... //create you own task taskPool.add(task);

API

Task

public abstract class Task<I, O> {

public void cancel();

// task status
  public boolean isPending();

  public boolean isExecuting();

  public boolean isDone();

  public boolean isCanceled();

  public boolean isTimeout();

  public State getState();

public Object getTag();

  public void setTag(Object tag);

public long getTimeout();

  public void setTimeout(long timeout, TimeUnit unit);

  public void resetTimeout();

public Priority getPriority();

  // Set priority before it is add to pool.
  public void setPriority(Priority priority);

  public int getSequence();

public RetryPolicy getRetryPolicy();

  public void setRetryPolicy(RetryPolicy retryPolicy);

// Only one task be executed at one time in the pool if the tasks' key are the same.
  public abstract String getExclusiveKey();

  public abstract void setData(I data);

  public abstract I getData();

  protected abstract void deliverResult(O result);

  protected abstract O onExecute() throws Exception;
  protected void onFinish();
 
}

TaskPool

public class TaskPool {

// Default using - Executors.newCachedThreadPool()
  protected ExecutorService createExecutor();

  public void start();

  public void stop();

// Return null when pool is stopped.
  public <I, O> Task<I, O> add(Task<I, O> task);

  public int size();

public List<Task<?, ?>> getTasks(RequestFilter filter);

  public List<Task<?, ?>> getTasks();

  public List<Task<?, ?>> getTasksByTag(final Object tag);

  public List<Task<?, ?>> getTasksByExclusiveKey(final String key);

public void cancel(RequestFilter filter);

  public void cancelAll();

  public void cancelByTag(final Object tag);

  public void cancelByExclusiveKey(final String key);

public interface RequestFilter {

boolean apply(Task<?, ?> task);

  
}

public <I, O> void addTaskListener(TaskPoolListener<I, O> listener);

  public <I, O> void removeTaskListener(TaskPoolListener<I, O> listener);

public interface TaskPoolListener<I, O> {

void onAdd(Task<I, O> task, int size);

void onRemove(Task<I, O> task, int size);

  
}
 
}

ResultDelivery

For task pool to deliver the result to specified thread

public interface ResultDelivery {

public void postResult(Task<?, ?> task, Result<?> result);

  public void postResult(Task<?, ?> task, Result<?> result, Runnable runnable);

  public void postError(Task<?, ?> task, Throwable error);
 
}

RetryPolicy

Do nothing if it needs to retry. Throw an error if it can't retry.

public interface RetryPolicy {

public void retryOrNot(Throwable error) throws Throwable; 
}

Resources

An Android library to make thumbs +1 effect. It supports text and images.

A pretty Music Player demo created by Anish Chandran and coded by André Mion

VirtualApp is an open platform for Android that allows you to create a Virtual Space, you can install and run apk inside. Beyond that, VirtualApp is also a Plugin Framework.

Cosmos Browser allows the user to connect to the internet through the use of SMS. No data or WiFi required.

Android Markdown editor.

This project allows to dump files from oat dex tool.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes