davsclaus commented on a change in pull request #4240:
URL: https://github.com/apache/camel/pull/4240#discussion_r490275407



##########
File path: 
components/camel-optaplanner/src/main/java/org/apache/camel/component/optaplanner/OptaPlannerProducer.java
##########
@@ -59,12 +63,38 @@ protected void doStop() throws Exception {
         super.doStop();
     }
 
-    @SuppressWarnings("unchecked")
     @Override
-    public synchronized void process(Exchange exchange) throws Exception {
-        final Object body = exchange.getIn().getMandatoryBody();
-        final String solverId = getSolverId(exchange);
+    public boolean process(Exchange exchange, AsyncCallback callback) {

Review comment:
       The callback must ALWAYS be called, so here is a few problems.
   
   There is no return false for the async processing. And in case of an 
exception in try .. catch, you need to call callback.done(true) also (for 
example if mandatory body throws exception), which can be done in a finally 
block, or just above return true etc.

##########
File path: 
components/camel-optaplanner/src/main/java/org/apache/camel/component/optaplanner/OptaPlannerProducer.java
##########
@@ -113,6 +143,54 @@ public void run() {
         }
     }
 
+    /**
+     * Using SolverManager
+     *
+     * @param  exchange
+     * @param  body
+     * @throws Exception
+     */
+    private void processWithSolverManager(Exchange exchange, Object body, 
AsyncCallback callback)
+            throws Exception {
+        final SolverManager solverManager = getSolverManager(exchange);
+
+        if (body.getClass().isAnnotationPresent(PlanningSolution.class)) {
+            LOGGER.debug("Asynchronously solving problem: [{}] with id [{}]", 
body);
+            Long problemId = endpoint.getConfiguration().getProblemId();
+            if (isAsync(exchange)) {
+                executor.submit(() -> {
+                    try {
+                        // create a consumer for best solution
+                        OptaplannerSolution consumer = 
endpoint.getOrCreateSolution(problemId);
+                        consumer.bestSolution(body);
+                        // start solving :: Solver Job is a thread
+                        SolverJob solverJob = 
solverManager.solveAndListen(problemId, t -> body, consumer::bestSolution);
+                        // wait for result
+                        populateResultWithSolverManager(exchange, solverJob);
+                    } catch (Throwable e) {
+                        LOGGER.error("Asynchronously solving failed with 
SolverManager, for problemId ({})", problemId, e);

Review comment:
       You may want to set that exception on exchange, so Camel can react upon 
that.

##########
File path: 
components/camel-optaplanner/src/main/java/org/apache/camel/component/optaplanner/OptaPlannerProducer.java
##########
@@ -113,6 +143,54 @@ public void run() {
         }
     }
 
+    /**
+     * Using SolverManager
+     *
+     * @param  exchange
+     * @param  body
+     * @throws Exception
+     */
+    private void processWithSolverManager(Exchange exchange, Object body, 
AsyncCallback callback)
+            throws Exception {
+        final SolverManager solverManager = getSolverManager(exchange);
+
+        if (body.getClass().isAnnotationPresent(PlanningSolution.class)) {
+            LOGGER.debug("Asynchronously solving problem: [{}] with id [{}]", 
body);
+            Long problemId = endpoint.getConfiguration().getProblemId();
+            if (isAsync(exchange)) {
+                executor.submit(() -> {
+                    try {
+                        // create a consumer for best solution
+                        OptaplannerSolution consumer = 
endpoint.getOrCreateSolution(problemId);
+                        consumer.bestSolution(body);
+                        // start solving :: Solver Job is a thread
+                        SolverJob solverJob = 
solverManager.solveAndListen(problemId, t -> body, consumer::bestSolution);
+                        // wait for result
+                        populateResultWithSolverManager(exchange, solverJob);
+                    } catch (Throwable e) {
+                        LOGGER.error("Asynchronously solving failed with 
SolverManager, for problemId ({})", problemId, e);
+                    } finally {
+                        callback.done(false);
+                    }
+                });
+            } else {
+                // no need for a consumer for sync call
+                SolverJob solverJob = solverManager.solve(problemId, body);
+                // wait for result
+                populateResultWithSolverManager(exchange, solverJob);
+                callback.done(true);
+            }
+        } else {
+            throw new Exception("Unsuported type");

Review comment:
       Can we make a better exception message what this means?

##########
File path: 
components/camel-optaplanner/src/main/java/org/apache/camel/component/optaplanner/OptaplannerSolution.java
##########
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.optaplanner;
+
+import java.beans.PropertyChangeListener;

Review comment:
       Can you please use something else as java.beans is some very old crap in 
Java that is bloated and may make quarkus pull in alot of extra classes.
   
   Instead add you own little event listener api

##########
File path: 
components/camel-optaplanner/src/main/java/org/apache/camel/component/optaplanner/OptaPlannerProducer.java
##########
@@ -113,6 +143,54 @@ public void run() {
         }
     }
 
+    /**
+     * Using SolverManager
+     *
+     * @param  exchange
+     * @param  body
+     * @throws Exception
+     */
+    private void processWithSolverManager(Exchange exchange, Object body, 
AsyncCallback callback)
+            throws Exception {
+        final SolverManager solverManager = getSolverManager(exchange);
+
+        if (body.getClass().isAnnotationPresent(PlanningSolution.class)) {
+            LOGGER.debug("Asynchronously solving problem: [{}] with id [{}]", 
body);
+            Long problemId = endpoint.getConfiguration().getProblemId();
+            if (isAsync(exchange)) {
+                executor.submit(() -> {
+                    try {
+                        // create a consumer for best solution
+                        OptaplannerSolution consumer = 
endpoint.getOrCreateSolution(problemId);
+                        consumer.bestSolution(body);
+                        // start solving :: Solver Job is a thread
+                        SolverJob solverJob = 
solverManager.solveAndListen(problemId, t -> body, consumer::bestSolution);
+                        // wait for result
+                        populateResultWithSolverManager(exchange, solverJob);
+                    } catch (Throwable e) {
+                        LOGGER.error("Asynchronously solving failed with 
SolverManager, for problemId ({})", problemId, e);
+                    } finally {
+                        callback.done(false);

Review comment:
       Yes this is correct, after we have the response we can tell Camel to 
continue by calling false on the callback

##########
File path: 
components/camel-optaplanner/src/main/java/org/apache/camel/component/optaplanner/OptaPlannerProducer.java
##########
@@ -113,6 +143,54 @@ public void run() {
         }
     }
 
+    /**
+     * Using SolverManager
+     *
+     * @param  exchange
+     * @param  body
+     * @throws Exception
+     */
+    private void processWithSolverManager(Exchange exchange, Object body, 
AsyncCallback callback)
+            throws Exception {
+        final SolverManager solverManager = getSolverManager(exchange);
+
+        if (body.getClass().isAnnotationPresent(PlanningSolution.class)) {
+            LOGGER.debug("Asynchronously solving problem: [{}] with id [{}]", 
body);
+            Long problemId = endpoint.getConfiguration().getProblemId();
+            if (isAsync(exchange)) {
+                executor.submit(() -> {
+                    try {
+                        // create a consumer for best solution
+                        OptaplannerSolution consumer = 
endpoint.getOrCreateSolution(problemId);
+                        consumer.bestSolution(body);
+                        // start solving :: Solver Job is a thread
+                        SolverJob solverJob = 
solverManager.solveAndListen(problemId, t -> body, consumer::bestSolution);
+                        // wait for result
+                        populateResultWithSolverManager(exchange, solverJob);
+                    } catch (Throwable e) {
+                        LOGGER.error("Asynchronously solving failed with 
SolverManager, for problemId ({})", problemId, e);
+                    } finally {
+                        callback.done(false);
+                    }
+                });
+            } else {
+                // no need for a consumer for sync call
+                SolverJob solverJob = solverManager.solve(problemId, body);
+                // wait for result
+                populateResultWithSolverManager(exchange, solverJob);
+                callback.done(true);

Review comment:
       return true to know that it was processed sync

##########
File path: 
components/camel-optaplanner/src/main/java/org/apache/camel/component/optaplanner/OptaPlannerProducer.java
##########
@@ -113,6 +143,54 @@ public void run() {
         }
     }
 
+    /**
+     * Using SolverManager
+     *
+     * @param  exchange
+     * @param  body
+     * @throws Exception
+     */
+    private void processWithSolverManager(Exchange exchange, Object body, 
AsyncCallback callback)
+            throws Exception {
+        final SolverManager solverManager = getSolverManager(exchange);
+
+        if (body.getClass().isAnnotationPresent(PlanningSolution.class)) {
+            LOGGER.debug("Asynchronously solving problem: [{}] with id [{}]", 
body);
+            Long problemId = endpoint.getConfiguration().getProblemId();
+            if (isAsync(exchange)) {
+                executor.submit(() -> {

Review comment:
       Here you need to return false after submit, as its another thread that 
continues this callback, so return false, and then return false from the 
process method that called this, then Camel knows its async now.
   
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to