2013-04-03 36 views
1

我有一個遠程服務,我打電話來爲特定事件發生時加載產品的定價數據。一旦加載,產品定價將被廣播給其他消費者在其他地方處理。Spring Integration + @Aysnc - Gateway vs ServiceActivator

調用代碼並不關心響應 - 它是一個即忘即會,響應應用程序事件並觸發新的工作流。

爲了儘可能快地保持調用代碼,我想在這裏使用@Async,但我有混合的結果。

的基本流程是:

CallingCode -> ProductPricingGateway -> Aggregator -> BatchedFetchPricingTask 

這裏的異步設置:

<task:annotation-driven executor="executor" scheduler="scheduler"/> 
<task:scheduler id="scheduler" pool-size="1" /> 
<task:executor id="executor" keep-alive="30" pool-size="10-20" queue-capacity="500" rejection-policy="CALLER_RUNS" /> 

使用的其它兩個分量是一個@Gateway,其intiating碼的呼叫,和一個下游@ServiceActivator,位於聚合器後面。 (呼叫分成小組)。

public interface ProductPricingGateway {  
    @Gateway(requestChannel="product.pricing.outbound.requests") 
    public void broadcastPricing(ProductIdentifer productIdentifier); 
} 

// ...elsewhere... 
@Component 
public class BatchedFetchPricingTask { 
    @ServiceActivator(inputChannel="product.pricing.outbound.requests.batch") 
    public void fetchPricing(List<ProductIdentifer> identifiers) 
    { 
     // omitted 
    } 
} 

而其他相關intergation配置:

<int:gateway service-interface="ProductPricingGateway" 
    default-request-channel="product.pricing.outbound.requests" /> 

<int:channel id="product.pricing.outbound.requests" /> 
<int:channel id="product.pricing.outbound.requests.batch" /> 

我發現,如果我在@ServiceActivator方法聲明@Async,它工作正常。 但是,如果我在@Gateway方法(這看起來更合適的地方)聲明它,聚合器永遠不會被調用。

爲什麼?

回答

1

我很努力地看到@Async可以在這裏的任何地方工作,因爲出發點是當您的代碼調用ProductPricingGateway.broadcastPricing()方法時。

對於gw上的@Async,調度程序會發送什麼?

同樣,如果服務上有@Async,那麼調度程序通過identifiers會怎樣?

正確的方法是儘快去異步是使product.pricing.outbound.requests一個ExecutorChannel ...

http://static.springsource.org/spring-integration/reference/html/messaging-channels-section.html#executor-channel

http://static.springsource.org/spring-integration/reference/html/messaging-channels-section.html#channel-configuration-executorchannel

...其中調用線程的手離開消息任務執行者。

相關問題