我有几十万个 URL 需要调用。这些是对应用程序服务器的调用,应用程序服务器将处理它们并将状态代码写入表中。我不需要等待响应(成功/失败),只需等待服务器收到请求。我还希望能够指定一次可以运行多少个并发作业,因为我还没有计算出 tomcat 可以处理多少个并发请求。
这是我到目前为止所得到的,基本上取自其他人尝试做类似的事情,只是不使用 url 调用。该文本文件中的每个 url 各占一行。网址如下所示:
http://webserver:8080/app/mwo/services/create?server=ServerName&e1user=admin&newMWONum=123456&sourceMWONum=0&tagNum=33-A-1B
和代码:
$maxConcurrentJobs = 10
$content = Get-Content -Path "C:\Temp\urls.txt"
foreach ($url in $content) {
$running = @(Get-Job | Where-Object { $_.State -eq 'Running' })
if ($running.Count -le $maxConcurrentJobs) {
Start-Job {
Invoke-WebRequest -UseBasicParsing -Uri $using:url
}
} else {
$running | Wait-Job -Any
}
Get-Job | Receive-Job
}
我遇到的问题是每个“作业”都会出现 2 个错误,我不知道为什么。当我转储 url 数组 $content 时,它看起来很好,当我一一运行 Invoke-WebRequest 时,它们工作正常,没有错误。
126 Job126 BackgroundJob Running True localhost ...
Invalid URI: The hostname could not be parsed.
+ CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], UriFormatException
+ FullyQualifiedErrorId : System.UriFormatException,Microsoft.PowerShell.Commands.InvokeRestMethodComman
d
+ PSComputerName : localhost
Invalid URI: The hostname could not be parsed.
+ CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], UriFormatException
+ FullyQualifiedErrorId : System.UriFormatException,Microsoft.PowerShell.Commands.InvokeRestMethodComman
d
+ PSComputerName : localhost
任何帮助或替代实现将不胜感激。我愿意不使用 powershell,但我仅限于 Windows 7 桌面或 Windows 2008 R2 服务器,并且我可能会使用 url 中的 localhost 在服务器本身上运行最终脚本,以减少网络延迟。