This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch fix/CAMEL-23676 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2fd4f4320d7d25ab0ea9e01bb418717620a19fbe Author: Claus Ibsen <[email protected]> AuthorDate: Wed Jun 3 18:22:02 2026 +0200 CAMEL-23676: camel-nats - Only send reply when exchange pattern is InOut Co-Authored-By: Claude Opus 4.6 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../apache/camel/component/nats/NatsConsumer.java | 11 +++- .../integration/NatsConsumerReplyToInOnlyIT.java | 64 ++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java index 2a1ce6df6113..ba52eae79fa6 100644 --- a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java +++ b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java @@ -36,6 +36,7 @@ import io.nats.client.api.AckPolicy; import io.nats.client.api.ConsumerConfiguration; import io.nats.client.api.StreamConfiguration; import org.apache.camel.Exchange; +import org.apache.camel.ExchangePattern; import org.apache.camel.Processor; import org.apache.camel.spi.HeaderFilterStrategy; import org.apache.camel.support.DefaultConsumer; @@ -346,6 +347,11 @@ public class NatsConsumer extends DefaultConsumer { } try { exchange.getIn().setBody(msg.getData()); + // auto-set InOut pattern when message has a replyTo (request/reply) + if (msg.getReplyTo() != null && !NatsConsumingTask.this.configuration.isReplyToDisabled() + && !exchange.getPattern().isOutCapable()) { + exchange.setPattern(ExchangePattern.InOut); + } exchange.getIn().setHeader(NatsConstants.NATS_REPLY_TO, msg.getReplyTo()); exchange.getIn().setHeader(NatsConstants.NATS_SID, msg.getSID()); exchange.getIn().setHeader(NatsConstants.NATS_SUBJECT, msg.getSubject()); @@ -384,9 +390,10 @@ public class NatsConsumer extends DefaultConsumer { NatsConsumer.this.processor.process(exchange); - // is there a reply? + // is there a reply? only send reply if exchange pattern supports output (InOut) if (!NatsConsumingTask.this.configuration.isReplyToDisabled() - && msg.getReplyTo() != null && msg.getConnection() != null) { + && msg.getReplyTo() != null && msg.getConnection() != null + && exchange.getPattern().isOutCapable()) { final Connection con = msg.getConnection(); final byte[] data = exchange.getMessage().getBody(byte[].class); if (data != null) { diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/integration/NatsConsumerReplyToInOnlyIT.java b/components/camel-nats/src/test/java/org/apache/camel/component/nats/integration/NatsConsumerReplyToInOnlyIT.java new file mode 100644 index 000000000000..f259f66546be --- /dev/null +++ b/components/camel-nats/src/test/java/org/apache/camel/component/nats/integration/NatsConsumerReplyToInOnlyIT.java @@ -0,0 +1,64 @@ +/* + * 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.nats.integration; + +import org.apache.camel.EndpointInject; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.jupiter.api.Test; + +public class NatsConsumerReplyToInOnlyIT extends NatsITSupport { + + @EndpointInject("mock:result") + protected MockEndpoint mockResultEndpoint; + + @EndpointInject("mock:reply") + protected MockEndpoint mockReplyEndpoint; + + @Test + public void testInOnlyNoReply() throws Exception { + mockResultEndpoint.expectedBodiesReceived("World"); + // reply endpoint should NOT receive any message when exchange pattern is InOnly + mockReplyEndpoint.expectedMessageCount(0); + + template.sendBody("direct:send", "World"); + + mockResultEndpoint.setAssertPeriod(5000); + mockResultEndpoint.assertIsSatisfied(); + mockReplyEndpoint.setAssertPeriod(2000); + mockReplyEndpoint.assertIsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:send") + .to("nats:testInOnly?replySubject=myReplyInOnly&flushConnection=true"); + + from("nats:testInOnly?flushConnection=true&exchangePattern=InOnly") + .to(mockResultEndpoint) + .convertBodyTo(String.class) + .setBody().simple("Bye ${body}"); + + from("nats:myReplyInOnly") + .to("mock:reply"); + } + }; + } +}
