Storm【实践系列-如何写一个爬虫- 对于Protocol进行的封装】

2019-03-02 23:57|来源: 网路


本章描述:对于Protocol的封装

package com.digitalpebble.storm.crawler.fetcher;

import com.digitalpebble.storm.crawler.util.Configuration;

public interface Protocol {

    public ProtocolResponse getProtocolOutput(String url) throws Exception;
    
    public void configure(Configuration conf);
}


 对于ProtoclFactory的封装

package com.digitalpebble.storm.crawler.fetcher;

import java.net.URL;
import java.util.WeakHashMap;

import com.digitalpebble.storm.crawler.fetcher.asynchttpclient.AHProtocol;
import com.digitalpebble.storm.crawler.util.Configuration;

/**
 * @author Yin Shuai
 *
 */
public class ProtocolFactory {

    private final Configuration config;

    private final WeakHashMap<String, Protocol> cache = new WeakHashMap<String, Protocol>();

	public ProtocolFactory(Configuration conf) {
        config = conf;
    }

    /** Returns an instance of the protocol to use for a given URL **/
    public synchronized Protocol getProtocol(URL url) {
        // get the protocol
        String protocol = url.getProtocol();
        Protocol pp = cache.get(protocol);
        if (pp != null)
            return pp;
        
        // yuk! hardcoded for now
        pp = new AHProtocol();
        pp.configure(config);
        cache.put(protocol,pp);
        return pp;
    }

}


 对于ProtocolResponse的封装

package com.digitalpebble.storm.crawler.fetcher;

import java.util.HashMap;

public class ProtocolResponse {

    final byte[] content;
    final int statusCode;
    final HashMap<String, String[]> metadata;

    public ProtocolResponse(byte[] c, int s, HashMap<String, String[]> md){
        content = c;
        statusCode = s;
        metadata = md;
    }

    public byte[] getContent() {
        return content;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public HashMap<String, String[]> getMetadata() {
        return metadata;
    }

}



转自:http://my.oschina.net/u/1791874/blog/305263

相关问答

更多
  • 如何写培训简报[2024-01-10]

    很简单啊,先下个简报样板,然后填内容,内容一般分成:前言,存在问题(即培训必要性和出发点),具体措施,取得成效,结束语。你要是需要的话,留个邮箱,我发个我写的给你看看,在这发出来不太好。
  • 如果属性不是静态的,并且3个网页分别用3个不同的对象不会有影响。做爬虫肯定都是多线程的。 在同一个线程实例化3次等于三个实例依次运行效率太低了肯定不成,建议您还是用多线程来处理。
  • 没有真正发现问题是什么,所以我尝试了另一个解决方案,这对我有用(我的主要目的是将数据保存到app的本地内存中)。 我使用了npm包electron-store ,这真的很容易使用。 您可以通过在终端输入此内容来获取它 npm install electron-store 有关它的更多信息: Electron store 希望它也可以帮助别人:-) Didn't really found out what the problem was, so I tried another solution, which ...
  • 这些例子非常不同......第一个创建了一个“MyObject” function ,当使用new作为构造function调用时,它将具有“getSecret” function作为属性; 第二个创建一个带有“getSecret” function作为属性的“MyObject” Object 。 在这方面,这有点像静态方法和公共方法之间的区别。 在第一种情况下,该方法仅在调用构造函数时才存在,而不在构造函数本身中。 在第二种情况下,没有构造函数。 所以,假设你有: var MyObject1 = funct ...
  • 客户有一个公开其历史订单的房产:只需添加一个吸气剂即可公开订单清单。 public List Orders {get;} = new List(); 尝试添加空订单应该什么都不做: 我认为这里所需的功能是忽略空对象 public void AddOrder(Order o){ if (o == null){ return; } //rest of your implememntaton } 您正在使用名称和日期添加订单,而不是尝试将Order对象传递给函数,为简单起见,假 ...
  • 嗯,肯定有很多方法。 标准的是将函数存储在字典中。 在函数式语言中,你会写出类似的东西 import MyProtocol handler = { mListFirmware : listFirmwareVersions, mLoadFirmware : startLoadingFirmwareVersion, mLoadFirmwareBl: loadFirmwareVersionBlock, ...
  • 您可以使用以下方法定义Protocol构造函数的主体: 在类定义中(隐式内联) // protocol.h class Protocol { public: Protocol() { // in the class definition } ... }; 明确的内联 // protocol.h class Protocol { public: Protocol(); ... }; // inline Protocol ...
  • 通过游乐场检查它可以毫无问题使用纯Swift,所以如果它不适合你,那么可能与@objc桥接有关,请看下面的图像为工作操场。 Checking through a playground it works without issue using pure Swift so if it is not working for you then maybe something to do with the @objc bridging see the image below for working playgroun ...