Java 如何测试我的连接池是否以正确的方式工作?
我正在使用ApacheAxis2实现一个SOAP客户端。由于SOAP客户端必须处理大量请求,因此我使用的是连接池 为此,我必须为从WSDL文件生成的存根设置一些传输层配置:Java 如何测试我的连接池是否以正确的方式工作?,java,http,soap,testing,axis2,Java,Http,Soap,Testing,Axis2,我正在使用ApacheAxis2实现一个SOAP客户端。由于SOAP客户端必须处理大量请求,因此我使用的是连接池 为此,我必须为从WSDL文件生成的存根设置一些传输层配置: stub._getServiceClient().getOptions().setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Constants.VALUE_TRUE); MultiThreadedHttpConnectionManager connectionManager = n
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);
我的客户似乎工作得很好。但是,我想知道如何测试连接池是否以正确的方式工作(即,创建的连接只有在空闲连接超时常量定义的时间之后才会被破坏)。有什么想法吗?编写一个测试床应用程序,它将发出大量请求,并断言连接数永远不会超过MAX\u连接数。您可以通过将jconsole或VisualVM附加到进程来检查后者
您还可以研究如何在类/客户机上生成负载,然后绘制出几个数据点(但不确定如何访问创建的客户机连接数)。基于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秒应根据连接管理器的灵敏度进行调整。您可以通过更改参数进行测试
- 没有任何线程
- 池大小
- 启用\u连接\u池
/**并发搜索数*/
私有静态final int NO_OF_线程=20;
/**http连接池的大小*/
专用静态最终整数池大小=20;
/**启用或禁用连接池*/
私有静态布尔启用\连接\池=true;
/**关闭空闲连接时间(毫秒)
*如果连接在此时间处于空闲状态,则将释放连接*/
私有静态int关闭\空闲\连接\时间=1000;
公开无效测试()
{
init();
长启动=System.currentTimeMillis();
List threads=new ArrayList();
for(int i=0;i
}如果空闲超时后连接被破坏,该如何测试?
/** 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 );
}