房价网Http-Client SDK使用说明
在项目中添加依赖
<dependency>
<groupId>com.fangjia</groupId>
<artifactId>fangjia-httpclient</artifactId>
<version>1.0.0</version>
</dependency>
同步调用
public static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(
20);
public static AtomicInteger ac =
new AtomicInteger();
/**
* 使用同步方式,使用者自己用线程池调度
* @author yinjihuan
* @date 2016年8月12日
* @throws InterruptedException
*/
private static void sync()
throws InterruptedException {
Long s = System.currentTimeMillis();
for (
int i =
0; i <
100; i++) {
fixedThreadPool.execute(
new Runnable() {
@Override
public void run() {
String content = FangJiaHttpUtil.get(
"http://fangjia.com");
if(content !=
null) {
ac.getAndIncrement();
}
}
});
}
fixedThreadPool.shutdown();
while (fixedThreadPool.isShutdown()) {
Thread.sleep(
1000);
System.out.println(ac.get());
Long e = System.currentTimeMillis();
System.out.println(e-s);
}
}
异步方式
public static AtomicInteger ac =
new AtomicInteger();
/**
* 异步调用方式,使用内置的线程池处理数据
* @author yinjihuan
* @date 2016年8月12日
* @throws InterruptedException
*/
private static void noSync()
throws InterruptedException {
Long s = System.currentTimeMillis();
for (
int i =
0; i <
4000; i++) {
FangJiaHttpUtil.get(urlList.get(RandomUtils.nextInt(urlList.size())),
new ResultCallBack() {
@Override
public void call(String result) {
if(result !=
null) {
ac.getAndIncrement();
}
}
});
}
while (
true) {
Thread.sleep(
1000);
System.out.println(ac.get());
Long e = System.currentTimeMillis();
System.out.println(e-s);
}
配置内容
Jvm参数添加:
-Dfangjia_artemis_ip=192.168.10.170 -Dfangjia_lcoal_ip=192.168.10.170
-Dfangjia_httpclient_poll_size=20
fangjia_artemis_ip:抓取系统IP, 可不配置,默认为192.168.0.63。 fangjia_lcoal_ip:必须配置,值为你使用的本机IP fangjia_httpclient_poll_size可不配置,使用异步方式会用内置的线程池进行调度,默认值为可用的处理器数量 * 5。
转载请注明原文地址: https://ju.6miu.com/read-1303595.html