0

I would like to pluck documents from a JSON payload and send them to different processors based on their location.

JSON:

{
    "email" : {
        "documents" : [{
                "name" : "Document 1",
                "id" : "1111"
            }, {
                "name" : "Document 2",
                "id" : "222"
            }
        ]
    },
    "sms" : {
        "documents" : [{
                "name" : "Document 3",
                "id" : "3333"
            }, {
                "name" : "Document 4",
                "id" : "4444"
            }
        ]
    }
}

I was thinking to achieve this by doing something like this:

from("servlet:///doc").unmarshal()
  .json(JsonLibrary.Jackson, DocumentRequest.class)
  .split().method("docSplit", "split")
  .choice()
    .when().header("mode").isEqualTo("email")
      .to("direct:email")
    .when().header("mode").isEqualTo("sms")
      .to("direct:sms");

My splitter can receive a DocumentRequest and pull out all the docs... but I do not know how to set the "mode" header for future routing.

How can I set the "mode" header?

Is there a better overall approach?

user897210
  • 109
  • 6

1 Answers1

1

You can always put custom split logic in a custom processor and use ProducerTemplate

For example:

    from("servlet:///doc").unmarshal()
              .json(JsonLibrary.Jackson, DocumentRequest.class)
              .process(new Processor()  {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        ProducerTemplate producer=exchange.getContext().createProducerTemplate();

                        String mode;
                        for (Document doc: // split and set mode logic goes here ) {
                            if (mode.compareToIgnoreCase("email") ==0) {
                                producer.sendBody("direct:email", doc);
                            } else 
                            if (mode.compareToIgnoreCase("sms") ==0) {
                                producer.sendBody("direct:sms", doc);
                            }
...
                        }
                    }
              });
Sergey
  • 1,332
  • 2
  • 18
  • 30