XMPP Java bot loop question  
Author Message
Sabine Dinis Blochberger





PostPosted: 2007-7-31 19:58:00 Top

java-programmer, XMPP Java bot loop question I want to write a bot for XMPP (Jabber) in Java (1.6) using smack
libraries (http://www.igniterealtime.org).

I have already written a GUI client using smack, so I'm familiar with
how it works.

For the bot however, I'm in doubt about the main loop in combination
with event listeners. The bot basically just sits idly (until explicitly
stopped) listening for presence changes of contacts. The presence
changes are dealt with by a roster listener.

How would I wait for those events and have the application keep
executing? I am a bit clueless aobut threads in Java, but I have used
them in Delphi...

On a side note, if anyone can shed light on how to use the commons
daemon (http://commons.apache.org/daemon/), that would be useful later
on.

Many thanks in advance,
--
Sabine Dinis Blochberger

Op3racional
www.op3racional.eu
 
Roedy Green





PostPosted: 2007-7-31 21:43:00 Top

java-programmer >> XMPP Java bot loop question On Tue, 31 Jul 2007 12:58:27 +0100, Sabine Dinis Blochberger
<email***@***.com> wrote, quoted or indirectly quoted someone who
said :

>How would I wait for those events and have the application keep
>executing? I am a bit clueless aobut threads in Java, but I have used
>them in Delphi...

If the system sends you events when things happen, there is no need
for you to do anything while you wait. When you get an event, deal
with it quickly, or you must spawn a thread to deal with it over an
extended period of time. Until you return from the event handler, the
GUI will be unresponsive.

If you need to periodically wake up and sniff around doing something
then go back to sleep, use a Timer see
http://mindprod.com/jgloss/timer.html or a sleeping background Thread.
See http://mindprod.com/jgloss/thread.html


It is unlikely you need to continuously compute something in the
background, so it unlikely you will need threads.

--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Sabine Dinis Blochberger





PostPosted: 2007-8-1 18:53:00 Top

java-programmer >> XMPP Java bot loop question Roedy Green wrote:

> On Tue, 31 Jul 2007 12:58:27 +0100, Sabine Dinis Blochberger
> <email***@***.com> wrote, quoted or indirectly quoted someone who
> said :
>
> >How would I wait for those events and have the application keep
> >executing? I am a bit clueless aobut threads in Java, but I have used
> >them in Delphi...
>
> If the system sends you events when things happen, there is no need
> for you to do anything while you wait. When you get an event, deal
> with it quickly, or you must spawn a thread to deal with it over an
> extended period of time. Until you return from the event handler, the
> GUI will be unresponsive.

Well, these are non-blocking events. Right now the main method executes
and then the application exits, as I anticipated.

Unresponsiveness to the command line shouldn't be a problem, except
maybe to tell the application to shutdown.

I have set up and registered a shutdown hook to be used when it's used
as a daemon/service. For debuggin I'm counting on Netbeans debugger
being able to end the application...

Just creating an instance of my main class didn't do the trick. The
listeners are being set up.

How to keep the main thread going (without consuming CPU)?
--
Sabine Dinis Blochberger

Op3racional
www.op3racional.eu