首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Commonj发送异步HTTP调用

使用Commonj发送异步HTTP调用
EN

Stack Overflow用户
提问于 2014-12-29 22:05:53
回答 1查看 934关注 0票数 0

我从连续的HTTP调用切换到4个REST服务,转而使用commonj4工作管理器任务执行器进行4个同时调用。我使用的是WebLogic 12c。这个新代码适用于我的开发环境,但是在我们的测试环境中,在加载条件下,有时在不加载的情况下,结果映射没有填充所有的结果。但是,日志记录表明每个工作项确实收到了结果。这会是ConcurrentHashMap的问题吗?在IBM的这个例子中,他们使用他们自己的工作版本,并且有一个getData()方法,尽管它不喜欢这个方法真的存在于他们的类定义中。我遵循了一个不同的示例,它只是使用了工作类,但没有演示如何将数据从这些线程中获取到主线程中。我应该使用execute()而不是schedule()吗?这个API似乎没有很好的文档。线程超时足够高。component.processInbound()实际上包含了HTTP调用的代码,但问题并不存在,因为我可以切换回下面类的同步版本,没有任何问题。

workmgr.html

我的代码:

代码语言:javascript
复制
public class WorkManagerAsyncLinkedComponentRouter implements
        MessageDispatcher<Object, Object> {

    private List<Component<Object, Object>> components;
    protected ConcurrentHashMap<String, Object> workItemsResultsMap;
    protected ConcurrentHashMap<String, Exception> componentExceptionsInThreads;
...

    //components is populated at this point with one component for each REST call to be made.
    public Object route(final Object message) throws RouterException {
    ...
        try {
            workItemsResultsMap = new ConcurrentHashMap<String, Object>();
            componentExceptionsInThreads = new ConcurrentHashMap<String, Exception>();
            final String parentThreadID = Thread.currentThread().getName();

            List<WorkItem> producerWorkItems = new ArrayList<WorkItem>();
            for (final Component<Object, Object> component : this.components) {
                producerWorkItems.add(workManagerTaskExecutor.schedule(new Work() {
                    public void run() {
                        //ExecuteThread th = (ExecuteThread) Thread.currentThread();
                        //th.setName(component.getName());
                        LOG.info("Child thread " + Thread.currentThread().getName()  +" Parent thread: " + parentThreadID + " Executing work item for: " + component.getName());
                        try {
                            Object returnObj = component.processInbound(message);
                            if (returnObj == null)
                                LOG.info("Object returned to work item is null, not adding to producer components results map, for this producer: "
                                        + component.getName());
                            else {
                                LOG.info("Added producer component thread result for: "
                                        + component.getName());
                                workItemsResultsMap.put(component.getName(), returnObj);
                            }
                            LOG.info("Finished executing work item for: " + component.getName());
                        } catch (Exception e) {
                            componentExceptionsInThreads.put(component.getName(), e);
                        }
                    }
...
                }));
            } // end loop over producer components

            // Block until all items are done
            workManagerTaskExecutor.waitForAll(producerWorkItems, stuckThreadTimeout);

            LOG.info("Finished waiting for all producer component threads.");
            if (componentExceptionsInThreads != null
                    && componentExceptionsInThreads.size() > 0) {
                ...
            }
            List<Object> resultsList = new ArrayList<Object>(workItemsResultsMap.values());
            if (resultsList.size() == 0)
                throw new RouterException(
                        "The producer thread results are all empty.  The threads were likely not created.  In testing this was observed when either 1)the system was almost out of memory (Perhaps the there is not enough memory to create a new thread for each producer, for this REST request), or 2)Timeouts were reached for all producers.");
            //** The problem is identified here.  The results in the ConcurrentHashMap aren't the number expected .
            if (workItemsResultsMap.size() != this.components.size()) {
                StringBuilder sb = new StringBuilder();
                for (String str : workItemsResultsMap.keySet()) {
                    sb.append(str + " ");
                }
                throw new RouterException(
                        "Did not receive results from all threads within the thread timeout period.  Only retrieved:"
                                + sb.toString());
            }
            LOG.info("Returning " + String.valueOf(resultsList.size()) + " results.");
            LOG.debug("List of returned feeds: " + String.valueOf(resultsList));
            return resultsList;

        }
...
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-13 18:46:48

最后,我克隆了用作参数的DOM文档。必须有一些下游代码对参数有副作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27696733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档