This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit a9ffc3fe23f029c699f063929dbd94ea716c0be1 Author: Otavio Rodolfo Piske <angusyo...@gmail.com> AuthorDate: Wed May 8 09:12:12 2024 +0200 (chores) camel-http-common: code cleanup - break large and complex methods --- .../org/apache/camel/http/common/CamelServlet.java | 74 ++++++++++++---------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java b/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java index 0d545e5b931..a3fb1eaaaf4 100644 --- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java +++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/CamelServlet.java @@ -106,45 +106,51 @@ public class CamelServlet extends HttpServlet implements HttpRegistryProvider { protected void handleService(HttpServletRequest req, HttpServletResponse resp) throws Exception { if (isAsync()) { - if (executorRef != null) { - HttpConsumer consumer = doResolve(req, resp); // can be done sync - if (consumer == null) { - return; - } - Executor pool = ObjectHelper.notNull(getExecutorService(consumer), executorRef); - final AsyncContext context = req.startAsync(); - try { - pool.execute(() -> { - try { - final CompletionStage<?> promise = doExecute(req, resp, consumer); - if (promise == null) { // early quit - context.complete(); - } else { - promise.whenComplete((r, e) -> context.complete()); - } - } catch (Exception e) { - onError(resp, e); - context.complete(); - } - }); - } catch (final RuntimeException re) { // submit fails - context.complete(); - throw re; - } - } else { // will use http servlet threads so normally http threads so better to enable useCamelExecutor - final AsyncContext context = req.startAsync(); - try { - context.start(() -> doServiceAsync(context)); - } catch (final RuntimeException re) { // submit fails - context.complete(); - throw re; - } - } + handleAsync(req, resp); } else { doService(req, resp); } } + private void handleAsync(HttpServletRequest req, HttpServletResponse resp) throws Exception { + if (executorRef != null) { + HttpConsumer consumer = doResolve(req, resp); // can be done sync + if (consumer == null) { + return; + } + Executor pool = ObjectHelper.notNull(getExecutorService(consumer), executorRef); + final AsyncContext context = req.startAsync(); + try { + pool.execute(() -> doAsyncExecution(req, resp, consumer, context)); + } catch (final RuntimeException re) { // submit fails + context.complete(); + throw re; + } + } else { // will use http servlet threads so normally http threads so better to enable useCamelExecutor + final AsyncContext context = req.startAsync(); + try { + context.start(() -> doServiceAsync(context)); + } catch (final RuntimeException re) { // submit fails + context.complete(); + throw re; + } + } + } + + private void doAsyncExecution(HttpServletRequest req, HttpServletResponse resp, HttpConsumer consumer, AsyncContext context) { + try { + final CompletionStage<?> promise = doExecute(req, resp, consumer); + if (promise == null) { // early quit + context.complete(); + } else { + promise.whenComplete((r, e) -> context.complete()); + } + } catch (Exception e) { + onError(resp, e); + context.complete(); + } + } + private void onError(HttpServletResponse resp, Exception e) { //An error shouldn't occur as we should handle most error in doService log.error("Error processing request", e);