Java API client version: 7.17.10
Java version: jdk-17.0.3.1
Elasticsearch Version: 7.17
我有个代码仓库可以复现这个问题: GitHub - xiaochunyong/elasticsearch-threadlocal-reference-changed
有一个类UserHolder, 里面有个ThreadLocal变量
public class UserHolder {
public static ThreadLocal<String> holder = new ThreadLocal<>();
}
当在PostController中调用ElasticsearchClient.query方法时, 进入query方法前和进入方法后, currentThread不一样
@RestController
@RequestMapping("/api/post")
public class PostController {
@Autowired
private ElasticsearchClient client;
public static Thread currentThread;
public static String welcome = "hello1";
@GetMapping
public Object query() throws IOException {
currentThread = Thread.currentThread();
welcome = "hello2";
SearchResponse<ObjectNode> search = client.search(s -> s
.index("post")
.query(b -> b
.matchAll(b2 -> b2)
),
ObjectNode.class
);
return search.hits().hits();
}
}