Volley.java中的newRequestQueue方法中有三个非常重要的类,今天先讲解RequestQueue类,它是框架的核心。
- RequestQueue
这个类在请求队列中默认使用的构造方法为:
public RequestQueue(Cache cache, Network network) {
this(cache, network, DEFAULT_NETWORK_THREAD_POOL_SIZE);
}
Cache对象是new DiskBasedCache(cacheDir),network是Network network = new BasicNetwork(stack);DEFAULT_NETWORK_THREAD_POOL_SIZE默认值为4,也就是网络任务分配的最大线程数是4,对于一般的app来说足以。初始化完毕以后运行start()方法。
/**
* Starts the dispatchers in this queue.
*/
public void start() {
stop(); // Make sure any currently running dispatchers are stopped.
// Create the cache dispatcher and start it.
mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
mCacheDispatcher.start();
// Create network dispatchers (and corresponding threads) up to the pool size.
for (int i = 0; i < mDispatchers.length; i++) {
NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork,
mCache, mDelivery);
mDispatchers[i] = networkDispatcher;
networkDispatcher.start();
}
}
首先stop,里边是停止缓存的dispatcher,然后遍历4个网络线程的dispatcher,并且停止他们。stop完毕,新建CacheDispatcher对象,传入4个参数,mCacheQueue,mNetworkQueue是声明的PriorityBlockingQueue对象,mCache是实现了Cache接口的对象,mDelivery是获取内容的反馈对象。接着根据dispatcher的数量,初始化网络工作线程对象。每个线程管理对象都传入mNetworkQueue, mNetwork,mCache, mDelivery四个对象。这两天有两个重要的对象,分别是CacheDispatcher和NetworkDispatcher对象。
- CacheDispatcher
这个对象的run方法代码如下
@Override
public void run() {
if (DEBUG) VolleyLog.v("start new dispatcher");
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
// Make a blocking call to initialize the cache.
mCache.initialize();
首先设置线程的优先级,然后初始化cache对象
@Override
public synchronized void initialize() {
if (!mRootDirectory.exists()) {
if (!mRootDirectory.mkdirs()) {
VolleyLog.e("Unable to create cache dir %s", mRootDirectory.getAbsolutePath());
}
return;
}
File[] files = mRootDirectory.listFiles();
if (files == null) {
return;
}
for (File file : files) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
CacheHeader entry = CacheHeader.readHeader(fis);
entry.size = file.length();
putEntry(entry.key, entry);
} catch (IOException e) {
if (file != null) {
file.delete();
}
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ignored) { }
}
}
}
- 如果不存在缓存目录就是创建目录,并返回。
- 如果存在缓存目录,列出所有文件,如果文件为空,则返回。
- 遍历所有的缓存文件,读取文件头信息,这个时候实体信息没有设置到entry中
while (true) {
try {
// Get a request from the cache triage queue, blocking until
// at least one is available.
final Request<?> request = mCacheQueue.take();
request.addMarker("cache-queue-take");
// If the request has been canceled, don't bother dispatching it.
if (request.isCanceled()) {
request.finish("cache-discard-canceled");
continue;
}
// Attempt to retrieve this item from cache.
Cache.Entry entry = mCache.get(request.getCacheKey());
if (entry == null) {
request.addMarker("cache-miss");
// Cache miss; send off to the network dispatcher.
mNetworkQueue.put(request);
continue;
}
// If it is completely expired, just send it to the network.
if (entry.isExpired()) {
request.addMarker("cache-hit-expired");
request.setCacheEntry(entry);
mNetworkQueue.put(request);
continue;
}
从缓存队列中取出请求对象,如果请求已经取消,直接跳出本次循环。否则,根据键值,获取entry实体对象。如果没有缓存这个对象就会交给网络任务队列处理。实体对象如果过期了,把实体对象缓存,同时把请求加入请求队列中。
request.addMarker("cache-hit");
Response<?> response = request.parseNetworkResponse(
new NetworkResponse(entry.data, entry.responseHeaders));
request.addMarker("cache-hit-parsed");
if (!entry.refreshNeeded()) {
// Completely unexpired cache hit. Just deliver the response.
mDelivery.postResponse(request, response);
} else {
// Soft-expired cache hit. We can deliver the cached response,
// but we need to also send the request to the network for
// refreshing.
request.addMarker("cache-hit-refresh-needed");
request.setCacheEntry(entry);
// Mark the response as intermediate.
response.intermediate = true;
// Post the intermediate response back to the user and have
// the delivery then forward the request along to the network.
mDelivery.postResponse(request, response, new Runnable() {
@Override
public void run() {
try {
mNetworkQueue.put(request);
} catch (InterruptedException e) {
// Not much we can do about this.
}
}
});
}
如果实体对象不需要刷新,则把请求中缓存的应用结果返回。否则把返回设置中间返回值(稍后会有新的返回结果),然后把请求放入网络请求队列。