有没有更有效的方法来编写这个Jsoup代码

时间:2016-04-24 06:42:09

标签: jsp jsoup

我在jsp中建立一个网站来比较不同购物网站的产品价格,我用以下方式在1个jsp页面上编写代码:

try
{
 jsoup.connect(first_website)
 /* code for fetching prices and images */
 /* Html code for displaying prices and images */     
} catch(){}

try
{
 jsoup.connect(second_website)
 /* code for fetching prices and images */
 /* Html code for displaying prices and images */
} catch(){}

try
{
 jsoup.connect(third_website)
 /* code for fetching prices and images */
 /* Html code for displaying prices and images */
} catch(){}

此代码显示单个搜索查询在同一页面上的三个网站的结果。但问题是我的网站现在加载缓慢,有些时候不会显示一个或其他网站的结果。它给了我以下错误:

org.jsoup.HttpStatusException: HTTP error fetching URL.Status=500,URL=/*websitename*/

1 个答案:

答案 0 :(得分:0)

使用多个线程来请求站点会更好,因此您将同时向站点发送请求。

你可以这样做java Task Executor Service这样:

    FutureTask<Boolean> futureTask1 = new FutureTask<Boolean>(new Callable<Boolean>()
    {
        @Override
        public Boolean call() throws Exception
        {
            try
            {
                //jsoup.connect(second_website)
                 /* code for fetching prices and images */
                 /* Html code for displaying prices and images */
                return true;
            } catch(Exception e){
                return false;
            }

        }
    });

    FutureTask<Boolean> futureTask2 = new FutureTask<Boolean>(new Callable<Boolean>()
    {
        @Override
        public Boolean call() throws Exception
        {
            //Same as above
        }
    });

    FutureTask<Boolean> futureTask3 = new FutureTask<Boolean>(new Callable<Boolean>()
    {
        @Override
        public Boolean call() throws Exception
        {
            //Same as above
        }
    });

    ExecutorService executorService = Executors.newFixedThreadPool(3);

    executorService.execute(futureTask1);
    executorService.execute(futureTask2);
    executorService.execute(futureTask3);

    while (true){
        if(futureTask1.isDone() && futureTask2.isDone() && futureTask3.isDone()){
            //Here work is done
            try
            {
                if(futureTask1.get()){
                    //task 1 done it's work correctly
                }else {
                    //some problem in task 
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

        }

        try
        {
            Thread.sleep(100);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

如果这些任务之间有共同逻辑,则可以创建一个实现Callable接口的类,并将网站url传递给其构造函数。