Ramesh Mohan Reddy
Ramesh Mohan Reddy

Reputation: 197

Throttling by rejecting based on response payload size in Apache Camel

I have a requirement where our application based out of Camel can get huge payloads (50+mb) responses too. We need a way to reject such huge responses. I couldn't find a built-in feature within Camel that enables this.

At this point, I am planning to build a custom throttler as I have exhausted with searching in web too for external custom Camel components that offers this feature.

Before re-inventing wheel, I thought if someone already encountered this scenario and has had a solution that can be shared with me.

Thanks in advance.

Upvotes: 1

Views: 688

Answers (2)

vikingsteve
vikingsteve

Reputation: 40428

I think if you use a processor to simply get the size of the payload and set it on a property, you can then use that as a filter in your route.

Processer:

int payloadSize = exchange.getIn().getBody(byte[].class).length;
exchange.getIn().setHeader("payloadSize", payloadSize)

and then in your route:

.filter(header("payloadSize").isLessThan(50 * 1024 * 1024))

Upvotes: 1

Claus Ibsen
Claus Ibsen

Reputation: 55540

If enabling stream caching then it has a length method you can use to know the size of the payload, and then reject the message if its bigger than X.

Mind the throttler EIP pattern does not reject message, it only slows down messages. To reject a message use Content Based Router, Filter, etc.

You can also look at using interceptors and use a from and check the size and then if its to be rejected set some error code / error body, and then stop the routing.

Upvotes: 0

Related Questions