Camel with JMS Transaction and REST post call












0














I need to read a message from a queue and call a REST service doing a post.



Then, I was looking about the JMS transaction with Camel, it looks like you can set a maximumRedeliveries to process the queue message again, so rolling back the transaction in case of a failure, I was wondering how it can work if in the same camel route we have to call a REST service to post something, how that part can be rollback??



maxDelivery conf:



errorHandler(new TransactionErrorHandlerBuilder()
.loggingLevel(LoggingLevel.ERROR)
.useOriginalMessage()
.maximumRedeliveries(2)
.logHandled(false)
.logExhausted(true)
);


Pseudo code for the route:



//Reading message from the queue
from("activemq:AMQ.App.EMC2.In.PMQueue?jmsMessageType=Bytes").
transacted().
unmarshal(jaxbDataFormat).bean(pmMessageEnricher).
to("direct:start-post");

//Then doing the post
from("direct:start-post").
setHeader(Exchange.HTTP_METHOD, constant("POST")).
setHeader(Exchange.CONTENT_TYPE, constant("application/json")).
setBody(constant(pmMessageEnricher.toJson())).
to("http://xxx").
to("direct:start-somethingelse");

//Then doing something else
from("direct:start-somethingelse").
blabla...


Let say an exception occurs in the start-somethingelse, How the REST post call can be roll backed ? since we call an external service in a stateless way.










share|improve this question





























    0














    I need to read a message from a queue and call a REST service doing a post.



    Then, I was looking about the JMS transaction with Camel, it looks like you can set a maximumRedeliveries to process the queue message again, so rolling back the transaction in case of a failure, I was wondering how it can work if in the same camel route we have to call a REST service to post something, how that part can be rollback??



    maxDelivery conf:



    errorHandler(new TransactionErrorHandlerBuilder()
    .loggingLevel(LoggingLevel.ERROR)
    .useOriginalMessage()
    .maximumRedeliveries(2)
    .logHandled(false)
    .logExhausted(true)
    );


    Pseudo code for the route:



    //Reading message from the queue
    from("activemq:AMQ.App.EMC2.In.PMQueue?jmsMessageType=Bytes").
    transacted().
    unmarshal(jaxbDataFormat).bean(pmMessageEnricher).
    to("direct:start-post");

    //Then doing the post
    from("direct:start-post").
    setHeader(Exchange.HTTP_METHOD, constant("POST")).
    setHeader(Exchange.CONTENT_TYPE, constant("application/json")).
    setBody(constant(pmMessageEnricher.toJson())).
    to("http://xxx").
    to("direct:start-somethingelse");

    //Then doing something else
    from("direct:start-somethingelse").
    blabla...


    Let say an exception occurs in the start-somethingelse, How the REST post call can be roll backed ? since we call an external service in a stateless way.










    share|improve this question



























      0












      0








      0







      I need to read a message from a queue and call a REST service doing a post.



      Then, I was looking about the JMS transaction with Camel, it looks like you can set a maximumRedeliveries to process the queue message again, so rolling back the transaction in case of a failure, I was wondering how it can work if in the same camel route we have to call a REST service to post something, how that part can be rollback??



      maxDelivery conf:



      errorHandler(new TransactionErrorHandlerBuilder()
      .loggingLevel(LoggingLevel.ERROR)
      .useOriginalMessage()
      .maximumRedeliveries(2)
      .logHandled(false)
      .logExhausted(true)
      );


      Pseudo code for the route:



      //Reading message from the queue
      from("activemq:AMQ.App.EMC2.In.PMQueue?jmsMessageType=Bytes").
      transacted().
      unmarshal(jaxbDataFormat).bean(pmMessageEnricher).
      to("direct:start-post");

      //Then doing the post
      from("direct:start-post").
      setHeader(Exchange.HTTP_METHOD, constant("POST")).
      setHeader(Exchange.CONTENT_TYPE, constant("application/json")).
      setBody(constant(pmMessageEnricher.toJson())).
      to("http://xxx").
      to("direct:start-somethingelse");

      //Then doing something else
      from("direct:start-somethingelse").
      blabla...


      Let say an exception occurs in the start-somethingelse, How the REST post call can be roll backed ? since we call an external service in a stateless way.










      share|improve this question















      I need to read a message from a queue and call a REST service doing a post.



      Then, I was looking about the JMS transaction with Camel, it looks like you can set a maximumRedeliveries to process the queue message again, so rolling back the transaction in case of a failure, I was wondering how it can work if in the same camel route we have to call a REST service to post something, how that part can be rollback??



      maxDelivery conf:



      errorHandler(new TransactionErrorHandlerBuilder()
      .loggingLevel(LoggingLevel.ERROR)
      .useOriginalMessage()
      .maximumRedeliveries(2)
      .logHandled(false)
      .logExhausted(true)
      );


      Pseudo code for the route:



      //Reading message from the queue
      from("activemq:AMQ.App.EMC2.In.PMQueue?jmsMessageType=Bytes").
      transacted().
      unmarshal(jaxbDataFormat).bean(pmMessageEnricher).
      to("direct:start-post");

      //Then doing the post
      from("direct:start-post").
      setHeader(Exchange.HTTP_METHOD, constant("POST")).
      setHeader(Exchange.CONTENT_TYPE, constant("application/json")).
      setBody(constant(pmMessageEnricher.toJson())).
      to("http://xxx").
      to("direct:start-somethingelse");

      //Then doing something else
      from("direct:start-somethingelse").
      blabla...


      Let say an exception occurs in the start-somethingelse, How the REST post call can be roll backed ? since we call an external service in a stateless way.







      java apache-camel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 12:13

























      asked Nov 20 at 10:58









      Emilien Brigand

      2,40742329




      2,40742329
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Your doubt is correct. In case of a JMS transaction rollback, the POST request cannot be rolled back because the service provider is not part of the JMS transaction. The transaction is just between the JMS broker and the Camel JMS consumer (see also Camel transactional client).



          However, if you catch the processing error, you can apply the needed compensation logic. For example delete the already posted data with another request.



          By the way: don't confuse Camel redeliveries and broker redeliveries!



          Camel redeliveries are done by the Camel Errorhandler (not the broker). In your example it does up to 2 redeliveries. But be aware, that Camel redeliveries are not reprocessing the entire route, but only the failed processor.



          So if to("http://xxx") fails and the Camel Errorhandler does redeliveries, Camel retries only the to("http://xxx").



          In contrast, if your JMS transaction is rolled back, the broker redelivers the message to Camel and the entire route is processed again.



          Take care that you don't "mask" the JMS redelivery with your Camel Errorhandler.






          share|improve this answer



















          • 1




            Good amount of details here. You might also consider linking the official documentation on transactional clients and/or on error handlers
            – Roman Vottner
            Nov 21 at 14:47










          • Thanks for the hint, I added the links
            – burki
            Nov 21 at 15:29






          • 1




            i found something else about the redelivery (coming from this very useful link): "The redelivery in transacted mode is not handled by Camel but by the backing system (the transaction manager). In such cases you should resort to the backing system how to configure the redelivery" meaning if i have a transaction manager set with activeMq, and if I set my route and sub routes associated with .transacted(), the .maximumRedeliveries(2) is pointless, the redeliveries max count is coming from the activemq broker
            – Emilien Brigand
            Dec 18 at 14:09













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391511%2fcamel-with-jms-transaction-and-rest-post-call%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Your doubt is correct. In case of a JMS transaction rollback, the POST request cannot be rolled back because the service provider is not part of the JMS transaction. The transaction is just between the JMS broker and the Camel JMS consumer (see also Camel transactional client).



          However, if you catch the processing error, you can apply the needed compensation logic. For example delete the already posted data with another request.



          By the way: don't confuse Camel redeliveries and broker redeliveries!



          Camel redeliveries are done by the Camel Errorhandler (not the broker). In your example it does up to 2 redeliveries. But be aware, that Camel redeliveries are not reprocessing the entire route, but only the failed processor.



          So if to("http://xxx") fails and the Camel Errorhandler does redeliveries, Camel retries only the to("http://xxx").



          In contrast, if your JMS transaction is rolled back, the broker redelivers the message to Camel and the entire route is processed again.



          Take care that you don't "mask" the JMS redelivery with your Camel Errorhandler.






          share|improve this answer



















          • 1




            Good amount of details here. You might also consider linking the official documentation on transactional clients and/or on error handlers
            – Roman Vottner
            Nov 21 at 14:47










          • Thanks for the hint, I added the links
            – burki
            Nov 21 at 15:29






          • 1




            i found something else about the redelivery (coming from this very useful link): "The redelivery in transacted mode is not handled by Camel but by the backing system (the transaction manager). In such cases you should resort to the backing system how to configure the redelivery" meaning if i have a transaction manager set with activeMq, and if I set my route and sub routes associated with .transacted(), the .maximumRedeliveries(2) is pointless, the redeliveries max count is coming from the activemq broker
            – Emilien Brigand
            Dec 18 at 14:09


















          2














          Your doubt is correct. In case of a JMS transaction rollback, the POST request cannot be rolled back because the service provider is not part of the JMS transaction. The transaction is just between the JMS broker and the Camel JMS consumer (see also Camel transactional client).



          However, if you catch the processing error, you can apply the needed compensation logic. For example delete the already posted data with another request.



          By the way: don't confuse Camel redeliveries and broker redeliveries!



          Camel redeliveries are done by the Camel Errorhandler (not the broker). In your example it does up to 2 redeliveries. But be aware, that Camel redeliveries are not reprocessing the entire route, but only the failed processor.



          So if to("http://xxx") fails and the Camel Errorhandler does redeliveries, Camel retries only the to("http://xxx").



          In contrast, if your JMS transaction is rolled back, the broker redelivers the message to Camel and the entire route is processed again.



          Take care that you don't "mask" the JMS redelivery with your Camel Errorhandler.






          share|improve this answer



















          • 1




            Good amount of details here. You might also consider linking the official documentation on transactional clients and/or on error handlers
            – Roman Vottner
            Nov 21 at 14:47










          • Thanks for the hint, I added the links
            – burki
            Nov 21 at 15:29






          • 1




            i found something else about the redelivery (coming from this very useful link): "The redelivery in transacted mode is not handled by Camel but by the backing system (the transaction manager). In such cases you should resort to the backing system how to configure the redelivery" meaning if i have a transaction manager set with activeMq, and if I set my route and sub routes associated with .transacted(), the .maximumRedeliveries(2) is pointless, the redeliveries max count is coming from the activemq broker
            – Emilien Brigand
            Dec 18 at 14:09
















          2












          2








          2






          Your doubt is correct. In case of a JMS transaction rollback, the POST request cannot be rolled back because the service provider is not part of the JMS transaction. The transaction is just between the JMS broker and the Camel JMS consumer (see also Camel transactional client).



          However, if you catch the processing error, you can apply the needed compensation logic. For example delete the already posted data with another request.



          By the way: don't confuse Camel redeliveries and broker redeliveries!



          Camel redeliveries are done by the Camel Errorhandler (not the broker). In your example it does up to 2 redeliveries. But be aware, that Camel redeliveries are not reprocessing the entire route, but only the failed processor.



          So if to("http://xxx") fails and the Camel Errorhandler does redeliveries, Camel retries only the to("http://xxx").



          In contrast, if your JMS transaction is rolled back, the broker redelivers the message to Camel and the entire route is processed again.



          Take care that you don't "mask" the JMS redelivery with your Camel Errorhandler.






          share|improve this answer














          Your doubt is correct. In case of a JMS transaction rollback, the POST request cannot be rolled back because the service provider is not part of the JMS transaction. The transaction is just between the JMS broker and the Camel JMS consumer (see also Camel transactional client).



          However, if you catch the processing error, you can apply the needed compensation logic. For example delete the already posted data with another request.



          By the way: don't confuse Camel redeliveries and broker redeliveries!



          Camel redeliveries are done by the Camel Errorhandler (not the broker). In your example it does up to 2 redeliveries. But be aware, that Camel redeliveries are not reprocessing the entire route, but only the failed processor.



          So if to("http://xxx") fails and the Camel Errorhandler does redeliveries, Camel retries only the to("http://xxx").



          In contrast, if your JMS transaction is rolled back, the broker redelivers the message to Camel and the entire route is processed again.



          Take care that you don't "mask" the JMS redelivery with your Camel Errorhandler.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 at 15:29

























          answered Nov 21 at 13:54









          burki

          1,6281415




          1,6281415








          • 1




            Good amount of details here. You might also consider linking the official documentation on transactional clients and/or on error handlers
            – Roman Vottner
            Nov 21 at 14:47










          • Thanks for the hint, I added the links
            – burki
            Nov 21 at 15:29






          • 1




            i found something else about the redelivery (coming from this very useful link): "The redelivery in transacted mode is not handled by Camel but by the backing system (the transaction manager). In such cases you should resort to the backing system how to configure the redelivery" meaning if i have a transaction manager set with activeMq, and if I set my route and sub routes associated with .transacted(), the .maximumRedeliveries(2) is pointless, the redeliveries max count is coming from the activemq broker
            – Emilien Brigand
            Dec 18 at 14:09
















          • 1




            Good amount of details here. You might also consider linking the official documentation on transactional clients and/or on error handlers
            – Roman Vottner
            Nov 21 at 14:47










          • Thanks for the hint, I added the links
            – burki
            Nov 21 at 15:29






          • 1




            i found something else about the redelivery (coming from this very useful link): "The redelivery in transacted mode is not handled by Camel but by the backing system (the transaction manager). In such cases you should resort to the backing system how to configure the redelivery" meaning if i have a transaction manager set with activeMq, and if I set my route and sub routes associated with .transacted(), the .maximumRedeliveries(2) is pointless, the redeliveries max count is coming from the activemq broker
            – Emilien Brigand
            Dec 18 at 14:09










          1




          1




          Good amount of details here. You might also consider linking the official documentation on transactional clients and/or on error handlers
          – Roman Vottner
          Nov 21 at 14:47




          Good amount of details here. You might also consider linking the official documentation on transactional clients and/or on error handlers
          – Roman Vottner
          Nov 21 at 14:47












          Thanks for the hint, I added the links
          – burki
          Nov 21 at 15:29




          Thanks for the hint, I added the links
          – burki
          Nov 21 at 15:29




          1




          1




          i found something else about the redelivery (coming from this very useful link): "The redelivery in transacted mode is not handled by Camel but by the backing system (the transaction manager). In such cases you should resort to the backing system how to configure the redelivery" meaning if i have a transaction manager set with activeMq, and if I set my route and sub routes associated with .transacted(), the .maximumRedeliveries(2) is pointless, the redeliveries max count is coming from the activemq broker
          – Emilien Brigand
          Dec 18 at 14:09






          i found something else about the redelivery (coming from this very useful link): "The redelivery in transacted mode is not handled by Camel but by the backing system (the transaction manager). In such cases you should resort to the backing system how to configure the redelivery" meaning if i have a transaction manager set with activeMq, and if I set my route and sub routes associated with .transacted(), the .maximumRedeliveries(2) is pointless, the redeliveries max count is coming from the activemq broker
          – Emilien Brigand
          Dec 18 at 14:09




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391511%2fcamel-with-jms-transaction-and-rest-post-call%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga