Processing Incoming Packets(without infinite loop !) ...PacketListener

Hi Guys,

I am trying to

  1. To process all incoming messages, so it should always listen to incoming messages

  2. Return them a message

I am using PacketListener but gets terminated after some time !! Why o Why shoudnt it keep running and listen incoming packet !!

I dug into Docs and implented following code , I tried this with PacketLisner but problem is my program terminates after few seconds , but it should keep listening …i dont want to use infinite loop to keep my program alive as it takes too much cpu and its bad programming practice…same code works when I add infinite loop (but takes 90% cpu) …pl tell whats wrong in my code below

if possible kindly give full class file example on how to Processing Incoming Packets(Messages) via PacketCollector and PacketListener as i was not able find a pratical example code

thanks in adavance !!

Code

/**

  • Doesnt work without infinite loop

*/

import org.jivesoftware.smack.PacketListener;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.packet.Packet;

import org.jivesoftware.smack.packet.Message.*;

import org.jivesoftware.smack.*;

import org.jivesoftware.smack.packet.*;

import org.jivesoftware.smack.filter.*;

import org.jivesoftware.smackx.packet.VCard;

import org.jivesoftware.smack.GoogleTalkConnection;

import com.pagux.ibot.actions.webservices.mq.*;

import com.pagux.ibot.plugin.im.Im;

import com.pagux.ibot.master.Master;

import org.apache.log4j.Logger;

/**

  • @author gp

*/

public class JabberTest {

/**

*/

public JabberTest() {

// TODO Auto-generated constructor stub

}

/**

  • @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

String username = “smack”;

String password = “willitwork”;

String server = “wildfire-server”;

String status = “will it work without infinite loop ??..”;

XMPPConnection connection = null;

try {

connection = new XMPPConnection(server);

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

connection.login(username, password);

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// Accept only messages from HQ

PacketFilter filter

= new AndFilter(new PacketTypeFilter(Message.class),

new FromContainsFilter(“wildfire-server”));

PacketListener myListener = new PacketListener() {

public void processPacket(Packet packet) {

if (packet instanceof Message) {

Message msg = (Message) packet;

// Process message

System.out.println(“Roger, Roger”);

}

}

};

// Register the listener.

connection.addPacketListener(myListener, filter);

//Doesnt work without infinite loop !!!

//while(true){}

}

}

Even though the XMPP connection and listeners kick off threads to do the work. I’'m pretty sure the main thread has to hang around or its exiting will terminate the threads its kicked off.

Rather then a dead loop you could wait or sleep forever.

infinite loop sucks !! even with thread sleeping …i am looking at muse if they got better solution

Sleeping is not an infinite loop.

Alex

Can you give a complete code example for god sake , smack documentation is really confusing and incomplete

import org.jivesoftware.smack.PacketListener;
    import org.jivesoftware.smack.XMPPException;
    import org.jivesoftware.smack.packet.Packet;
    import org.jivesoftware.smack.packet.Message.*;
    import org.jivesoftware.smack.*;
    import org.jivesoftware.smack.packet.*;
    import org.jivesoftware.smack.filter.*;
    import org.jivesoftware.smackx.packet.VCard;
    import org.jivesoftware.smack.GoogleTalkConnection;
    import com.pagux.ibot.actions.webservices.mq.*;
    import com.pagux.ibot.plugin.im.Im;
    import com.pagux.ibot.master.Master;
    import org.apache.log4j.Logger;     /**
     * @author gp
     *
     */
    public class JabberTest {         /**
         *
         */
        public JabberTest() {
// TODO Auto-generated constructor stub
        }         /**
         * @param args
         */
        public static void main(String[] args) {
            String username = "smack";
            String password = "willitwork";
            String server = "wildfire-server";
            String status = "will it work without infinite loop ??...";             XMPPConnection connection = null;
            try {
                connection = new XMPPConnection(server);
            }
            catch (XMPPException e) {
                e.printStackTrace();
            }
            try {
                connection.login(username, password);
            }
            catch (XMPPException e) {
                e.printStackTrace();
            }             PacketFilter filter
                    = new AndFilter(new PacketTypeFilter(Message.class),
                    new FromContainsFilter("wildfire-server"));             PacketListener myListener = new PacketListener() {
                public void processPacket(Packet packet) {
                    if (packet instanceof Message) {
                        Message msg = (Message) packet;
                        System.out.println("Roger, Roger");                     }
                }
            };
        synchronized (Thread.currentThread()) {
            try {
                Thread.currentThread().wait();
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        }