简体   繁体   中英

Throttle in Apache Camel/Spring

How can I add throttle in all routes in camel

@Component
public class MyRestRoute extends RouteBuilder {

@Value("${spring.application.name}")
private String appName;

@Value("${spring.application.description}")
private String description;

@Value("${spring.application.version}")
private String appVersion;

@Override
public void configure() throws Exception {

    restConfiguration().apiContextRouteId("swagger").contextPath(System.getenv("CONTEXT_PATH"))
            .apiContextPath("/swagger").component("servlet")
            .apiProperty("api.title", appName)
            .apiProperty("api.description", description)
            .apiProperty("api.version", appVersion)
            .apiProperty("host", "localhost")
            .apiProperty("port", "8080")
            .apiProperty("schemes", "http");



    rest("/transfers/{transfer_id}")
            .post().type(Request.class).id("id-limits").description("transfer").bindingMode(RestBindingMode.auto)
            .skipBindingOnErrorCode(true)
            .param().name("transfer_id").type(RestParamType.path).description("transferId").endParam()
            .produces(MediaType.APPLICATION_JSON_VALUE)

            .to("direct:transferRoute);


    rest("/accounts")
            .get().id("id-limits").description("Get Accounts").bindingMode(RestBindingMode.auto)
            .skipBindingOnErrorCode(true)

            .param().name("account_id").type(RestParamType.query).description("account_id").endParam()
            .param().name("document").type(RestParamType.query).description("document").endParam()

            .produces(MediaType.APPLICATION_JSON_VALUE)
            .to("direct:accountsRoute));
}
}

That can receive more than a rest resource_path, how Can I insert throttle in all my main route. I know I can insert after the start of each route in .from("direct:transferRoute") and .from("direct:accountsRoute"), but I want to insert in all my resources generically. Can I do this in Camel, or maybe using spring is more safe?

As far as I know, I do not think it is possible to do it globally . I'm afraid you have to do it on a per-route basis.

As a (pretty stupid) workaround, you can yet imagine something like this.

public abstract class CustomRouteBuilder extends RouteBuilder {
  public RouteDefinition fromWithTrottle(String uri) {
    return this.fromWithTrottle(uri, 3, 10000); // default throttling
  }
  public RouteDefinition fromWithTrottle(String uri, int count, long period) {
    return super.from(uri)
       .throttle(count)
       .timePeriodMillis(period);
}

public class MyRestRoute extends CustomRouteBuilder {
  @Override
  public void configure() throws Exception {
     fromWithTrottle("direct:transferRoute")
       .to("mock:transfer");

     fromWithTrottle("direct:accountsRoute", 2, 12_000)
       .to("mock:accounts")
  }
}

也许你可以使用拦截器https://camel.apache.org/components/3.17.x/eips/intercept.html#_using_intercept

interceptFrom().throttle(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM