ramu11 commented on code in PR #25778: URL: https://github.com/apache/camel/pull/25778#discussion_r3869550039
########## components/camel-quickjs/src/main/java/org/apache/camel/language/quickjs/QuickjsLanguage.java: ########## @@ -0,0 +1,190 @@ +/* + * 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.language.quickjs; + +import java.io.ByteArrayOutputStream; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.atomic.AtomicInteger; + +import io.roastedroot.quickjs4j.core.Engine; +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.Expression; +import org.apache.camel.Predicate; +import org.apache.camel.Service; +import org.apache.camel.spi.ScriptingLanguage; +import org.apache.camel.spi.annotations.Language; +import org.apache.camel.support.TypedLanguageSupport; + +/** + * Camel expression language for JavaScript via <a href="https://github.com/roastedroot/quickjs4j">QuickJS4J</a>. + * + * <p> + * Scripts see only JSON-serializable data bindings: {@code body}, {@code headers}, {@code properties}, and + * {@code exchangeId}. Live {@code Exchange}, {@code Message}, and {@code CamelContext} objects are not bound, so + * {@code exchange.getMessage()} is a JavaScript {@code ReferenceError} rather than Java interop. + * </p> + */ +@Language("quickjs") +public class QuickjsLanguage extends TypedLanguageSupport implements ScriptingLanguage, Service { + + private final AtomicInteger generation = new AtomicInteger(); + private final ConcurrentLinkedQueue<Engine> engines = new ConcurrentLinkedQueue<>(); + private final ThreadLocal<EngineState> engine = new ThreadLocal<>(); + + /** + * Helper for use in the Java route DSL, e.g. {@code .filter(QuickjsLanguage.quickjs("body == 'Hello'"))}. + */ + public static QuickjsExpression quickjs(String script) { + return new QuickjsExpression(script); + } + + @Override + public void start() { + // Engines are created lazily per worker thread. + } + + @Override + public void stop() { + // Invalidate ThreadLocal caches on other worker threads before closing so a later + // get() cannot reuse a closed Engine (ThreadLocal.remove() only affects this thread). + generation.incrementAndGet(); + Engine next; + while ((next = engines.poll()) != null) { Review Comment: Addressed. Engine registration and stop() cleanup are now synchronized with a ReentrantLock, preventing an engine from being added after the shutdown drain. Added a lifecycle test covering concurrent evaluation and shutdown. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
