最近在做性能测试,我想每隔100ms发送一个数据给服务器上的系统(web服务器是tomcat),共10000次。这样写可以吗?
//发送10000次告警至A系统
    public static void main(String[] args) {
        for(int i = 1; i <= 10000; i++) {
            AlarmThread at = new AlarmThread();
            at.setName("AlarmThread-"+i);
            at.start();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
问题来了,我一开始没有设i=10000,i=200都还可以,但是往上改成300,500就出现了以下的问题。
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at com.SendModule.doSend(SendModule.java:28)
at com.LockAlarm.doSend(LockAlarm.java:58)
at com.AlarmThread.run(AlarmThread.java:21)
是由于tomcat设置的最大线程为150吗,所以Connection refused?那我把最大线程数改成10000可以吗?如果不行,那我要发10000个,一次只能发200个,岂不是要发50次?还是要换tomcat,用别的服务器
Tomcat

解决方案 »

  1.   

    我是这样的。URL请求访问servlet,然后再调用数据库。这是设置数据库连接池
    <Context path="/BAL" reloadable="true" docBase="E:\eclipse\workspace\AAA" workDir="E:\eclipse\workspace\AAA\work">
      <Resource
    name="jdbc/fastfrm/sqlserver" 
    type="javax.sql.DataSource" 
    driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
    maxIdle="2" 
    maxWait="10000" 
    username="aaa" 
    password="111" 
    url="jdbc:sqlserver://127.0.0.1:1433;SelectMethod=cursor;DatabaseName=BAL" 
    maxActive="200"/>
    </Context>
    那如果我把maxActive改成5000或10000,可以吗
      

  2.   

    恩,可能的。我把server.xml里maxThreads设置成500,再把maxActive=500,还是有这个问题。它也不是所有的都不成功,有的数据会出现connection refused.那怎么办?难不成我每次只能发200个,得发50次?
      

  3.   

    maxActive是最大激活连接数,设 0 为没有限制。这里取值为20,表示同时最多有20个数据库连接。
    maxIdle是最大的空闲连接数,设 0 为没有限制。这里取值为20,表示即使没有数据库连接时依然可以保持20空闲的连接,而不被清除,随时处于待命状态。
    MaxWait是最大等待秒钟数,这里取值-1,表示无限等待,直到超时为止,也可取值9000,表示9秒后超时。
    如以下配置:maxWait=5000
     maxActive=50
     maxIdle=5
    LZ可多试试看