小编Diy*_*kir的帖子

如果我不使用自定义连接管理器,我还应该在Apache HttpClient上设置ConnectionRequestTimeout吗?

我正在使用Apache RequestConfig来配置我的一些超时HttpClient.

RequestConfig config = RequestConfig.custom()
        .setConnectTimeout(timeout)
        .setSocketTimeout(timeout)
        .setConnectionRequestTimeout(timeout) // Can I leave this out..
        .build();

CloseableHttpClient httpClient = HttpClients.custom()
        //.setConnectionManager(connectionManager) // ..if I don't use this
        .setDefaultRequestConfig(config)
        .build();
Run Code Online (Sandbox Code Playgroud)

setConnectionRequestTimeout(timeout)即使我没有自定义连接管理器/池设置,它是否有意义调用?

据我所知,setConnectionRequestTimeout(timeout)用于设置等待连接管理器/池连接的时间.

请注意,我没有在httpClient(请参阅注释行)上设置连接管理器.

java timeout apache-httpclient-4.x

8
推荐指数
2
解决办法
2788
查看次数

是否有必要为在Tomcat容器中运行的Web应用程序关闭ExecutorService?

ExecutorService如果它在Servlet的Tomcat容器中运行,是否有必要在某个时刻关闭它.如果是,那么我应该在何处致电关机?我尝试在submit()调用之后添加它,但是当我从客户端浏览器向Servlet发出另一个请求时,我得到一个RejectedExecutionException可能是因为我关闭了?我试图了解它如何在Tomcat中的Servlet中工作以及我应该如何使用它.

我在我的webApplication中执行以下操作(它似乎工作正常,没有任何关闭):

// In configuration class
@Bean (name = "executorService")
public ExecutorService executorService() {
  return Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
}

// In some other class
@Qualifier("executorService")
@Autowired
private ExecutorService executorService;
....
private void load() {
  executorService.submit(new Runnable() {
    @Override
    public void run() {
        doSomethingInTheBackground();
    }
});

// If I enable this I will get a RejectedExecutionException 
// for a next request.
// executorService.shutdown();
Run Code Online (Sandbox Code Playgroud)

}

java tomcat executorservice

6
推荐指数
1
解决办法
971
查看次数

如何使用javax动态servlet注册配置多个Jersey提供程序包

javax.servlet.Registration用来动态配置/注册servlet.

我想知道如何配置多个提供程序包Jersey.我可以使用以下setInitParamater方法添加单个包:

public class MyWebInitalization implements WebApplicationInitializer {

  @Override
  public void onStartup(final ServletContext servletContext) throws ServletException {
    //... some code
    final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", ServletContainer.class);
    dispatcher.setInitParameter("jersey.config.server.provider.packages", "com.package1"); // how to add more packages?
    //... more code
  }
} 
Run Code Online (Sandbox Code Playgroud)

为了让您了解我的意思,如果我使用web.xml,这是一个等价的:

<servlet>
    ...
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.package1;com.package2</param-value>
    </init-param>
    ...
</servlet>
Run Code Online (Sandbox Code Playgroud)

java servlets jersey

5
推荐指数
1
解决办法
974
查看次数

使用空抽象类有什么好的替代方法?

假设我有以下内容:

public abstract class AbstractResponse {
  // this class is totally empty!
}

public class ResponseA extends AbstractResponse {
  // JSON POJO 
}

public class ResponseB extends AbstractResponse {
  // JSON POJO 
}

public abstract class AbstractClient {
  public abstract Class getType();

  public AbstractResponse readResponse() {
    ObjectMapper mapper = new ObjectMapper();
    AbstractResponse response;
    try {
        return (AbstractResponse) mapper.readValue(data, getType());
    } catch (IOException e) {...}
  }
}

public class ResponseAClient extends AbstractClient {
  public Class getType() {
    return ResponseA.class;
  }
} …
Run Code Online (Sandbox Code Playgroud)

java abstract

4
推荐指数
1
解决办法
667
查看次数

Apache HttpClient执行是否会对所有HTTP 5XX错误抛出IOException?

Apache的HttpClient的文档execute(HttpHost target, HttpRequest request)方法,说:

IOException - 如果出现问题或连接中止

如果我捕获IOException,这会捕获所有 服务器5xx错误吗?

try {
  response = client.execute(httpHost, request);
} catch (IOException e) {
  // throw custom Exception
} finally {
  // close response and client
}
Run Code Online (Sandbox Code Playgroud)

我问的原因是,在这个逻辑之后的其他地方,我们正在做类似以下的事情:

if (response.getStatusLine().getStatusCode() >= 500) {
  // Could we ever reach this point after the code above?
}
Run Code Online (Sandbox Code Playgroud)

java ioexception apache-httpclient-4.x

4
推荐指数
1
解决办法
3332
查看次数

何时使用线程池而不是调用新线程

我有一个 JAX-RS/Jersey Rest API,它获取请求并需要在单独的线程中执行额外的工作,但我不确定是否建议使用线程池。我预计会有很多对该 API 的请求(每天几千个),但我在后台只有一项额外的工作。

每次都像这样创建一个新线程会不会很糟糕?任何意见,将不胜感激。我以前没有使用过ThreadPool。

@Get
@Path("/myAPI")
public Response myCall() {
  // call load in the background
  load();
  ...
  // do main job here
  mainJob();
  ...
}

private void load() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            doSomethingInTheBackground();
        }
    }).start();
}
Run Code Online (Sandbox Code Playgroud)

编辑: 只是为了澄清。我只需要一个额外的作业在后台运行。该作业将调用另一个 API 来记录一些信息,仅此而已。但它必须对每个请求执行此操作,并且我不需要等待响应。这就是为什么我想在新的后台线程中执行此操作。

Edit2: 这就是我现在想到的。谁能告诉我这是否可行(它在本地工作)以及我是否需要关闭执行程序(请参阅代码中的评论)?

// Configuration class
@Bean (name = "executorService")
public ExecutorService executorService() {
    return Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
}

// Some other class
@Qualifier("executorService")
@Autowired
private ExecutorService executorService;
....
private void …
Run Code Online (Sandbox Code Playgroud)

java multithreading

2
推荐指数
1
解决办法
1180
查看次数

更改 REST API 响应中的字段名称

嗨,我是新手,所以我可能不了解高级解决方案!

因此,我想为 REST API 发送响应。所以基本上发生的事情是我从一个方法返回一个对象,然后这些对象的字段及其相应的值显示在 API 响应中。

明确地说,我们有一个类定义为

Class Sample
{
    String fruit;
    int cost;
}
Run Code Online (Sandbox Code Playgroud)

现在假设我们有一个上述类的对象(示例 abc)。我们还有一个方法如下:

@RequestMapping(/inventory)
public Sample getInventory()
{
    Sample abc = new Sample();
    ----Some processing----
    return abc;
}
Run Code Online (Sandbox Code Playgroud)

所以我得到的回应是

{
    "fruit":"apple",
    "cost":100
}
Run Code Online (Sandbox Code Playgroud)

现在我想要的是响应中的“fruit.name”,但显然我不能有具有该名称的字段(带有点或@ 字符)!

{
    "fruit.name":"apple",
    "cost@Dollars":100
}
Run Code Online (Sandbox Code Playgroud)

那么有什么方法可以帮助我得到这种回应吗?

java rest spring json spring-mvc

1
推荐指数
1
解决办法
4984
查看次数

没有try/catch块关闭InputStream?

我知道InputStream应该关闭.但我有点怀疑在哪里以及如何做到这一点.

根据IOUtils.closeQuietly上的文档:

无条件地关闭InputStream.等效于InputStream.close(),但将忽略任何异常.这通常用在finally块中.

我的try/catch代码中不需要块,所以我没有finally块.在我的方法中返回之前关闭InputStream是否可以,或者我应该采取不同的做法?多个服务将使用此方法InputStream从文件加载.

public InputStream read(String filename) {
    InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);

    if (inputStream == null) {
      // Throw some exception
    }

    IOUtils.closeQuietly(inputStream);

    return inputStream;
}
Run Code Online (Sandbox Code Playgroud)

java inputstream

1
推荐指数
1
解决办法
2340
查看次数