在多线程负载/压力测试下,Spring Integration TCP连接被拒绝

时间:2017-10-23 12:57:53

标签: java multithreading sockets tcp spring-integration

我有一个处理TCP消息的Spring Integration项目。

最简单的场景是PING消息(从源接收消息并回显它),SI项目中的流程如下:

1)从源(通过tcp-inbound-gateway)接收消息。源在每条消息后关闭套接字。

2)变换器分析消息并使用回复通道名称

设置(以及其他)标头值

3)Header-Value-Router应用于将其路由回源的消息。

XML配置(简化版)如下:

<int-ip:tcp-connection-factory id="TCP_SRV"
                               type="server"
                               port="${router.port}"
                               using-nio="true"
                               single-use="true"
                               serializer="CustomSerializer"
                               deserializer="CustomSerializer"/>

<int-ip:tcp-inbound-gateway request-channel="rawInputFromSource"
                            reply-channel="outputBackToSource"
                            connection-factory="TCP_SRV"/>

<int:channel id="rawInputFromSource"/>

<int:transformer ref="inputFromSourceTransformer"
                 input-channel="rawInputFromSource"
                 output-channel="processedInputFromSource"/>

<int:channel id="processedInputFromSource"/>

<bean id="inputFromSourceTransformer" class="my.org.InputFromSourceTransformer"/>

<int:header-value-router input-channel="processedInputFromSource" 
                         header-name="RouteToChannel"/>

在手动调用消息时,它可以从功能性pov中正常工作,但在压力测试下失败。一旦我超过15个线程(每个线程运行for循环发送10条消息),我收到 java.net.ConnectException:连接被拒绝:连接大约20%的尝试。

线程用于发送消息的代码段:

byte[] sendAndReceive(byte[] data){
    byte[] result = new byte[data.length];
    try {
        Socket socket=new Socket("localhost", SI_PORT); // here is where the err occurs
        OutputStream output = socket.getOutputStream();
        InputStream  input = socket.getInputStream();
        output.write(data);
        input.read(result);
        socket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result;
}

错误:

java.net.ConnectException: Connection refused: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at my.org.PerformanceTest.sendAndReceive(PerformanceTest.java:98)

要求说它必须通过60个线程。我有什么想法可以解决这个问题?我尝试在工厂添加任务:executor id =“threadPoolTask​​Executor”pool-size =“5-10”queue-capacity =“100”rejection-policy =“CALLER_RUNS”,但这不是解决了这个问题。

非常感谢任何建议

1 个答案:

答案 0 :(得分:1)

增加服务器连接工厂的积压工作。

/**
 * The number of sockets in the connection backlog. Default 5;
 * increase if you expect high connection rates.
 * @param backlog The backlog to set.
 */
public void setBacklog(int backlog) {
    Assert.isTrue(backlog >= 0, "You cannot set backlog negative");
    this.backlog = backlog;
}

提供backlog属性的XML配置...

<xsd:attribute name="backlog" type="xsd:string">
    <xsd:annotation>
        <xsd:documentation>
            Specifies the connection backlog for server sockets. Does not
            apply to client factories.
        </xsd:documentation>
    </xsd:annotation>
</xsd:attribute>