我正在开发一个基于.NET4的应用程序,它必须请求第三方服务器才能从中获取信息.我正在使用HttpClient来发出这些HTTP请求.
我必须在短时间内创建一百或一千个请求.我想将这些请求的创建限制为一个限制(由常量或其他东西定义),以便其他服务器不会收到大量请求.
我已经检查了这个链接,它显示了如何减少随时创建的任务量.
这是我的非工作方法:
// create the factory
var factory = new TaskFactory(new LimitedConcurrencyLevelTaskScheduler(level));
// use the factory to create a new task that will create the request to the third-party server
var task = factory.StartNew(() => {
return new HttpClient().GetAsync(url);
}).Unwrap();
Run Code Online (Sandbox Code Playgroud)
当然,这里的问题是即使创建了当时的一个任务,也会同时创建和处理大量请求,因为它们在另一个调度程序中运行.我找不到将调度程序更改为HttpClient的方法.
我该如何处理这种情况?我想将创建的请求数量限制为某个限制,但不要阻止等待这些请求完成.
这可能吗?有任何想法吗?
我有以下2个groovy片段,应该做同样但但他们没有.
try {
throw new RuntimeException()
} catch (IllegalStateException) {
println("hello!")
}
Run Code Online (Sandbox Code Playgroud)
这个输出 'hello!'
try {
throw new RuntimeException()
} catch (IllegalStateException e) {
println("hello!")
}
Run Code Online (Sandbox Code Playgroud)
而这个输出是一个意外的例外:
Caught: java.lang.RuntimeException
java.lang.RuntimeException
at 2.run(2.groovy:2)
Run Code Online (Sandbox Code Playgroud)
请注意唯一的区别是,在一个片段e中,catch块中没有参数.
我正在运行以下版本的groovy和JVM.
groovy --version
Groovy Version: 2.0.5 JVM: 1.6.0_37 Vendor: Sun Microsystems Inc. OS: Linux
这是预期的行为还是编译器中的错误?谢谢