简体   繁体   中英

Understanding spring-integration service-activator

I was given a task to make some modification to an spring-integration project which was developed a decade ago and I have know clue of how it works so I've looked at some of the spring-integration tutorials, although I didnt understand it fully I got some basic knowledge of it. Now am trying to understand the below snippet of spring-integration configuration of spring.integration.version 2.2.4.RELEASE before making the changes

<!-- 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" />

Below are the correscponding classes to the above beans

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;
    }
}

So below is my understanding the above spring-integration configuration once the application starts (please correct if its wrong)
1. Spring initializes a proxy object for INetSuiteCurrencyPullService first
2. Then instatiates nsCurrencyPullService bean
3. Then invokes getCurrencies method of nsCurrencyPullService
4. And passes the output to saveCurrenciesToDB method of msSqlCurrencyPushService

Please help me with my below questions
So the above steps are performed only during the spring application startup?
Or is it done on regular intervals once the application is up?
If its performed on regular intervals where can I check the polling frequency of nsCurrencyPullService invokation?

So the above steps are performed only during the spring application startup?

The infrastructure (bean) creation is performed once, during the application context initialization. The components are wired together using MessageChannel s. When the gateway is called, the payload is wrapped in a message and sent to the channel. By default, channels are DirectChannel s, which means the service is invoked directly on the caller's thread.

The output of the first service is sent via the channel to the second service; again on the caller's thread. The result of that service is returned to the caller as the result of the method call.

polling frequency

There is no polling in this scenario; messages are sent into the Integration flow by the caller of the gateway.

A modern way to do the same would be with the DSL.

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

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