[英]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.