Spring Cloud AWS:优化线程数

时间:2019-06-26 08:17:06

标签: java spring multithreading amazon-web-services spring-cloud-aws

我在Spring Boot应用程序中使用spring-cloud-aws-autoconfigurespring-cloud-aws-messaging从SQS队列中读取消息。

在应用程序线程转储中,几乎没有使用50个名为pool-3-thread-1 ... pool-3-thread-50的线程。在任何给定的时间,仅使用这些线程中的1个。使用线程时,它具有以下堆栈跟踪:

java.net.SocketInputStream.socketRead0(SocketInputStream.java:-2) native
              java.net.SocketInputStream.socketRead(SocketInputStream.java:116) 
              java.net.SocketInputStream.read(SocketInputStream.java:171) 
              java.net.SocketInputStream.read(SocketInputStream.java:141) 
              sun.security.ssl.InputRecord.readFully(InputRecord.java:465) 
              sun.security.ssl.InputRecord.read(InputRecord.java:503) 
              sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975) 
              sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:933) 
              sun.security.ssl.AppInputStream.read(AppInputStream.java:105) 
              org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137) 
              org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153) 
              org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:282) 
              org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138) 
              org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56) 
              org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259) 
              org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163) 
              org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:165) 
              org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273) 
              com.amazonaws.http.protocol.SdkHttpRequestExecutor.doReceiveResponse(SdkHttpRequestExecutor.java:82) 
              org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125) 
              org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272) 
              org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185) 
              org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) 
              org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) 
              org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) 
              com.amazonaws.http.apache.client.impl.SdkHttpClient.execute(SdkHttpClient.java:72) 
              com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1297) 
              com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1113) 
              com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:770) 
              com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:744) 
              com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:726) 
              com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:686) 
              com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:668) 
              com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:532) 
              com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:512) 
              com.amazonaws.services.sqs.AmazonSQSClient.doInvoke(AmazonSQSClient.java:2147) 
              com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:2116) 
              com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:2105) 
              com.amazonaws.services.sqs.AmazonSQSClient.executeReceiveMessage(AmazonSQSClient.java:1559) 
              com.amazonaws.services.sqs.AmazonSQSAsyncClient$14.call(AmazonSQSAsyncClient.java:1057) 
              com.amazonaws.services.sqs.AmazonSQSAsyncClient$14.call(AmazonSQSAsyncClient.java:1051) 
              java.util.concurrent.FutureTask.run(FutureTask.java:266) 
              java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 
              java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 
              java.lang.Thread.run(Thread.java:748) 

堆栈跟踪表明线程保留供从SQS读取。由于未使用线程,因此我想限制线程数以节省资源。

如何配置AmazonSQSClient正在使用的线程数?

我尝试按照建议的here创建自己的ThreadPoolTaskExecutor,但这改变了另一个池的设置,而不是pool-3

编辑:我从一开始就在监视应用程序,它的工作原理是这样的。该应用程序从池3中的1个线程开始:pool-3-thread-1。该线程正在运行,并在20秒后停止运行,并创建了另一个正在运行的线程(pool-3-thread-2)。重复此过程,直到创建了50个线程,然后在重用了线程2 20秒后再次重用了线程1,依此类推...

0 个答案:

没有答案