使用fsockopen预编译关键jsp页面

发布于 2025-01-07 01:23:06 字数 1406 浏览 2 评论 0原文

因此,最初的问题是,我们在 WebSphere App Server 上运行“行业标准”的基于 Java 的 Web 应用程序,每年的访问量约为 1 亿次。问题是在这些应用程序服务器重新启动后,我们需要访问一些关键页面,以便在我们让公众访问它们之前编译主要的 servlet……否则它们往往会在最初的崩溃中崩溃。

在某些集群上,需要访问大约 6 个页面,每个 35 个以上市场一次......200 个左右的 url!

所以我正在编写的脚本已经完成了如何将所有这些 URL 放在一起的所有艰苦工作,最后是一个数组中 200 个 url 的列表......现在如何点击它们?

我们之前使用 CGI 来实现这一点,它的主要问题是它是同步的……花费了很长的时间。现在我正在尝试制作一个简单的 url.php ,它将命中一个 URL,然后我可以以异步方式从 JQuery 调用该 URL。当然,我不想一开始就达到全部 200 个,可能 5 个批次意味着速度提高 500% :)

所以进入 url.php 。我过去很少使用 php,所以套接字对我来说有点新。到目前为止,我拼凑起来的是:

function checkUrl($url,$port) {
set_time_limit(20); 
ob_start(); 
header("Content-Type: text/plain");
$u = $url; 
$p = $port;

$post = "HEAD / HTTP/1.1\r\n";
$post .= "Host: $u\r\n";
$post .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2\r\n";
$post .= "Keep-Alive: 200\r\n";
$post .= "Connection: keep-alive\r\n\r\n";

$sock = fsockopen($u, $p, $errno, $errstr, 10);
if (!$sock) {
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($sock, $post, strlen($post));
    while (!feof($sock)){
        echo fgets($sock);
    }
    ob_end_flush();
    }
}

如果 url 只是 someserver.somedomain.com,那么效果很好,但如果是在末尾点击的 Uri,则会失败(例如 someserver.somedomain.com/gb/en)

据我了解它,到目前为止我对代码所做的只是打开套接字连接......但是我怎样才能让它单独解析路径呢?

我最终需要的唯一输出是 HTTP 状态代码(200、404、301 等),但重要的是它首先获取完整页面以便正确编译。

So the original problem is that we run an "industry standard" java based web app application, on WebSphere App Servers with around 100 million visits per year. The issue is after a restart of these appservers, we need to hit a few of the key pages so that the main servlets get compiled before we let the public onto them ... otherwise they tend to crash in the initial crush.

On some clusters, its about 6 pages that need to be hit, once for each of 35+ markets.... 200 ish url's!

So the script I am working on has all the hard work done of how to put together all these URL's and at the end of it all is a list of 200 url's in an array... now how to hit them?

We were using CGI for this earlier and it's main problem was that is was synchronous... taking a loooooong time. Now I am trying to make a simple url.php which will hit one single URL which I can then call from JQuery in an asynchronous way. I don't want to hit all 200 at first of course, probably in batchs of 5 should mean a 500% speed increase :)

So onto the url.php . I haven't use php much in the past so sockets is a bit new to me. What I have cobbled together so far is this:

function checkUrl($url,$port) {
set_time_limit(20); 
ob_start(); 
header("Content-Type: text/plain");
$u = $url; 
$p = $port;

$post = "HEAD / HTTP/1.1\r\n";
$post .= "Host: $u\r\n";
$post .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2\r\n";
$post .= "Keep-Alive: 200\r\n";
$post .= "Connection: keep-alive\r\n\r\n";

$sock = fsockopen($u, $p, $errno, $errstr, 10);
if (!$sock) {
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($sock, $post, strlen($post));
    while (!feof($sock)){
        echo fgets($sock);
    }
    ob_end_flush();
    }
}

Which works great if the url is simply someserver.somedomain.com but if the is a Uri tapped on the end it fails (e.g. someserver.somedomain.com/gb/en)

As I understand it, all I have done with the code so far is open the socket connection ... but how can I get it to parse the path separately?

The only output I need from this in the end is the HTTP Status code (200, 404, 301 etc) though it is important that it does fetch the complete page first in order for it to be compiled properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱冒险 2025-01-14 01:23:06

也许我遗漏了一些东西,但是你有 curl 扩展 可用吗?无需混合使用 jQuery,您可以直接从 PHP 轻松运行异步查询。您还可以轻松控制批量大小,并根据您的需要添加延迟和其他内容。另外,我不确定为什么您需要使用原始套接字来访问 JSP 页面,希望这能让您的生活更轻松!

这是我的快速测试脚本,基于 php.net 的代码,我确信:

<?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();

// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://news.php.net/php.general/255000");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://news.php.net/php.general/255001");
curl_setopt($ch2, CURLOPT_HEADER, 0);

//create the multiple cURL handle
$mh = curl_multi_init();

//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$active = null;
//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>

Maybe I'm missing something but do you have the curl extension available? No need to get jQuery in the mix, you can run asynchronous queries straight from PHP with ease. You'll also be able to control batch size easily, and put in delays and what-not per your needs. Also I'm not sure why you would need to use a raw socket to hit the JSP pages, hopefully this makes your life easier!

Here's a quick test script I have, based on code from php.net I'm sure:

<?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();

// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://news.php.net/php.general/255000");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://news.php.net/php.general/255001");
curl_setopt($ch2, CURLOPT_HEADER, 0);

//create the multiple cURL handle
$mh = curl_multi_init();

//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$active = null;
//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文