[ 
https://issues.apache.org/jira/browse/CAMEL-22705?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Claus Ibsen updated CAMEL-22705:
--------------------------------
    Issue Type: Improvement  (was: Bug)

> Camel-Nats: Reuse existing stream for consumers
> -----------------------------------------------
>
>                 Key: CAMEL-22705
>                 URL: https://issues.apache.org/jira/browse/CAMEL-22705
>             Project: Camel
>          Issue Type: Improvement
>          Components: camel-nats
>    Affects Versions: 4.16.0
>            Reporter: Vineet Saurabh
>            Priority: Major
>             Fix For: 4.17.0
>
>
> The NATS JetStream Consumer setup logic incorrectly attempts to create a 
> stream using {{jsm.addStream()}} every time the consumer starts. If a stream 
> with the same name exists but has a different configuration (e.g., subject 
> list, storage type, or retention policy), this operation fails, preventing 
> the consumer from subscribing.
> h4. Steps to Reproduce
>  # Start the application with a JetStream Consumer configured for stream 
> *{{MY_STREAM}}* and subject {*}{{topic.A}}{*}.
>  # Stop the application.
>  # Modify the consumer configuration (or have an external system create) 
> stream *{{MY_STREAM}}* with subjects *{{topic.B}}* (or a different subject 
> set than in step 1).
>  # Restart the application with the consumer configured for stream 
> *{{MY_STREAM}}* and subject {*}{{topic.A}}{*}.
> h4. 🔴 Actual Result
> The consumer fails to start and shuts down. The application logs the 
> following error:
> io.nats.client.JetStreamApiException: stream name already in use with a 
> different configuration [10058]
> h4. 🟢 Expected Result
> The consumer should successfully detect that stream *{{MY_STREAM}}* already 
> exists and proceed with the subscription setup without attempting to re-add 
> or redefine the stream.
> h2. Proposed Fix
> Modify the stream setup logic in {{setupJetStreamConsumer}} to use the 
> *check-then-create* pattern, leveraging the approach from the official NATS 
> Java client utilities to gracefully handle the "stream not found" condition.
> h3. Implementation Steps
>  # *Introduce Stream Existence Check:* Use a helper method (similar to 
> {{{}streamExists{}}}) to safely determine if the stream exists.
>  # *Conditional Creation:* Only call {{jsm.addStream()}} if the existence 
> check returns {{{}false{}}}.
> {code:java}
> // Method logic (similar to NatsJsUtils) should be available:
> public static boolean streamExists(JetStreamManagement jsm, String 
> streamName) throws IOException, JetStreamApiException {
>     try {
>         jsm.getStreamInfo(streamName);
>         return true;
>     } catch (JetStreamApiException jsae) {
>         // NATS uses HTTP status codes for API errors. 404 is Not Found.
>         if (jsae.getErrorCode() == 404) {
>             return false;
>         }
>         throw jsae;
>     }
> } {code}
> Note: While this pattern of using {{try-catch}} for resource existence 
> checking violates the principle of *Effective Java, Item 69* ("Use exceptions 
> only for exceptional conditions"), it is the necessary and idiomatic 
> workaround for handling the {{404}} (Not Found) status returned as a 
> {{JetStreamApiException}} by the NATS Java Client API.
>  
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to