I'm a beginner in springframe. There are some problems when using webclient. I can get the desired return information in the subscribe but after all the requests are returned, the file handles counted by
lsof -n |grep <javaPid> | wc -l
(handle count is 2097; Without subscribe it's only 46, but sure the target server haven't received the request) are greatly increased, and cannot be released all the time. Is there something wrong with my code? Thank you for your time.
package com.test.webclienttest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class WebclienttestApplication {
public static void main(String[] args) {
TestTasks tt = new TestTasks();
tt.updateAdsInfoWhenBoot();
SpringApplication.run(WebclienttestApplication.class, args);
}
}
package com.test.webclienttest;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TestTasks {
public void updateAdsInfoWhenBoot() {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Accept-Encoding", "gzip,deflate,br");
headers.put("Accept", "*/*");
String url = "https://test.com";
String path = "/api/test";
String body = "{\"content\":{\"data\":{}}}";
for(int i = 0; i < 2000; i++){
WebClient.create()
.post()
.uri(url+path)
.headers(httpHeaders -> {
for(Map.Entry<String,String> entry: headers.entrySet()){
httpHeaders.set(entry.getKey(), entry.getValue());
}
})
.body(BodyInserters.fromValue(body))
.retrieve()
.bodyToMono(String.class)
.subscribe(response -> {
log.info("b:{}", response);
});
}
log.info("TestTasks.updateAdsInfoWhenBoot!");
}
}
source fd.sh result:
ec2-user 4606 71.3 3.8 8102884 640708 pts/1 Sl+ 11:17 0:34 java -jar ./target/webclienttest-0.0.1-SNAPSHOT.jar
PID:4606
total:2561
java:2560
GC:0
VM:0
Reference:0
Finalizer:0
Signal:0
C2:0
C1:0
Sweeper:0
Service:0
Common-Cl:0
Catalina-:0
container:0
http-nio-:0
schedulin:0
reactor-h:0
parallel-:0
fd.sh
pid=`ps aux|grep java|grep webclienttest|grep -v pushd|awk '{print $2}'`
echo `ps aux|grep java|grep webclienttest|grep -v pushd`
echo PID:$pid
echo total:`lsof -n|grep $pid|wc -l`
TYPES=("java GC VM Reference Finalizer Signal C2 C1 Sweeper Service Common-Cl Catalina- container http-nio- schedulin reactor-h parallel-")
for TYPE in ${TYPES[@]}
do
echo $TYPE:`lsof -n|grep $pid|grep $TYPE|wc -l`
done
lsof -n|grep <PID>
(most handle is like that)
java 4606 ec2-user 2105u sock 0,7 0t0 455065798 can't identify protocol
java 4606 ec2-user 2106u sock 0,7 0t0 455065799 can't identify protocol
java 4606 ec2-user 2107u sock 0,7 0t0 455065800 can't identify protocol
java 4606 ec2-user 2108u sock 0,7 0t0 455065801 can't identify protocol
java 4606 ec2-user 2109u sock 0,7 0t0 455065802 can't identify protocol
java 4606 ec2-user 2110u sock 0,7 0t0 455065803 can't identify protocol
java 4606 ec2-user 2111u sock 0,7 0t0 455065804 can't identify protocol
java 4606 ec2-user 2112u sock 0,7 0t0 455065805 can't identify protocol
java 4606 ec2-user 2113u sock 0,7 0t0 455065806 can't identify protocol
java 4606 ec2-user 2114u sock 0,7 0t0 455065807 can't identify protocol
java 4606 ec2-user 2115u sock 0,7 0t0 455065808 can't identify protocol
java 4606 ec2-user 2116u sock 0,7 0t0 455065809 can't identify protocol
java 4606 ec2-user 2117u sock 0,7 0t0 455065810 can't identify protocol
java 4606 ec2-user 2118u sock 0,7 0t0 455065811 can't identify protocol
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>webclienttest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>webclienttest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<lombok.version>1.18.12</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Comment From: bclozel
Thanks for the report @maomaoguo2017 , this is a duplicate of #21923 This issue will be solved in the next release with the reactor upgrade.
Comment From: izeye
@bclozel You seem to reference a wrong PR 😅
Comment From: bclozel
@izeye Sorry, got tricked by the issue number autocompletion - again. I've edited my comment.