Hi Guys,
I am trying to
-
To process all incoming messages, so it should always listen to incoming messages
-
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){}
}
}