接続プールが正しく動作しているかどうかをテストするにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1093888

質問

Apache Axis 2 を使用して SOAP クライアントを実装しています。SOAP クライアントは大量のリクエストを処理する必要があるため、接続プールを使用しています。

そのためには、WSDL ファイルから生成されたスタブのトランスポート層構成をいくつか設定する必要がありました。

stub._getServiceClient().getOptions().setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Constants.VALUE_TRUE);

MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
connectionManager.getParams().setDefaultMaxConnectionsPerHost(MAX_CONNECTIONS_PER_HOST);
connectionManager.closeIdleConnections(IDLE_CONNECTION_TIMEOUT);
HttpClient httpClient = new HttpClient(connectionManager);

stub._getServiceClient().getOptions().setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);

私のクライアントは問題なく動作しているようです。ただし、接続プールが正しい方法で機能しているかどうかをテストする方法を知りたいです(つまり、作成された接続は、IDLE_CONNECTION_TIMEOUT 定数で定義された時間が経過した後にのみ破棄されます)。何か案は?

役に立ちましたか?

解決

のJUnit 3.xのに基づいて擬似コード

  setUp() {
    initialize connection manager;
    initialize connection by creating client;
  }

  tearDown() {
    close connection if necessary;
    close connection manager if necessary;
  }

  testConnectionOpen() {
    assert that connection is open;
    pause for time of idle connection timeout - 1 second;
    assert that connection **is still open**;
  }

  testConnectionClosed() {
    assert that connection is open;
    pause for time of idle connection timeout + 1 second;
    assert that connection **is closed**;
  }

1秒を追加し、1秒を減算すると、接続マネージャの感度に応じて調整されるべきである。

他のヒント

多数の要求を行いますテストベッドアプリケーションを作成し、接続数がMAX_CONNECTIONS以上は決してないことを主張します。あなたは、プロセスにjconsoleをやVisualVMのを取り付けることによって、後者を確認することができるかもしれません。

また、クラス/クライアントの負荷を生成するためのジャカルタのJMeter を使用してに見ることができる、とその後、(あなたがが、作成したクライアント接続の数へのアクセスを獲得したいかどうかはわかりません)いくつかのデータポイントをグラフます。

パラメータを変更してテストできます

  • NO_OF_THREADS
  • プール_サイズ
  • ENABLE_CONNECTION_POOLING

/** number of concurrent searches */
private static final int NO_OF_THREADS = 20;

/** size of the http connection pool */
private static final int POOL_SIZE = 20;

/** enabling or disabling the connection pool */
private static boolean ENABLE_CONNECTION_POOLING = true;

/** close idle connection time in milliseconds
 * connections will be release if they are idle for this time */
private static int CLOSE_IDLE_CONNECTION_TIME = 1000;

public void test()
{
    init();

    long start = System.currentTimeMillis();

    List<Thread> threads = new ArrayList<Thread>();
    for ( int i = 0; i < NO_OF_THREADS; i++ )
    {
        SimpleThread thread = new SimpleThread();
        thread.start();

        threads.add( thread );
    }

    for ( Thread t : threads )
    {
        try
        {
            t.join();
        }
        catch ( InterruptedException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    logger.info( "************* Test Finish In *******************" + ( System.currentTimeMillis() - start ) + " ms" );
}

public void init()
{
    super.init();

    try
    {
        long t1 = System.currentTimeMillis();
        stub = new Viva_x0020_external_x0020_API_x0020_for_x0020_partnersStub( endUrl );

        if ( ENABLE_CONNECTION_POOLING )
        {
            stub._getServiceClient().getOptions().setProperty( HTTPConstants.REUSE_HTTP_CLIENT, Constants.VALUE_TRUE );
            MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
            connectionManager.getParams().setDefaultMaxConnectionsPerHost( POOL_SIZE );
            connectionManager.closeIdleConnections( CLOSE_IDLE_CONNECTION_TIME );
            HttpClient httpClient = new HttpClient( connectionManager );
            stub._getServiceClient().getOptions().setProperty( HTTPConstants.CACHED_HTTP_CLIENT, httpClient );
        }

        logger.info( "Connection Established in " + ( System.currentTimeMillis() - t1 ) + "ms" );
    }
    catch ( AxisFault e1 )
    {
        e1.printStackTrace();
        fail( "Error on creating the stub" );
    }

    logger.info( "Connection Initialized successfully" );
}

public class SimpleThread extends Thread
{
    public void run()
    {
        search();
    }
}

private static void search()
{
    logger.info( "$$$$$$$$$$$$$ Start the Search $$$$$$$$$$$$$$" );
    GetAirportConnections request = new GetAirportConnections();
    request.setAirportCode( "MTY" );

    GetAirportConnectionsResponse response = null;
    try
    {
        long t1 = System.currentTimeMillis();
        response = stub.getAirportConnections( request, getUserCredentials() );
        logger.info( "Results Retrived in " + ( System.currentTimeMillis() - t1 ) + "ms" );
    }
    catch ( Exception e )
    {
        logger.error( "------------------------- Connection Timeout --------------------------" );
        e.printStackTrace();
        fail( e.getMessage() );
    }

    Airport[] airports = response.getGetAirportConnectionsResult().getAirport();
    logger.info("Number of airports : " + airports.length );
}

}

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top