powered by Jive Software

Conection closes because thread ends

I’m trying to create a sample console project using smack. I used another library before (muse) and I’m accustomed to the concept of the JabberSession. Using the session, and connecting to the server through it, creates a separate thread that is always running. I did not find anything similar in the smack API, and when I create a connection in my main method, the connection is closed after the code in the method is executed. Is there anything similar to muse’s JabberSession that I can use?

I think your problem is that the main() function finishes and closes the application. You need to start a thread to keep the main() from exiting your application, or an event-loop.

Here’s an article that can point you in the right direction: http://pezra.barelyenough.org/blog/2005/03/java-daemon/

Here’s an incomplete snippet of how I implemented a XMPP bot client

public class BotMain implements Runnable {
    private static Thread mainloop = null;     public BotMain() {
    }     /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Throwable {
        BotMain bot = new BotMain();         try {
            // sanity checks and startup actions
            // database setup
            // XMPP setup
                    } catch (XMPPException ex) {
            throw new Throwable(errorMessage, ex);
                } catch (final Throwable e) {
            shutdown();
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    logger.fatal("Startup failed", e);
                }
            });
            System.exit(1);
            return;
        }                       mainloop = new Thread(bot);
        if (mainloop != null)  {
            mainloop.start();
        }
        mainloop = null;
    }

Thanks a lot. I’ll read the article, and will try your solution. I will be updating you with the results. Thanks again