Resilient connection to server
I'm trying to maintain a permanent connection with an imap server.
I use the following code to connect:
String protocol ="imap";
url =new URLName(protocol, server, -1, mbox, user, password);
Properties props = System.getProperties();
session = Session.getDefaultInstance(props);
try{
folder = session.getFolder(url);
// add connection and messagecount listeners to the folder
folder.open(Folder.READ_WRITE);
}catch (Exception e){
thrownew IOException("Couldn't connect..");
}
And periodically poll the connection with the following method:
void poll()throws Exception{
if (!folder.isOpen()){
folder.open(folder.READ_WRITE);
}
}
The method above seems to reopen my folder just fine after a disconnection, even the listeners are still there, but is this by design or just a lucky coincidence?
I'm writing a server app that should ideally run 'forever' and not need manual intervention. Should I be doing something differently?
Thank you,
Isak
[1646 byte] By [
ihansena] at [2007-10-3 5:23:30]

That approach should prevent the server from deciding that the
connection is not being used and close it, but of course there's
lots of other reasons the connection could be closed, and lots
of other places in your code where that might happen, and your
app probably needs to consider these other failure modes as well.
i'm also trying the same thing but i used one more thing that before checking folder i used
if(!store.isConnected())
{
store.connect();
}
i'm try to connect multiple mail-account from may be different mail servers..........
but still the problem remains the same, don't know why sometimes mailservers get connected sometimes they are not!! there is no relibility for my connection it may be becaues of my coding .... :) but if u find some answer then plz let me know......
how long the mail-server maintains the connection? i'm keeping my tomcat's session time as 20 minutes assumin that mail-server connection is less than that.....
how to maintain "permanent" connection with the mail-server.....
there r more question than the answers...................
(i no problem with my Internet Connection :) )
It depends on the server how quickly it times out idle connections.Typical is 30 minutes.I've also heard rumors that some NAT boxes will time out idle connectionsin as little as 5 minutes.
so..................what's the moral of the story?i'm already checking store connection before using it.....i'm i forgetting something?thanks.
> That approach should prevent the server from deciding
> that the
> connection is not being used and close it, but of
> course there's
> lots of other reasons the connection could be closed,
> and lots
> of other places in your code where that might happen,
> and your
> app probably needs to consider these other failure
> modes as well.
Hmm.. my initial post wasn't too clear.
The polling thing isn't relevant, only how that method *seems to* reconnect my folder just fine after connection outages. Is this really all it takes to get back up and running?
As for the rest of my app (may as well get an evaluation on that now that i'm posting here) it goes something like this:
I aquire a (closed) Folder and add two listeners to it. A ConnectionListener that responds to opened() events, and a MessageCountListener that responds to messageAdded(), and then open the folder for read/write.
My listeners hand off each message they see to a method which deletes them from the inbox after processing - the method just fails silently on IO errors.
I do not track disconnects, as I need to poll the connection regularly anyway for message events to occur (IMAP), and the poll method hopefully reconnects me to the server as needed.
Sounds reasonable?
> so..................what's the moral of the story?> i'm already checking store connection before using> it.....i'm i forgetting something?You're forgetting that the connection can break the momentafter you check.
> The polling thing isn't relevant, only how that
> method *seems to* reconnect my folder just fine after
> connection outages. Is this really all it takes to
> get back up and running?
Yes.
> As for the rest of my app (may as well get an
> evaluation on that now that i'm posting here) it goes
> something like this:
>
> I aquire a (closed) Folder and add two listeners to
> it. A ConnectionListener that responds to opened()
> events, and a MessageCountListener that responds to
> messageAdded(), and then open the folder for
> read/write.
>
> My listeners hand off each message they see to a
> method which deletes them from the inbox after
> processing - the method just fails silently on IO
> errors.
>
> I do not track disconnects, as I need to poll the
> connection regularly anyway for message events to
> occur (IMAP), and the poll method hopefully
> reconnects me to the server as needed.
>
> Sounds reasonable?
Yes.
There's only one issue I can think of...
If the Store connection fails, I believe it will still allow the Folder to be
opened, but I'm not sure whether that's a bug or a feature.
