我有一个正在使用的客户库和传入DataRequest对象拥有userid,timeout并且在它的一些其他领域.现在我使用这个DataRequest对象创建一个URL,然后我使用了一个HTTP调用RestTemplate,我的服务返回一个JSON响应,我用它来创建一个DataResponse对象并将这个DataResponse对象返回给它们.
以下是DataClient客户通过将DataRequest对象传递给我的类.DataRequest如果在getSyncData方法中花费太多时间,我正在使用客户传递的超时值来超时请求.
public class DataClient implements Client {
private final RestTemplate restTemplate = new RestTemplate();
private final ExecutorService service = Executors.newFixedThreadPool(10);
// this constructor will be called only once through my factory
// so initializing here
public DataClient() {
try {
restTemplate.setRequestFactory(clientHttpRequestFactory());
} catch (Exception ex) {
// log exception
}
}
@Override
public DataResponse getSyncData(DataRequest key) { …Run Code Online (Sandbox Code Playgroud) java spring multithreading resttemplate apache-httpclient-4.x
我有下面的代码
ThreadLocal<Map<String, Service<Request, Response>>> connectinonMapThread = new ThreadLocal<Map<String, Service<Request, Response>>>() {
@Override
protected Map<String, Service<Request, Response>> initialValue() {
return new HashMap<String, Service<Request, Response>>();
}
};
Run Code Online (Sandbox Code Playgroud)
我想使用 lambda 表达式来编写它,如下所示 -
ThreadLocal<Map<String, Service<Request, Response>>> connectinonMapThread2 = new ThreadLocal<Map<String, Service<Request, Response>>>(() -> new HashMap<String, Service<Request, Response>>());
Run Code Online (Sandbox Code Playgroud)
我尝试了另一种。
ThreadLocal<Map<String, Service<Request, Response>>> connectinonMapThread2 = initialValue() -> {
return new HashMap<String, Service<Request, Response>>();
};
Run Code Online (Sandbox Code Playgroud)
但我收到编译错误。但 IntelliJ Idea 建议这可以写成 lambda 表达式。