How to autowire beans inside a Spring Integration custom message handler? -


i wish create custom message handler use checkpoints in flows. besides, checkpoints stored in elasticsearch.

i created class checkpoint:

@component public class checkpoint {      public static final string task_header_key = "task";      public static checkpointmessagehandlerspec warn(string message) {         return new checkpointmessagehandlerspec(new checkpointhandler("warn", message));     } } // ... methods omitted: error, info etc 

next created checkpointmessagehandlerspec:

public class checkpointmessagehandlerspec extends messagehandlerspec<checkpointmessagehandlerspec, checkpointhandler> {      public checkpointmessagehandlerspec(checkpointhandler checkpointhandler) {         this.target = checkpointhandler;     }      public checkpointmessagehandlerspec apply(message<?> message) {         this.target.handlemessage(message);         return _this();     }      @override     protected checkpointhandler doget() {         throw new unsupportedoperationexception();     } } 

checkpointhandler, in class wish inject things, services or repositories spring data:

public class checkpointhandler extends integrationobjectsupport implements messagehandler {      private string status;     private string message;      // want inject services or repositories here      public checkpointhandler(string status, string message) {         this.status = status;         this.message = message;     }      @override     public void handlemessage(message<?> message) {         // test watch if have bean factory. null         this.getbeanfactory();          expression expression = expression_parser.parseexpression("'" + this.message + "'");          // here intend persist information of payload/headers spring-data-elasticsearch repository injected            object obj = expression.getvalue(message);     } } 

finally, example of use, inside flow:

@bean public integrationflow checkpointflow(checkpoint checkpoint) {     return integrationflows.from(http.inboundchanneladapter("/checkpointflow"))             .enrichheaders(collections.singletonmap(checkpoint.task_header_key, taskname))             .handle(new appendmessagehandler())             .wiretap(c -> c.handle(m -> checkpoint.warn("something happening here. payload: ' + payload.tostring() + '").apply(m)))             .handle(m -> log.info("[logging demo] {}" , m.getpayload()))             .get(); }  private class appendmessagehandler implements generichandler {      @override     public string handle(object payload, map headers) {         return new stringbuilder().append(testmessage).tostring();     } } 

what miss? possible that? had idea following question how create custom component , add flow in spring java dsl?

thanks!

bean can autowired, if are, well, beans.

let take code 1 more time!

c.handle(m -> checkpoint.warn("something happening here. payload: ' + payload.tostring() + '").apply(m)) 

the real bean here lambda :). sad, of course, not custom factory subsequent apply(). custom code invoked in target lambda each incoming message, without aware beanfactory.

to fix problem should use factory is:

.wiretap(c -> c.handle(checkpoint.warn("something happening here. payload: ' + payload.tostring() + '"))) 

and framework take care registration checkpointhandler bean and, therefore, autowiring.

as may guess don't need apply() method. because there need distinguish assemble phase when java dsl populates tree beans. initialization , registration phase, when tree parsed framework , beans registered in application context. and, finally, there runtime phase, when messages travel channel channel though message handler, transformers etc.


Comments

Popular posts from this blog

Multilayer CSV to filtered excel -

Listboxes in c# -

ios - Why must I define variables twice in the Header file? -