2

I have a situation where I need applications to send messages to a message broker. Another client listening to the broker must then consume messages off the queue, determine what type of message they are, and pass the message off to the appropriate handler.

For instance if a Fizz POJO is serialized as JSON and then sent to the broker, the other process must consume it, deserialize it from JSON back into a Fizz instance, and then know to pass the Fizz off to a FizzHandler processor. Same for a Buzz message: it should be deserialized back into a Buzz and sent to the BuzzHandler, etc.

I believe the pseudo-code for the route should look something like this:

from(broker)
    .unmarshal().json(JsonLibrary.Gson)
    .dynamicRouter(someMechanismForDeterminingHandler)

I believe a dynamic router is an appropriate processor for this problem, but not being an EIP expert, I may be out of my element.

The two big problems here:

  • How could Camel-GSON know that one type of JSON represents a Fizz object, whereas another string of JSON represents a Buzz object?
  • What EIP/Camel DSL/processor should be used to route the deserialized message off to the correct handler?
DirtyMikeAndTheBoys
  • 1,077
  • 3
  • 15
  • 29

1 Answers1

2

You need to know what class to unmarshal a certain string to in advance. Setup a data format and refer to it wherever you want to unmarshall to Fizz. Pojo -> JSON is easier since gson can figure the format you by looking at the object at hands.

GsonDataFormat json2Fizz = new GsonDataFormat(Fizz.class);
GsonDataFormat json2Buzz = new GsonDataFormat(Buzz.class);

Probably the best approach is to do the routing before unmarshal. Use the Content based router, which gives easy to follow routing.

If you have something in the json itself that makes you able to identify a Fizz or a Buzz, you can use the expression language JsonPath that allows you to route/filter etc directly on the json string. If not, you may want to pass along a header that somehow states the message type which you can route on.

Petter Nordlander
  • 22,053
  • 5
  • 50
  • 84