Spring FTP Inbound vs Outbound Channel Adaptor - java

What is the difference between Inbound Channel Adaptor and Outbound Channel Adaptor in Spring Integration FTP. Which one I should I use and when?
I read from documentation that outbound can send any type of file(like byte[], String, java.io.File) but Inbound is restricted for File type only. Is that only difference or anything else?

I suggest you to read a theory first of all.
Any Inbound adapter is intended to get data from external system. Outbound - to put data. E.g. simple case: JDBC Inbound performs a SELECT from DB, Outbound - INSERT.
In case of FTP: the first one to read files from FTP, the last - to write them.

Related

What connection except for network connection can Jetty Endpoint represents

https://www.eclipse.org/jetty/javadoc/9.4.8.v20171121/org/eclipse/jetty/io/EndPoint.html#getLocalAddress--
From this documentation, getLocalAddress will return null if the endpoint does not represent a network connection. So under what circumstances the endpoint represents connection other than network connection? And what type of connections are they?
FastCGI, UnixSocket, and LocalConnector usages are some examples of connections that have an EndPoint but are not network connections that have a java.net.InetSocketAddress available to represent the remote endpoint.
Note: Jetty 10 is considering changing that API from returning java.net.InetSocketAddress to just java.net.SocketAddress (as all three of the above can return flavors of SocketAddress)
FastCGI - FastCgiSocketAddress - points to the directory on disk
UnixSocket - UnixSocketAddress - points to the socket file (usually on disk)
LocalConnector - LocalSocketAddress - an implementation with no public methods (as this is a purely in-memory connection)
Other projects have variants of in-memory or mock connectors that would also represent an EndPoint without a InetSocketAddress.

netty http file example: why ssl handler uses a different way to write messages?

The example code:
https://github.com/netty/netty/blob/4.1/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java
if ssl is disabled, it uses FileRegion instead of ChunkedInput. Why is that? Won't a large file break the code?
Thanks.
It turned out to be calling FileChannel's transferTo() method when ssl is disabled. That method is more efficient than reading from a channel or some wrappings of ByteBuf. And when the request is passed to channel the size of the message will be set to 0 in that case.

how to respond from plain tcp client to MessagingTemplate's sendAndReceive

I'm using channel-adapters (not gateways) to send data with MessagingTemplate's sendAndReceive from spring integration server to a connected nonspring client (or just telnet).
After receiving the data in the client, somewhen I want to reply data to the server and resolve that sendAndReceive-Waiting. I still want to be able to send other data to the server.
How will sendAndReceive detect a response? Right now I can send whatever I want to the server, it will assume it as a new incoming message.
Is there a predefined way, like prefixing a messageid or do I have to implement it manually by interpreting the incoming messages and somehow "resolve" the sendAndReceive-blocker?
MessagingTemplate.sendAndReceive is based on the TemporaryReplyChannel which is placed to the MessageHeaders and afterward some AbstractReplyProducingMessageHandler just uses that header to send reply back.
Yes, the sending Thread is blocked to wait for the reply throughout that TemporaryReplyChannel.
Hope that can help you a bit.
All other your comment regarding TCP/IP isn't clear for me yet...

Netty 5, how to bind some specify info in channel

ClientA and Client B connect to the NettyServer.
when the connection builds, there is a channel between Client and NettyServer, I want when channel active, I can add Client side info in the channel, so that I can store like Map(ClientInfo,Channel). The on the server side, I can use Map.get(ClientInfo) to get the specific channel, then I can use channel.writeAndFlush() to send message to the specific client.
how to implement it? Use attachment? But, in neety5 API ChannelHandler, the attachment example is not the way I want to use. I wander if I can add attachment at Client side, and I can get it at the Server side?
You can use the AttributeKey or AttributeMap to store your client info, and store them into channel or channelContext, if necessary you can use them using get or set method.

FTPClient Pool - Java

I am writing a Rest Service that connects to an FTP server to read some files, then do some operations over the read data to serve the service request. I am using Apache commons FTPClient.
As a temporary solution, I am creating an FTPClient object - then connecting it - and then logging in with the credentials - inside a method (the client is local to this method - doing this as FTPClient is not thread safe) in my data access layer and then disconnecting it before coming out of the methods(ie.. after reading the file). The issue is, FTPClient is taking some 3-7 seconds for logging in which is very high. So I am thinking of implementing an FTPClientPool that can provide an already prepared client in the data access method.
Do any such ClientPools already exist?
If yes, then what one should I opt for?
If no, the difficulty in implementing is once created and connected, How long does an apache FTPClient stay alive? for infinite time?? (what I mean is what is the default keep alive time for an FTPClient - idle time after which client gets disconnected - coz I see various kind of times in the java docs. :( ) And next questions is How do you keep it alive always?? (may be sending the NOOPS after regular intervals in a separate thread??) Any kind of help regarding how should I move forward is really helpful.
Thanks & Regards
Idle timeout for clients is generally determined server side.
Here's some of the more non obvious client parameters:
soTimeout - Determines how long the client blocks waiting for a message. Generally you poll a socket every so often and this determines how long you wait during a poll.
soLinger - Determines how long to keep the connection after close() has been called.
From my experience of using FTP, they normally just reconnect if the connection closes - it's not normally vital to have a constant uninterrupted connection unlike in other applications.
What are you using FTP for - it's normally not that time critical a service ...
As for ClientPools, I happened to write a demo project.
commons-pool-ftp
I am getting a little bit annoyed by the ftp protocol,
in our experience, it would meet broken pipe when testing on the client that just getting from the pool.
testOnBorrow=true
Configure
protected static ThreadLocal<FTPClient> ftpClientContainer = new
ThreadLocal<>();
Then use:
//login() will be your login method to FTP:
ftpClientContainer.set(ftpLogin());
Then in each method add:
FTPClient ftpClient = ftpClientContainer.get();
and finely when done:
//ftpDisconnect () will be your disconnect method to FTP:
ftpDisconnect(ftpClientContainer.get());

Categories