forked from ntoll/RESTProvider
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moving from intent service to Executor service with lifecycle as per …
…AndroidPerfBenchmark on github
- Loading branch information
Showing
11 changed files
with
374 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
RESTProvider/src/novoda/rest/concurrent/HttpRequestCallable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package novoda.rest.concurrent; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.methods.HttpUriRequest; | ||
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; | ||
|
||
public class HttpRequestCallable implements Callable<HttpResponse> { | ||
|
||
private HttpClient client; | ||
|
||
private HttpUriRequest request; | ||
|
||
public HttpRequestCallable(HttpClient client, HttpUriRequest request) { | ||
if (client.getConnectionManager() instanceof ThreadSafeClientConnManager) { | ||
this.client = client; | ||
this.request = request; | ||
} else { | ||
throw new RuntimeException("Can not use non threaded http client"); | ||
} | ||
} | ||
|
||
@Override | ||
public HttpResponse call() throws Exception { | ||
return client.execute(request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
|
||
package novoda.rest.database; | ||
|
||
import android.net.Uri; | ||
|
||
public interface Persister { | ||
public void setPersister(ModularSQLiteOpenHelper sqlite); | ||
|
||
public void setPersister(ModularSQLiteOpenHelper sqlite); | ||
|
||
//public Uri getContentProviderUri(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
RESTProvider/src/novoda/rest/services/HttpExecutorService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package novoda.rest.services; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import android.app.Service; | ||
import android.content.Intent; | ||
import android.os.Handler; | ||
import android.os.IBinder; | ||
import android.os.Message; | ||
|
||
public class HttpExecutorService extends Service { | ||
|
||
private static final int CORE_POOL_SIZE = 5; | ||
private static final int MAXIMUM_POOL_SIZE = 128; | ||
private static final int KEEP_ALIVE = 10; | ||
|
||
private static final ThreadFactory sThreadFactory = new ThreadFactory() { | ||
private final AtomicInteger mCount = new AtomicInteger(1); | ||
|
||
public Thread newThread(Runnable r) { | ||
return new Thread(r, "HttpExecutorService #" | ||
+ mCount.getAndIncrement()); | ||
} | ||
}; | ||
|
||
private static final BlockingQueue<Runnable> sWorkQueue = new LinkedBlockingQueue<Runnable>( | ||
10); | ||
|
||
private static final ThreadPoolExecutor sExecutor = new ThreadPoolExecutor( | ||
CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, | ||
sWorkQueue, sThreadFactory); | ||
|
||
private static final int MESSAGE_RECEIVED_REQUEST = 0x1; | ||
private static final int MESSAGE_TIMEOUT_AFTER_FIRST_CALL = 0x2; | ||
|
||
// 5 Minutes | ||
public static final long SERVICE_LIFESPAN = 1000 * 60 * 5; | ||
|
||
private class LifecycleHandler extends Handler { | ||
|
||
private long lastCall = 0L; | ||
|
||
@Override | ||
public void handleMessage(Message msg) { | ||
switch (msg.what) { | ||
case MESSAGE_RECEIVED_REQUEST: | ||
lastCall = System.currentTimeMillis(); | ||
msg.what = MESSAGE_TIMEOUT_AFTER_FIRST_CALL; | ||
sendMessageDelayed(msg, SERVICE_LIFESPAN); | ||
break; | ||
case MESSAGE_TIMEOUT_AFTER_FIRST_CALL: | ||
// If we have not received another call in the last 5 minutes | ||
if (System.currentTimeMillis() - lastCall > SERVICE_LIFESPAN) { | ||
stopSelf(msg.arg1); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public int onStartCommand(Intent intent, int flags, int startId) { | ||
return super.onStartCommand(intent, flags, startId); | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent intent) { | ||
return null; | ||
} | ||
} |
Oops, something went wrong.