【问题标题】:Understanding spring-integration service-activator了解spring-integration service-activator
【发布时间】:2019-06-29 17:37:53
【问题描述】:

我的任务是对十年前开发的 spring-integration 项目进行一些修改,我知道它是如何工作的,所以我查看了一些 spring-integration 教程,虽然我没有完全理解它 我对它有了一些基本的了解。现在我试图在进行更改之前了解spring.integration.version 2.2.4.RELEASEspring-integration 配置的以下sn-p

<!-- Currency Pull from NetSuite -->
<int:gateway id="currencySyncGateway" service-interface="com.integration.service.INetSuiteCurrencyPullService" default-request-channel="currenciesFromNetSuite" />


<bean id="nsCurrencyPullService" class="com.integration.service.impl.NetSuiteCurrencyPullService" />
<int:channel id="currenciesFromNetSuite" />
<int:service-activator input-channel="currenciesFromNetSuite" ref="nsCurrencyPullService" method="getCurrencies" output-channel="pushCurrenciesToDB" />

<bean id="msSqlCurrencyPushService" class="com.integration.service.impl.MSSQLCurrencyPushService" />
<int:channel id="pushCurrenciesToDB" />
<int:service-activator input-channel="pushCurrenciesToDB" ref="msSqlCurrencyPushService" method="saveCurrenciesToDB" />

以下是上述bean对应的类

INetSuiteCurrencyPullService

public interface INetSuiteCurrencyPullService {

    List<Currency> getCurrencies(String in);

}

NetSuiteCurrencyPullService

public class NetSuiteCurrencyPullService implements INetSuiteCurrencyPullService {

    @Autowired
    INetSuiteClient restletClient;

    @Override
    public List<Currency> getCurrencies(String in) {
        LOGGER.info("Retrieving currencies from NetSuite...");
        PullCurrenciesRestletResponse response = restletClient.pullCurrencies();
        LOGGER.debug("Restlet response: {}", response);

        if ("SUCCESS".equals(response.getError().getCode())) {
            LOGGER.info("Received restlet response: executionTimeMillis=" + response.getExecutionTimeMillis() + ", count=" + response.getCurrencies().size());
            return response.getCurrencies();
        } else {
            String msg = "Error retrieving currencies from NetSuite: " + response.getError().getMessage();
            LOGGER.error(msg);
            throw new RuntimeException(msg);
        }
    }

}

MSSQLCurrencyPushService

public class MSSQLCurrencyPushService implements IMSSQLCurrencyPushService {

    @Autowired
    CurrencyConversionRepository currencyConversionRepository;

    @Override
    public List<Currency> saveCurrenciesToDB(List<Currency> in) {
        // logic to save Currency in DB     
        return in;
    }
}

所以下面是我对上述spring-integration应用程序启动后的配置的理解(如果错误请更正)
1.Spring首先为INetSuiteCurrencyPullService初始化一个代理对象
2.然后实例化nsCurrencyPullService bean
3.然后调用nsCurrencyPullServicegetCurrencies方法
4.并将输出传递给msSqlCurrencyPushServicesaveCurrenciesToDB方法

请帮助我解决以下问题
那么上面的步骤只在spring应用启动的时候进行呢?
还是在应用程序启动后定期执行?
如果它定期执行,我在哪里可以检查nsCurrencyPullService 调用的轮询频率?

【问题讨论】:

    标签: java spring spring-integration channel


    【解决方案1】:

    那么上面的步骤只在spring应用启动的时候执行?

    基础设施(bean)创建在应用程序上下文初始化期间执行一次。这些组件使用MessageChannels 连接在一起。当网关被调用时,有效载荷被包装在一条消息中并发送到通道。默认情况下,通道是DirectChannels,这意味着服务直接在调用者的线程上调用。

    第一个服务的输出通过通道发送到第二个服务;再次在调用者的线程上。该服务的结果作为方法调用的结果返回给调用者。

    轮询频率

    在这种情况下没有轮询;消息由网关的调用者发送到集成流中。

    使用 DSL 来做同样的事情的现代方法。

    @Bean
    public IntegrationFlow() {
        return IntegrationFlows.from(INetSuiteCurrencyPullService.class)
             .handle("bean1", "method1")
             .handle("bean2", "method2")
             .get();
    }
    

    【讨论】:

    • 感谢您的回复,您说"When the gateway is called..." 在我的应用程序中,网关currencySyncGateway 没有在spring-integration-context.xml 以外的任何地方被调用/使用,那么这个网关是如何被调用的?
    • 它必须由 bean 名称“currencySyncGateway”引用或键入其他 bean 并在那里调用。否则什么都不会发生,因为该配置是完全被动的。
    猜你喜欢
    • 2019-05-06
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多