gnodet commented on code in PR #22158: URL: https://github.com/apache/camel/pull/22158#discussion_r3000167808
########## components/camel-state-store/camel-state-store/src/main/java/org/apache/camel/component/statestore/StateStoreProducer.java: ########## @@ -0,0 +1,115 @@ +/* + * 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.statestore; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.support.DefaultProducer; + +/** + * Producer for the State Store component that performs key-value operations. + */ +public class StateStoreProducer extends DefaultProducer { + + private final StateStoreEndpoint endpoint; + + public StateStoreProducer(StateStoreEndpoint endpoint) { + super(endpoint); + this.endpoint = endpoint; + } + + @Override + public void process(Exchange exchange) throws Exception { + StateStoreOperations op = determineOperation(exchange); + if (op == null) { + throw new IllegalArgumentException( + "No operation specified. Set the operation via URI option or header " + StateStoreConstants.OPERATION); + } + + StateStoreBackend backend = endpoint.getBackend(); + long ttl = determineTtl(exchange); + Message message = exchange.getMessage(); + + switch (op) { + case put -> { + String key = requireKey(message); + Object value = message.getBody(); + Object previous = backend.put(key, value, ttl); + message.setBody(previous); + } + case putIfAbsent -> { + String key = requireKey(message); + Object value = message.getBody(); + Object existing = backend.putIfAbsent(key, value, ttl); + message.setBody(existing); Review Comment: The return-previous-value behavior follows `java.util.Map.put()` / `Map.putIfAbsent()` semantics: - **`put`** — returns the previous value (or `null` if key was new), same as `Map.put()` - **`putIfAbsent`** — returns the existing value if key already existed (meaning nothing was stored), or `null` if the value was stored successfully This is intentional — it's the only way to communicate the result back to the route, and it enables patterns like idempotent deduplication: ```java from("kafka:orders") .setHeader(StateStoreConstants.KEY, simple("${header.orderId}")) .to("state-store:processed?operation=putIfAbsent") .choice() .when(body().isNotNull()) // non-null means key already existed = duplicate .log("Duplicate order, skipping").stop() .end() .to("direct:process-order"); ``` This is consistent with how `camel-caffeine-cache` handles it (the `PUT` action also returns the previous value in the body). _Claude Code on behalf of Guillaume Nodet_ -- 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]
