Skip to content

Commit

Permalink
moving from intent service to Executor service with lifecycle as per …
Browse files Browse the repository at this point in the history
…AndroidPerfBenchmark on github
  • Loading branch information
charroch committed Oct 19, 2010
1 parent 3a3d669 commit fa6c299
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 209 deletions.
11 changes: 8 additions & 3 deletions RESTProvider/src/novoda/rest/command/PersistableCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package novoda.rest.command;

import android.content.ContentProviderClient;
import android.util.Log;
import novoda.rest.database.ModularSQLiteOpenHelper;
import novoda.rest.database.Persister;
import novoda.rest.parsers.Node;
Expand All @@ -22,10 +24,13 @@ public void setData(Node<?> data) {

@Override
public void execute() {
// TODO locking?
traverse(data, sqlite);
try {
traverse(data, sqlite);
} finally {
// sqlite.close();
}
}

protected abstract void traverse(Node<?> response,
ModularSQLiteOpenHelper dbHelper);
ModularSQLiteOpenHelper client);
}
29 changes: 29 additions & 0 deletions RESTProvider/src/novoda/rest/concurrent/HttpRequestCallable.java
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@ public ModularSQLiteOpenHelper(Context context) {
super(context, new StringBuilder(
context.getApplicationInfo().packageName).append(".db")
.toString(), null, dbVersion);
final Cursor cur = getReadableDatabase().rawQuery(SELECT_TABLES_NAME,
null);
while (cur.moveToNext()) {
createdTable.add(cur.getString(0));
}
cur.close();

// final Cursor cur = getReadableDatabase().rawQuery(SELECT_TABLES_NAME,
// null);
// while (cur.moveToNext()) {
// createdTable.add(cur.getString(0));
// }
// cur.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
Log.v(TAG, "creating database");
db.execSQL(CREATE_TABLE_STATUS);
//db.execSQL(CREATE_TABLE_STATUS);
}

@Override
Expand Down Expand Up @@ -122,7 +123,7 @@ public synchronized Map<String, SQLiteType> getColumnsForTable(
* @return true if the table has been created. false otherwise
*/
public synchronized boolean isTableCreated(final String tableName) {
return createStatements.containsKey(tableName);
return createdTable.contains(tableName);
}

}
7 changes: 6 additions & 1 deletion RESTProvider/src/novoda/rest/database/Persister.java
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();
}
13 changes: 4 additions & 9 deletions RESTProvider/src/novoda/rest/database/etag/ETagSQLiteHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
Expand Down Expand Up @@ -99,6 +100,7 @@ public synchronized ETag getETag(String uri) {
etag.lastModified = cur.getString(1);
}
if (cur != null) {
getReadableDatabase().close();
cur.close();
}
return etag;
Expand All @@ -112,14 +114,7 @@ public synchronized boolean hasEtag(String uri) {
getWritableDatabase().delete(TABLE_NAME, null, null);
}

/* package */int getCount() {
Cursor cur = getReadableDatabase().rawQuery(
"SELECT COUNT(*) FROM " + TABLE_NAME + ";", null);
cur.moveToFirst();
try {
return cur.getInt(0);
} finally {
cur.close();
}
private synchronized long getCount() {
return DatabaseUtils.queryNumEntries(getReadableDatabase(), TABLE_NAME);
}
}
8 changes: 5 additions & 3 deletions RESTProvider/src/novoda/rest/net/AndroidHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
Expand All @@ -46,7 +47,6 @@

import android.content.ContentResolver;
import android.content.Context;
import android.net.SSLCertificateSocketFactory;
import android.net.SSLSessionCache;
import android.os.Looper;
import android.util.Log;
Expand Down Expand Up @@ -119,8 +119,10 @@ public static AndroidHttpClient newInstance(String userAgent,
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLCertificateSocketFactory
.getHttpSocketFactory(30 * 1000, sessionCache), 443));

// Changed from android.net to Apache to fit versions prior to 2.2
schemeRegistry.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443));

ClientConnectionManager manager = new ThreadSafeClientConnManager(
params, schemeRegistry);
Expand Down
1 change: 0 additions & 1 deletion RESTProvider/src/novoda/rest/providers/RESTProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import novoda.rest.clag.provider.ClagProvider;
import novoda.rest.configuration.ProviderMetaData;
import novoda.rest.configuration.SQLiteMetaData;
import novoda.rest.database.ModularSQLiteOpenHelper;
import novoda.rest.database.SQLiteTableCreator;
import novoda.rest.intents.HttpServiceIntent;
Expand Down
75 changes: 75 additions & 0 deletions RESTProvider/src/novoda/rest/services/HttpExecutorService.java
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;
}
}
Loading

0 comments on commit fa6c299

Please sign in to comment.