Billybong
Billybong

Reputation: 707

How to respond with multiple messages in a sync (two-way) camel route?

I'd like to model an Apache Camel route that accepts tcp requests containing xml messages.

Each message may result in a multitude of responses which should be sent back on the incoming socket. I've played around with the camel-netty component in sync mode which works for single messages. But is it possible to send back multiple messages on the socket? Basically a split before the return.

from(String.format("netty:tcp://0.0.0.0:%s?sync=true&decoders=#length-decoder,#string-decoder&encoders=#string-encoder,#length-encoder", INBOUND_PORT))
            .id("my-mock")
            .unmarshal(jaxbDataFormat)
            .process(exchange -> {
                List<String> responses = service.accept(exchange.getIn().getBody(MyXmlRootElement.class));
                exchange.getOut().setBody(responses);
            })
            .split().body()  //Split is not doing what it should. Should become multiple messages, and each should be returned with a delay
            .delay(2000);

My messages are length-encoded containing an integer at first 4 bytes specifying the length of each individual message.

In my case the exception is IllegalArgument, stating that the endpoint does not support ArrayList as the payload.

Caused by: [java.lang.IllegalArgumentException - unsupported message type: class java.util.ArrayList]
at org.apache.camel.component.netty.handlers.ServerResponseFutureListener.operationComplete(ServerResponseFutureListener.java:53) ~[camel-netty-2.16.0.jar:2.16.0]
at org.jboss.netty.channel.DefaultChannelFuture.notifyListener(DefaultChannelFuture.java:409) [netty-3.10.4.Final.jar:na]
at org.jboss.netty.channel.DefaultChannelFuture.notifyListeners(DefaultChannelFuture.java:395) [netty-3.10.4.Final.jar:na]

Cheers.

Upvotes: 0

Views: 1332

Answers (2)

Souciance Eqdam Rashti
Souciance Eqdam Rashti

Reputation: 3191

I have designed this as well for single messages and that works. For multiple response messages, you could try to aggregate them as one and send that back to the client. Assuming off course aggregation is possible in your case.

Upvotes: 0

Claus Ibsen
Claus Ibsen

Reputation: 55525

That is not how its designed, the sync option on netty is for sending one response message when the route ends.

Upvotes: 1

Related Questions