Top / Java / Pingアプリケーション / Pingをうつクライアント

まずは単純なところから。PingをうつクライアントPingClientを作りました。

ソースコードはこちら

このクラスは、フィールドにセットされたPingサーバたちにPingをうちます。 PingClient#doPingメソッドで、ブログ名やブログのURLを指定することができます。 XML-RPC形式のPingリクエストの生成は、XML-RPCのページにあるとおりApache XML-RPCを用いています。

/*******************************************************************************
 * Copyright (c) 2005 Masatomi KINO.
 * All rights reserved. 
 * $Id: PingClient.java,v 1.5 2006/02/20 16:51:48 cvsuser Exp $
 *******************************************************************************/
//作成日: 2005/10/29
package nu.mine.kino.servlets.ping;

/**
 * PingサーバにweblogUpdates.pingを打つクライアントです。
 * 
 * @author Masatomi KINO
 * @version $Revision: 1.5 $
 * @spring.bean id = "pingClient"
 */
public class PingClient {
  private static final Logger logger = Logger.getLogger(PingClient.class);

  private List endpointURLList = new ArrayList();

  /**
   * {@link PingClient#addEndpoint(String)} で指定された
   * エンドポイントのPingサーバにPingを打ちます。
   * 
   * @param weblogname Blogの名前
   * @param weblogurl  BlogのURL
   */
  public void doPing(String weblogname, String weblogurl) {
    logger.info("doPing(String, String) - start");

    Iterator iterator = endpointURLList.iterator();
    while (iterator.hasNext()) {
      String destination = (String) iterator.next();
      logger.info(destination + " : にPingを打ちます。");
      logger.info("Weblog Name: " + weblogname);
      logger.info("URL: " + weblogurl);
      doPing(weblogname, weblogurl, destination); <-後述
    }
    logger.info("doPing(String, String) - end");
  }

  /**
   * @param endpointURLList
   * @spring.property list = "http://ping.cocolog-nifty.com/xmlrpc"
   */
  public void setEndpointURLList(List endpointURLList) {
    this.endpointURLList = endpointURLList;
  }
}

実際にApache XML-RPCを使ったPingの処理は以下の通りになっています。

private void doPing(String weblogname, String weblogurl, String destination) {
  logger.debug("doPing(String, String) - start");

  // weblogUpdates.ping に渡すパラメタを順番通りVectorに入れる
  Vector parameterList = new Vector();
  parameterList.addElement(weblogname);
  parameterList.addElement(weblogurl);

  try {
    XmlRpcClient client = null;
    XmlRpc.setEncoding("UTF-8");
    client = new XmlRpcClient(destination);

    // さっきのエンドポイントの、weblogUpdates.ping と
    // いうメソッドをコールする。パラメタはVectorに格納。
    Hashtable response = (Hashtable) client.execute(
        "weblogUpdates.ping", parameterList);
    if (response != null) {
      Enumeration e = response.keys();
      while (e.hasMoreElements()) {
        Object key = e.nextElement();
        logger
            .debug("KEY=" + key + " : VALUE="
                + response.get(key));
      }
    }
  } catch (MalformedURLException e1) {
    logger.error("doPing(String, String)", e1);
  } catch (XmlRpcException e) {
    logger.error("doPing(String, String)", e);
  } catch (IOException e) {
    logger.error("doPing(String, String)", e);
  }

  logger.debug("doPing(String, String) - end");
}

以上でPingをうつクライアントは完成です。これで試しにPingをうってみたところ以下のリクエストがPostされました。

<?xml version="1.0"?>
<methodCall>
  <methodName>weblogUpdates.ping</methodName>
  <params>
    <param><value>[weblogname]</value></param>
    <param><value>[weblogurl]</value></param>
  </params>
</methodCall>

この記事は

選択肢 投票
おもしろかった 1  
そうでもない 0  

Top / Java / Pingアプリケーション / Pingをうつクライアント

現在のアクセス:7783


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS