Back to project page tapad-android-sdk.
The source code is released under:
MIT License
If you think the Android project tapad-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.
package com.tapad.tracking; /*from ww w .j av a 2s . co m*/ import com.tapad.util.Logging; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * A dispatcher that maintains the event queue and starts / stops * an executor service for doing the actual network IO on demand. * * This first version has no queue persistence and no network detection. * If the network is not available at the time events are submitted, they * will be lost. * */ class EventDispatcher { private EventResource resource; private ConcurrentLinkedQueue<Event> queue = new ConcurrentLinkedQueue<Event>(); private ExecutorService executor = null; EventDispatcher(EventResource resource) { this.resource = resource; } protected void dispatch(Event e) { queue.add(e); startExecutor(); } /** * Creates and starts a new executor if there is no existing one * or if the existing one is shut down. */ private synchronized void startExecutor() { if (executor == null || executor.isShutdown()) { executor = Executors.newSingleThreadExecutor(); executor.submit(new DispatchWorker()); } } private class DispatchWorker implements Runnable { private static final String TAG = "Tapad/DispatchWorker"; public void run() { Event event; while ((event = queue.poll()) != null) { try { Logging.info(TAG, "Posting event " + event); resource.post(event); } catch (Exception e) { Logging.warn(TAG, "Error posting event: " + e.getClass() + ", " + e.getMessage()); } } // We're done. Shut down the executor service for now. synchronized (EventDispatcher.this) { executor.shutdown(); } } } }