0

Using JdbcOutboundGateway, how do I get the reply to be original payload?

Currently, the response payload is {UPDATED=1}. Neither reference document (Spring 5.2.x) nor the source code seems to offer an alternative.

Should I extend the class and override handleRequestMessage to return the original payload? The method is protected which reads to me as an invite to extend or is there a preferred 'pattern' in Spring Integration to handle {UPDATED=1} in the next @ServiceActivator and somehow restore the payload.

If someone is kind enough to respond, please give an example using configuration/annotations and not XML or DSL.

Edit

I ended up doing something like below. Is it advisable to do this? Am I missing something by not using the 'integration-jdbc' methods?

@Bean
@ServiceActivator(inputChannel="myIn", outputChannel="myOut")
public GenericHandler<String> saveToDb(@Autowired MyRepository myRepo) {
  return (payload, header) -> {
    MyClass x = (MyClass) headers.get("MyClassObject")
    myRepo.save(x);
    return payload;
  };
}
lafual
  • 683
  • 1
  • 7
  • 23
  • Any unrelated comments why not XML or DSL? – Artem Bilan Apr 27 '20 at 14:15
  • @Artem It is mainly from a standpoint of readability and probably ignorance. I started Spring on version 5 and was taught to use annotations. I was taught that XML was "old style". Then I started using Integration, who's documentation is heavily XML orientated. I assumed that this was just a hangover / backward compatibility to Spring 4 downwards. So I wanted to keep my code as "Java" like as possible. As for DSL, it just become very unreadable once you get into big flows. . – lafual Apr 27 '20 at 14:39
  • That's not correct assumption: the XML configuration is still in support and can be even combined with the annotation configuration. But yeah, I agree: if everything is Java & Annotations would be better to not bother yourself with XML. So, what is the problem with the Java DSL on the other side then? – Artem Bilan Apr 27 '20 at 14:41
  • @Artem Is there a pattern that one can follow when reading XML configs? If I look at an XML config; to my untrained eye there seems to be no correlation to the annotated version. Take for example this JdbcOutboundGateway. I assumed that it would be `@Gateway` but I saw a Gist by you which used `@ServiceActivator`! – lafual Apr 27 '20 at 14:48
  • See here: https://docs.spring.io/spring-integration/reference/html/overview.html#programming-tips – Artem Bilan Apr 27 '20 at 14:57
  • 1
    @ArtemBilan Having re-written my code as DSL, I am now a DSL convert :-) I needed far less `@Beans` to do the job as I could combine many actions. I didn't have to consider which annotation (e.g. `@ServiceActivator` `@Transformer` etc) was required. The auto-complete in the IDE made it more intuitive to program. (Thanks for your great work and to @GaryRussell) – lafual May 04 '20 at 05:59

1 Answers1

0

The simplest solution is to add a header enricher before the gateway and copy the payload into a header.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Another solution is like `PublishSubscribeChannel` with `JdbcMessageHandler` as a first subscriber and your `@ServiceActivator` as another one. This way your logic for original payload won't be called until `JdbcMessageHandler` finishes its job. – Artem Bilan Apr 27 '20 at 14:14
  • @Gary-Russell - thanks for your response. I edited my question with what I did so far. Is it legitimate or am I by passing some 'magic' in the JDBC Handlers? – lafual Apr 27 '20 at 15:52