0

Currently, code similar to the following exists in one of our applications:

@Component
public class ProcessRequestImpl {

   private ExecutorService executorService;

   public processRequest(...) {
      // code to pre-process request

      executorService.execute(new Runnable() {
         public void run() {
            ProcessRequestImpl.this.doWork(...);
         }
      }
   }

   private void doWork(...) {
      // register in external file that request is being processed
      // call external service to handle request
   } 

}

The intent of this is to create a queue of requests to the external service. The external service may take some time to process each incoming request. After it handles each one, it will update the external file to register that the specific request has been processed.

ProcessRequestImpl itself is stateless, in that all state is set in the constructor and there is no external access to that state. The process() method is called by another component in the application.

If this were to be implemented in a Spring Integration application, which of the following two approaches would be best recommended:

  1. Keep the above code as is.
  2. Extract doWork(), into a separate endpoint, configure that endpoint to receive messages on a channel, and to use configuration to achieve the multi threading in place of the executor service.

Some of the reasons we are looking at Spring Integration are as follows:

  1. To remove the workflow logic from the code itself, so that the workflow and the chain of processing is evident on a higher level.
  2. To simplify each class, enhancing readability and testability.
  3. To avoid threading code if possible, and define that at a higher level of abstraction in configuration.

Given the sample code, could those goals be achieved using Spring Integration. Also, what would be an example of the DSL to achieve that.

Thanks

user1052610
  • 4,440
  • 13
  • 50
  • 101

1 Answers1

0

Something like

@Bean 
public IntegrationFlow flow() {
    return IntegrationFlows.from(SomeGatewayInterface.class)
          .handle("someBean", "preProcess")
          .channel(MessageChannels.executor(someTaskExecutorBean())
          .handle("someBean", "doWork")
          .get();

The argument passed to the gateway method become the payload of the preprocess method, which would return some object that becomes the message payload, which becomes the parameter passed to doWork.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179