Skip to content
Browse files

Moved short-lived tasks to dasein-util task pool

  • Loading branch information...
1 parent 5721a2a commit 3a05d157f8221c7e0645db6ea214d05f8ed86012 @timf committed Feb 13, 2013
View
2 pom.xml
@@ -3,7 +3,7 @@
<groupId>org.dasein</groupId>
<artifactId>dasein-persist</artifactId>
- <version>2013.01-SNAPSHOT</version>
+ <version>2013.02-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dasein-persist</name>
View
21 src/main/java/org/dasein/persist/Execution.java
@@ -43,6 +43,7 @@
import org.dasein.persist.dao.LoadTranslator;
import org.dasein.persist.dao.RemoveTranslator;
import org.dasein.persist.dao.SaveTranslator;
+import org.dasein.util.DaseinUtilTasks;
import org.dasein.util.Translator;
/**
@@ -106,18 +107,16 @@
catch( Exception e ) {
logger.error("Problem reading " + PROPERTIES + ": " + e.getMessage(), e);
}
- Thread stackPusher = new Thread() {
- public void run() {
- pushExecutions();
- }
- };
-
- stackPusher.setPriority(Thread.NORM_PRIORITY-1);
- stackPusher.setDaemon(true);
- stackPusher.setName("dasein-persistence - STACK PUSHER");
- stackPusher.start();
+ DaseinUtilTasks.submit(new StackPusher());
}
-
+
+ private static class StackPusher implements Runnable {
+ @Override
+ public void run() {
+ pushExecutions();
+ }
+ }
+
static public String getDataSourceName(String cname) {
return getDataSourceName(cname, false);
}
View
140 src/main/java/org/dasein/persist/PersistentFactory.java
@@ -56,6 +56,7 @@
import org.dasein.util.CacheLoader;
import org.dasein.util.CacheManagementException;
import org.dasein.util.ConcurrentMultiCache;
+import org.dasein.util.DaseinUtilTasks;
import org.dasein.util.JitCollection;
import org.dasein.util.Jiterator;
import org.dasein.util.JiteratorFilter;
@@ -1683,6 +1684,62 @@ private String getSqlName(String nom) {
}
return params;
}
+
+ private class PersistentFactoryTask implements Runnable {
+ private final Jiterator<T> it;
+ private final Map<String,Object> results;
+
+ private PersistentFactoryTask(Jiterator<T> it, Map<String,Object> results) {
+ this.it = it;
+ this.results = results;
+ }
+
+ @Override
+ public void run() {
+ try {
+ for( Map<String,Object> map: (Collection<Map<String,Object>>)this.results.get(LISTING) ) {
+ T item = null;
+
+ if( singletons.size() > 0 ) {
+ for( String key : singletons.keySet() ) {
+ Object ob = map.get(key);
+
+ if( ob instanceof java.math.BigDecimal ) {
+ java.math.BigDecimal tmp = (java.math.BigDecimal)ob;
+
+ ob = tmp.longValue();
+ map.put(key, ob);
+ }
+ item = cache.find(key, ob);
+ if( item != null ) {
+ break;
+ }
+ }
+ }
+ if( item == null ) {
+ if( dependency != null ) {
+ try {
+ dependency.loadDependencies(map);
+ }
+ catch( PersistenceException e ) {
+ it.setLoadException(e);
+ return;
+ }
+ }
+ item = cache.find(map);
+ }
+ this.it.push(item);
+ }
+ this.it.complete();
+ }
+ catch( Exception e ) {
+ this.it.setLoadException(e);
+ }
+ catch( Throwable t ) {
+ this.it.setLoadException(new RuntimeException(t));
+ }
+ }
+ }
@SuppressWarnings("unchecked")
private Collection<T> load(Class<? extends Execution> cls, JiteratorFilter<T> filter, SearchTerm ... usingTerms) throws PersistenceException {
@@ -1698,88 +1755,7 @@ private String getSqlName(String nom) {
results = xaction.execute(cls, params);
xaction.commit();
- Thread t = new Thread() {
- public void run() {
- try {
- for( Map<String,Object> map: (Collection<Map<String,Object>>)results.get(LISTING) ) {
- T item = null;
-
- if( singletons.size() > 0 ) {
- for( String key : singletons.keySet() ) {
- Object ob = map.get(key);
-
- if( ob instanceof java.math.BigDecimal ) {
- java.math.BigDecimal tmp = (java.math.BigDecimal)ob;
-
- ob = tmp.longValue();
- map.put(key, ob);
- }
- item = cache.find(key, ob);
- if( item != null ) {
- break;
- }
- }
- }
- if( item == null ) {
- if( dependency != null ) {
- try {
- dependency.loadDependencies(map);
- }
- catch( PersistenceException e ) {
- it.setLoadException(e);
- return;
- }
- }
- item = cache.find(map);
- }
- /*
- if( dependency != null ) {
- if( singletons.size() > 0 ) {
- for( String key : singletons.keySet() ) {
- Object ob = map.get(key);
-
- if( ob.getClass().getName().equals(java.math.BigDecimal.class.getName()) ) {
- java.math.BigDecimal tmp = (java.math.BigDecimal)ob;
-
- ob = tmp.longValue();
- map.put(key, ob);
- }
- item = cache.find(key, ob);
- if( item != null ) {
- break;
- }
- }
- }
- if( item == null ) {
- try {
- dependency.loadDependencies(map);
- }
- catch( PersistenceException e ) {
- it.setLoadException(e);
- return;
- }
- }
- }
- if( item == null ) {
- item = cache.find(map);
- }
- */
- it.push(item);
- }
- it.complete();
- }
- catch( Exception e ) {
- it.setLoadException(e);
- }
- catch( Throwable t ) {
- it.setLoadException(new RuntimeException(t));
- }
- }
- };
-
- t.setDaemon(true);
- t.setName("Loader");
- t.start();
+ DaseinUtilTasks.submit(new PersistentFactoryTask(it, results));
return new JitCollection<T>(it, cache.getTarget().getName());
}
catch( PersistenceException e ) {
View
65 src/main/java/org/dasein/persist/RelationalCache.java
@@ -24,7 +24,6 @@
import java.util.Map;
import org.apache.log4j.Logger;
-import org.dasein.util.Jiterator;
import org.dasein.persist.jdbc.Counter;
import org.dasein.persist.jdbc.Creator;
import org.dasein.persist.jdbc.Deleter;
@@ -35,7 +34,9 @@
import org.dasein.util.CacheLoader;
import org.dasein.util.CachedItem;
import org.dasein.util.CacheManagementException;
+import org.dasein.util.DaseinUtilTasks;
import org.dasein.util.JitCollection;
+import org.dasein.util.Jiterator;
import org.dasein.util.JiteratorFilter;
public final class RelationalCache<T extends CachedItem> extends PersistentCache<T> {
@@ -445,6 +446,39 @@ protected String getSqlNameForClassName(String cname) {
}
return params;
}
+
+ private class RelationalCacheTask implements Runnable {
+ private final Jiterator<T> it;
+ private final Map<String,Object> results;
+
+ private RelationalCacheTask(Jiterator<T> it, Map<String,Object> results) {
+ this.it = it;
+ this.results = results;
+ }
+
+ @Override
+ public void run() {
+ try {
+ for( Map<String,Object> map: (Collection<Map<String,Object>>)this.results.get(Loader.LISTING) ) {
+ for( String fieldName : map.keySet() ) {
+ LookupDelegate delegate = getLookupDelegate(fieldName);
+
+ if( delegate != null && !delegate.validate((String)map.get(fieldName)) ) {
+ throw new PersistenceException("Unable to validate " + fieldName + " value of " + map.get(fieldName));
+ }
+ }
+ this.it.push(getCache().find(map));
+ }
+ this.it.complete();
+ }
+ catch( Exception e ) {
+ this.it.setLoadException(e);
+ }
+ catch( Throwable t ) {
+ this.it.setLoadException(new RuntimeException(t));
+ }
+ }
+ }
@SuppressWarnings("unchecked")
private Collection<T> load(Loader loader, JiteratorFilter<T> filter, Map<String,Object> params) throws PersistenceException {
@@ -459,33 +493,8 @@ protected String getSqlNameForClassName(String cname) {
results = xaction.execute(loader, params, readDataSource);
xaction.commit();
- Thread t = new Thread() {
- public void run() {
- try {
- for( Map<String,Object> map: (Collection<Map<String,Object>>)results.get(Loader.LISTING) ) {
- for( String fieldName : map.keySet() ) {
- LookupDelegate delegate = getLookupDelegate(fieldName);
-
- if( delegate != null && !delegate.validate((String)map.get(fieldName)) ) {
- throw new PersistenceException("Unable to validate " + fieldName + " value of " + map.get(fieldName));
- }
- }
- it.push(getCache().find(map));
- }
- it.complete();
- }
- catch( Exception e ) {
- it.setLoadException(e);
- }
- catch( Throwable t ) {
- it.setLoadException(new RuntimeException(t));
- }
- }
- };
-
- t.setDaemon(true);
- t.setName("Loader");
- t.start();
+
+ DaseinUtilTasks.submit(new RelationalCacheTask(it, results));
return new JitCollection<T>(it, getEntityClassName());
}
catch( PersistenceException e ) {
View
3 src/main/java/org/dasein/persist/Transaction.java
@@ -220,7 +220,7 @@ static public void report() {
private int transactionId;
private StackTraceElement[] stackTrace;
-
+
/**
* Constructs a transaction object having the specified transaction ID.
* @param xid the transaction ID that identifies the transaction
@@ -555,6 +555,7 @@ private synchronized void open(Execution event, String dsn) throws SQLException,
ds = (DataSource)ctx.lookup(dsn);
conn = ds.getConnection();
openTime = System.currentTimeMillis();
+ logger.warn("DPTRANSID-" + transactionId + " connection.get - dsn='" + dsn + '\'');
if( logger.isDebugEnabled() ) {
logger.debug("DPTRANSID-" + transactionId + " connection.get - dsn='" + dsn + '\'');
}

0 comments on commit 3a05d15

Please sign in to comment.
Something went wrong with that request. Please try again.