09-24-2012 07:36 AM - edited 09-24-2012 07:43 AM
Hi I has been trying to figure out how to do this all the morning and googled about this without finding a good answer.
I have an application that listen continuosly pushes from a a bes server. Now I have to modify it to listen pushes for a period of time. After this time I have to stop listen (and process the push messages received but this not the case.
Setting a timeout seems to work only when a connection is already open so it doesn't helps.
Finally I've tried to run a Thread just before making the 'acceptAndOpen' call. This Thread waits for some time and if nothing is received then it tries to call to 'close' the Connection, but all I get is a 'Invalid IPPP Queue State' in simulator. I provide part of the code for more details.
StreamConnection stream = null;
InputStream input = null;
MDSPushInputStream pushInputStream = null;
[...]
while (!stop) {
try {
[...]
synchronized (this) {
// scn = (StreamConnectionNotifier) Connector.open(URL + ";ConnectionTimeout=30000;deviceside=false",Connector.READ,true);
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc = connFact.getConnection(URL + ";deviceside=false");
connFact.setConnectionTimeout(30000);
scn = (StreamConnectionNotifier) connDesc.getConnection();
}
AccepConnectionTimeout thread = null;
while (!stop) {
try {
// NOTE: the following will block until data is received.
thread = new AccepConnectionTimeout(scn);
thread.start();
stream = scn.acceptAndOpen();
} catch (InterruptedIOException iioe) {
Log.error(ERR_TAG + "Timeout waiting connection:" + iioe.getMessage());
scn.close();
scn = null;
// TODO do something here
continue;
}
timestampUltimaRecepcion = System.currentTimeMillis();
[...]
public class AccepConnectionTimeout extends Thread {
public boolean stop = false;
public StreamConnectionNotifier scn = null;
public AccepConnectionTimeout(StreamConnectionNotifier scn) {
this.scn = scn;
}
public void run() {
try {
Thread.sleep(30000);
} catch (Exception e) {
Log.error(ERR_TAG + "Error waiting for accept timeout");
}
if (!stop) {
try {
scn.close();
} catch (IOException ioe) {
Log.error(ERR_TAG + "Error closing connection after accept timeout:" + ioe.getMessage());
} catch (Exception e) {
Log.error(ERR_TAG + "(Exception)Error closing connection after accept timeout:" + e.getMessage());
}
}
}
[...]
(NOTE:I've only run this in a simulator)
I don't find many information about this 'error' and also my main purpose is achieve the target described in second paragraph. Could you help me in any way? how to do this? how to solve it? examples?
Solved! Go to Solution.
09-24-2012 07:26 PM - edited 09-25-2012 05:58 PM
I am not sure I understand your requirement.
Normally what you would do if have a listener for push messages, and each time a message is received, it starts a new Thread and passes the received data to it. So I don't understand the issue with this:
"After this time I have to stop listen (and process the push messages"
Adopting the processing approach I mentioned will make sure pushed messages are processed as soon as they are received, which seems the correct thing to do.
Given you do that, then I think the requirement to stop the Listener disappears.
Sorry I have no comment on your message, just the design of this part of your application.
09-25-2012 03:31 PM
Thank you for your response, I was wrong with my approach. My main mistake has been to try to slighty modify the old code (that I also didn't know) to achieve the new required functionality.
Simply I was blind trying to stop the listener because I was in a hurry....I've rewriten the code properly with several threads and know everything works fine.