[英]Rate limit the messages using Spring integration
我是Spring集成的新手。
我们有一个REST应用程序,它接收的消息太多(每分钟6000条消息),超出了数据库的处理能力。 所以我想将请求的速率限制为每15秒500条消息(每分钟2000条)。 我正在使用队列通道来实现这一目标。
一段时间后,该应用程序将创建30,000+个Java线程。 同样,队列通道所容纳的消息数量超过队列容量中提到的数量。
如何减少线程数并限制队列中的消息?
集成上下文xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-5.0.xsd">
<!-- Endpoint -->
<int:gateway service-interface="com.ratelimiter.PrintGateway" default-request-channel="inputChannel">
<int:method name="print"/>
</int:gateway>
<!-- Channel -->
<int:channel id="inputChannel">
<int:queue capacity="30000"/>
</int:channel>
<!-- Endpoint -->
<int:service-activator ref="receiver" input-channel="inputChannel" method="save">
<int:poller fixed-rate="15" time-unit="SECONDS" max-messages-per-poll="500"></int:poller>
</int:service-activator>
<!-- Spring Bean -->
<bean id="receiver" class="com.ratelimiter.saveToDataStore"/>
</beans>
PrintGateway界面:
public interface PrintGateway {
public Future<Message<String>> print(Message<?> message);
}
由于您的网关签名将返回Future<Message<String>>
,因此将其视为异步网关: https : //docs.spring.io/spring-integration/docs/5.0.6.RELEASE/reference/html/消息的端点,chapter.html#异步网关
默认情况下,它使用
private volatile AsyncTaskExecutor asyncExecutor = new SimpleAsyncTaskExecutor();
这实际上为每个新消息启动了一个新线程。 重要的是:它等待答复完成该Future
。 根据您的代码,将不会有任何答复,因此,网关中的线程将长时间不等待任何内容。
您应该考虑将网关的签名更改为void
返回类型。 这样,您确实会发送并忘记。 没有多余的后台线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.