2

I have a system that I need to integrate with that needs files dropped in one folder and it copies that file to a "success" folder or an "error" folder depending on if it was processed correctly. I currently have a flow defined in DSL like so

return f -> f.channel("orders.in")
       .transform(Transformers.marshaller(marshaller(), resultTransformer(), true))
       .handle(Files.outboundAdapter(properties.getInputDirectory()));

With a gateway defined as

@Gateway(requestChannel="orders.in")
public void submitOrder(Order order);

What I would like is to have a response instead public String submitOrder(Order order) where the returned String is an order number if sucessful and null if failed. I've been playing around for a few hours now and am stumped on how to accomplish this without introducing AMQ or something but feel like it should be possible. I don't even know if this is how I'll end up using it as I might handle the responses async later and notify the user through other means, but am stubborn and wanted to try and get it to work this way first if possible. Any help appreciated.

Andrew Wynham
  • 2,310
  • 21
  • 25

1 Answers1

1

You subscribe a second consumer to the channel between the transformer and file adapter...

.transform(...)
.publishSubscribeChannel(s -> s
     .subscribe(f -> f.handle(...))
     .subscribe(f -> f ...)); // produce a reply here

The second subscriber will be invoked after a successful write the file and whatever is produced by the second subscriber will be returned to the @Gateway as the method result.

@Gateway(requestChannel="orders.in")
public String submitOrder(Order order);

By default, a failure (exception) will be thrown to the gateway caller. To get null after a failure, you would have to handle the error on the gateway's error channel and have no return value from that flow.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • thanks for looking. The exception is not a problem, actually better that way. I don't know how to work the multiple `FileReadingMessageSource` into the flow though given your example. Somehow I need to tell the flow "write xml to dirA, wait, check dirB for xml file and return success if found, check dirC for file and return error if found". All examples with `FileReadingMessageSource` tend to start a flow instead of what I'm dealing with. – Andrew Wynham Jul 07 '15 at 15:42
  • ? this is a different question; your original question didn't mention anything about reading files. You can write whatever logic you want into a message handler. Yes, in the framework itself, `FileReadingMessageSource`s are used in polled inbound adapters, but there's no reason you can't invoke one directly from a message handler downstream. – Gary Russell Jul 07 '15 at 15:49
  • Sorry, I was shooting for as much brevity as possible in my question and thought the reading of the files that are copied was implied in the first sentence. I had started down the path you mentioned with a custom handler but that "felt" wrong to me as I was trying to not reinvent anything and keep my flow free of custom code. Do you think that's my only option given this use case? – Andrew Wynham Jul 07 '15 at 16:40
  • With (S)FTP we have an outbound gateway for request/response operations (list, get, put etc). We don't have an equivalent for the file system, just inbound and outbound channel adapters. Feel free to open an 'improvement' [JIRA Issue](https://jira.spring.io/browse/INT) and we'll see if we can squeeze something in. – Gary Russell Jul 07 '15 at 17:24
  • Thanks, I RTM over and over but wanted to make sure I didn't miss something. Accepting answer as it covers all of the "flow stuff" :) – Andrew Wynham Jul 07 '15 at 18:22