java - REST endpoints in Spring Integration make messaging channels multithreaded -
i have simple spring boot application, provides couple of restful endpoints , supposed drive sftp file being uploaded sftp server. requirement if there more 1 file, files should queued. expected achieve default behaviour of sftp spring integration workflow, read directchannel automatically queues files. test behaviour following:
- send big file, blocking channel while calling endpoint.
- send smaller file calling endpoint.
expected result: smaller file queued onto channel , processed after uploading of bigger file finished. actual result: new connection sftp server opened , smaller file uploaded there without being queued, while bigger file continues transmission.
there 2 files in application:
demoapplication.java
@springbootapplication @integrationcomponentscan @enableautoconfiguration(exclude={datasourceautoconfiguration.class}) public class demoapplication { public static void main(string[] args) { springapplication.run(demoapplication.class, args); } @bean public sessionfactory<lsentry> sftpsessionfactory() { defaultsftpsessionfactory factory = new defaultsftpsessionfactory(true); factory.sethost("localhost"); factory.setport(22); factory.setuser("tester"); factory.setpassword("password"); factory.setallowunknownkeys(true); return factory; } @bean @serviceactivator(inputchannel = "tosftpchannel") public messagehandler handler() { sftpmessagehandler handler = new sftpmessagehandler(sftpsessionfactory()); handler.setremotedirectoryexpression(new literalexpression("/")); return handler; } @messaginggateway public interface mygateway { @gateway(requestchannel = "tosftpchannel") void sendtosftp(file file); } }
democontroller.java
@restcontroller public class democontroller { @autowired mygateway gateway; @requestmapping("/sendfile") public void sendfile() { file file = new file("c:/smallfile.txt"); gateway.sendtosftp(file); } @requestmapping("/sendbigfile") public void sendbigfile() { file file = new file("d:/bigfile.zip"); gateway.sendtosftp(file); } }
i'm complete newbie spring , i'm not sure entirely sftp channels created here properly, guess new 1 gets created every time sendtosftp call. on how achieve queue behaviour in case appreciated.
you don't have queue here because each http request performed in own thread. right, maybe queued there anyway when pool of http threads exhausted, doesn't in simple use-case 2 requests.
you can achieve queue behavior there anyway, should declare tosftpchannel
queuechannel
bean.
that way downstream process performed on same thread , next message pulled queue after first one.
see reference manual more information.
update
since use ftpmessagehandler
one-way component, still need reply mvc controller's methods, way have @gateway
method non-void
return , of course need send reply somehow.
for purpose suggest use publishsubscribechannel
:
@bean @bridgeto public messagechannel tosftpchannel() { return new publishsubscribechannel(); } @bean @serviceactivator(inputchannel = "tosftpchannel") @order(0) public messagehandler handler() { sftpmessagehandler handler = new sftpmessagehandler(sftpsessionfactory()); handler.setremotedirectoryexpression(new literalexpression("/")); return handler; }
this way have 2 subscribers tosftpchannel
. @order(0)
ensure @serviceactivator
first subscriber since need perform sftp transfer first. @bridgeto
add second bridgehandler
same publishsubscribechannel
. purpose replychannel
header , send request message there. since don't use threading bridgehandler
performed after finish of transfer sftp.
of course instead of bridgehandler
can have other @serviceactivator
or @transfromer
return reply not request file
, else. example:
@serviceactivator(inputchannel = "tosftpchannel") @order(1) public string transfercomplete(file payload) { return "the sftp transfer complete file: " + payload; }
Comments
Post a Comment