On 12/15/10 12:08 AM, Mark Harris wrote:
Hello,
Hi,
I'm a Mina newbie and have been tasked with adding some logic to some
existing code. The code as it stands uses Mina to initially connect to a
server, then - utilising an inner class that extends IoHandlerAdapter -
it organises the sending of messages. The messages to be sent are
organised in a Map prior to sending and removed from the Map when a
response is received from the server.
ok.
The spec for the work is quite vague, for which I apologise. I need to
add some "retry" functionality to the code I have so that if, for
example, a network cable is disconnected, outbound messages will retry
for a period of time for a given number of attempts.
Understood. The only problem is to be able to detect that the connection
is broken, and then to resend the messages.
In fact, there is no way to know that the remote host has disconnected
brutally, except using a timeout. Note that closing a connection and
disconnecting a network cable are two different things and the result
will be different : if the remote host close the connection, the client
will be 'informed' that the connection has been closed, something that
does not occur when the cable is removed from the plug.
The other problem is that if the connection is closed, then you'll have
to reopen it. It's quit easy : you drop the current session, disconnect
and reconnect a bit later (no need to retry immediately, as the
disconnection cause won't be solved immediately, but this is something
you would configure)
I've not done anything like this before in any programming task and am a
bit lost when it comes to picturing what I need to do in terms of new
functionality. The message to the server is written to the IOSession
(set when the server connection is established), which then waits until
a response is received before that response is processed. I'm guessing
that it is here somewhere that I would need to add appropriate
functionality.
Yep. Mainly detect either a disconnection (and an Closed() event will be
fired), or use a timeout for the expected response, and if the response
has not been received, then retry, after having closed the session and
reconnected.
I've had a look on the Net for similar examples but have yet to find
anything which does the same thing. If anyone has any pointers, I'd be
grateful. I'm not looking for complete solutions, just general ideas so
that I can get it clear in my head how the functionality will work. Then
I can code it appropriately myself.
You can have a look at
http://svn.apache.org/viewvc/directory/shared/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java?view=markup
It may give you a clue about how to manage a timeout for a synchronous
operation. Don't be afraid if the class is huge, it's used to
communicate with a LDAP server, but basically, if you follow a add()
operation, you'll see the logic : first, connect(), then send a add(),
wait for the response, and return it or generate an error if the timeout
has been reached.
What is complex is that MINA is asynchronous, which means we simulate a
synchronous operation by waiting on a future returned by the addAsync()
operation. This future is notified in the messageReceived() method.
Feel free to ask for help if needed.
Thanks for any advice,
Mark
--
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com