相关疑难解决方法(0)

如何在多线程环境中更好地使用ExecutorService?

我需要创建一个库,在其中我将有同步和异步方法.

  • executeSynchronous() - 等到我有结果,返回结果.
  • executeAsynchronous() - 立即返回Future,如果需要,可在其他事情完成后处理.

我的图书馆的核心逻辑

客户将使用我们的库,他们将通过传递DataKey构建器对象来调用它.然后我们将使用该DataKey对象构造一个URL,并通过执行它来对该URL进行HTTP客户端调用,然后在我们将响应作为JSON字符串返回之后,我们将通过创建DataResponse对象将该JSON字符串发送回我们的客户.有些客户会打电话executeSynchronous(),有些人可能会打电话executeAsynchronous(),这就是为什么我需要在我的库中单独提供两种方法.

接口:

public interface Client {

    // for synchronous
    public DataResponse executeSynchronous(DataKey key);

    // for asynchronous
    public Future<DataResponse> executeAsynchronous(DataKey key);
}
Run Code Online (Sandbox Code Playgroud)

然后我有我DataClient实现上面的Client接口:

public class DataClient implements Client {

    private RestTemplate restTemplate = new RestTemplate();
    // do I need to have all threads as non-daemon or I can have daemon thread for my use case?
    private ExecutorService executor = …
Run Code Online (Sandbox Code Playgroud)

java multithreading daemon callable executorservice

10
推荐指数
2
解决办法
1280
查看次数

在库中实现同步和异步方法的正确方法是什么?

我需要创建一个库,在其中我将具有同步和异步功能.

  • executeSynchronous() - 等到我有结果,返回结果.
  • executeAsynchronous() - 立即返回Future,如果需要,可在其他事情完成后处理.

我的图书馆的核心逻辑

客户将使用我们的库,他们将通过传递DataKey构建器对象来调用它.然后我们将使用该DataKey对象构造一个URL,并通过执行它来对该URL进行HTTP客户端调用,然后在我们将响应作为JSON字符串返回之后,我们将通过创建DataResponse对象将该JSON字符串发送回我们的客户.有些客户会调用executeSynchronous(),有些可能会调用executeAsynchronous()方法,这就是为什么我需要在库中单独提供两种方法.

接口:

public interface Client {

    // for synchronous
    public DataResponse executeSynchronous(DataKey key);

    // for asynchronous
    public Future<DataResponse> executeAsynchronous(DataKey key);
}
Run Code Online (Sandbox Code Playgroud)

然后我有我DataClient实现上面的Client接口:

public class DataClient implements Client {

    private RestTemplate restTemplate = new RestTemplate();
    private ExecutorService executor = Executors.newFixedThreadPool(10);

    // for synchronous call
    @Override
    public DataResponse executeSynchronous(DataKey key) {
        DataResponse dataResponse = null;
        Future<DataResponse> future = null;

        try …
Run Code Online (Sandbox Code Playgroud)

java performance multithreading executorservice resttemplate

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

使用RestTemplate的spring webservices的超时配置

我想使用RestTemplate为客户端配置spring webservices的超时.我尝试了以下配置:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg>
    <bean class="org.springframework.http.client.CommonsClientHttpRequestFactory">
    <property name="readTimeout" value="10000" />
    </bean>
</constructor-arg>
    <property name="messageConverters">
    <list>
    <ref bean="stringHttpMessageConverter" />
    <ref bean="marshallingHttpMessageConverter" />
    </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

但是当我启动tomcat时,我有一个NoClassDefFoundError:

06 févr. 2012 10:43:43,113 [ERROR,ContextLoader] Context initialization failed
java.lang.NoClassDefFoundError: org/apache/commons/httpclient/HttpMethodBase
Run Code Online (Sandbox Code Playgroud)

但是我在我的pom.xml中包含了commons-httpclient:

    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency
Run Code Online (Sandbox Code Playgroud)

知道如何做/解决这个问题吗?

提前致谢 !

java spring web-services timeout resttemplate

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