Smack API has Very High CPU Utilization , Is this a bug or feature?

i am getting 90 % cpu usage in my program which uses smack api to listnen to incoming IM and reply a dummy message which is simple two class file program . I ran the profiling and found that following clases were taking maximum time

  • org.jivesoftware.smack.Roster:fireRosterChangedEvent(Collection arg1, Collection arg2, Collection arg3) : void 10.00 ms
  • org.jivesoftware.smack.provider.ProviderManager:() : void 490.00 ms
  • com.pagux.ibot.plugin.im.gtalk.GooglePacketListener:() : void 460.00 ms
  • org.jivesoftware.smack.SmackConfiguration:() : void 220.00 ms
  • org.jivesoftware.smack.ServerTrustManager:(String arg1, ConnectionConfiguration arg2) : void 730.00 ms
  • org.jivesoftware.smack.PacketWriter:nextPacket() : Packet 9.07 sec
  • org.jivesoftware.smack.XMPPConnection:proceedTLSReceived() : void 7.65 sec
  • org.jivesoftware.smack.PacketReader:startup() : void 8.43 sec
  • org.jivesoftware.smack.XMPPConnection:init() : void 90.00 ms
  • org.jivesoftware.smack.SmackConfiguration:parseClassToLoad(XmlPullParser arg1) : void 100.00 ms

i am using jdk 1.5

This is simple two class files code . You can view the actual code at
http://paste-bin.com/869

and cpu profiling here
http://paste-bin.com/872

i cant yet believe smack takes so much of CPU but profiling says otherwise !!

is there any other otimization i should do to my code ? pl suggest

-> Smack CPU Sucking Vampire Code

  1. import org.jivesoftware.smack.Chat;
  2. import org.jivesoftware.smack.GoogleTalkConnection;
  3. import org.jivesoftware.smack.XMPPConnection;
  4. import org.jivesoftware.smack.XMPPException;
  5. import org.jivesoftware.smack.filter.PacketTypeFilter;
  6. import org.jivesoftware.smack.packet.Message;
  7. import org.jivesoftware.smack.packet.Presence;
  8. import com.pagux.ibot.plugin.im.Load;
  9. /**
  10. */
  11. /**
    • @author gp
  12. */

public class Loader extends Thread implements Load {
1.
1.
1.

  1. boolean isLoaded;
  2. [String|http://www.google.com/search?q=allinurl%3AString+java.sun.com&bntl=1] username, password, status,server;
  3. [String|http://www.google.com/search?q=allinurl%3AString+java.sun.com&bntl=1] type=“jabber”;
  4.  public Loader(String username, String password, String status,[String|http://www.google.com/search?q=allinurl%3AString+java.sun.com&bntl=1] server) {
    
  5. super();
  6. this.username = username;
  7. this.password = password;
  8. this.status = status;
  9. this.server=server;
  10. }
  11. public void load() {
  12. GoogleTalkConnection googleConnection;
  13. try {
  14. if(type.equalsIgnoreCase(“jabber”))
  15. {
  16. XMPPConnection con = new XMPPConnection(server);
  17. con.login(username, password);
  18. PacketTypeFilter filter = new PacketTypeFilter (Message.class);
  19. con.addPacketListener(new JabberPacketListener(con), filter);
  20. Presence presence = new Presence(Presence.Type.fromString(status));
  21. presence.setStatus(status);
  22. con.sendPacket(presence);
  23. SendMessageGtalk.getInstance().setConnection(con);
  24. setLoaded(true);
  25. }else {
  26. googleConnection = new GoogleTalkConnection();
  27. PacketTypeFilter filter = new PacketTypeFilter (Message.class);
  28. googleConnection.login(username, password);
  29. googleConnection.addPacketListener(new GooglePacketListener(googleConnection), filter);
  30. Presence presence = new Presence(Presence.Type.fromString(status));
  31. presence.setStatus(status);
  32. googleConnection.sendPacket(presence);
  33. SendMessageGtalk.getInstance().setConnection(googleConnection);
  34. setLoaded(true);
  35. }
  36. while(true) {}
  37. } catch(XMPPException e) {
  38. e.printStackTrace();
  39. setLoaded(false);
  40. }
  41. }
  42. public void run() {
  43. load();
  44. }
  45. /**
    • @param args
  46. */
  47.  public static void main(String[] args) {
    
  48. // TODO Auto-generated method stub
  49.          String username = "username";
    
  50.          String password = "password";
    
  51.          String status = "bhow bhow ...";
    
  52. /**/
  53. /*
    • ld.setType(“jabber”);
  54. String username = “bhow”;
  55. String password = “meow”;
  56. String status = “bhow …”;
  57. */
  58. Loader ld = new Loader(username,password,status,“gauravp-vp-a10”);
  59. ld.setType(“gtalk”);
  60. ld.load();
  61. //ld.loa.lod(username, password, status);
  62. //String resource = “”;
  63. }
  64. public boolean isLoaded() {
  65. return isLoaded;
  66. }
  67. public void setLoaded(boolean isLoaded) {
  68. this.isLoaded = isLoaded;
  69. }
  70.  public String getType() {
    
  71. return type;
  72. }
  73.  public void setType(String type) {
    
  74. this.type = type;
  75. }
  76.  public String getStatus() {
    
  77. return status;
  78. }
  79.  public void setStatus(String status) {
    
  80. this.status = status;
  81. }
  82. }

  83. GooglePacketListner Java code

  84. import java.util.regex.Matcher;
  85. import java.util.regex.Pattern;
  86. import org.jivesoftware.smack.PacketListener;
  87. import org.jivesoftware.smack.XMPPException;
  88. import org.jivesoftware.smack.packet.Packet;
  89. import org.jivesoftware.smack.packet.Message.*;
  90. import org.jivesoftware.smack.*;
  91. import org.jivesoftware.smack.packet.*;
  92. import org.jivesoftware.smack.filter.*;
  93. import org.jivesoftware.smackx.packet.VCard;
  94. import org.jivesoftware.smack.GoogleTalkConnection;
  95. import com.pagux.ibot.actions.webservices.mq.*;
  96. import com.pagux.ibot.plugin.im.Im;
  97. import com.pagux.ibot.master.Master;
  98. import org.apache.log4j.Logger;
  99. public class GooglePacketListener implements PacketListener,Im {
  100. static Logger log = Logger.getLogger(“com.pagux.ibot.plugin.im.gtalk.GooglePacketListener”);
  101. GoogleTalkConnection connection;
  102.  String chatMessage;
    
  103.  String chatFrom ;
    
  104.  String userName;
    
  105. Client cl ;
  106. Master master;
  107. Patterns pats;
  108. public GooglePacketListener(GoogleTalkConnection googleConnection) {
  109. this.connection =googleConnection;
  110. // this.master= Master.getInstance();
  111. log.equals(“abe log ho ja”);
  112. }
  113. public void processPacket(Packet packet) {
  114. Message message = (Message)packet;
  115. Message.Type type = message.getType();
  116. //Patterns pats = new Patterns();
  117.  String userName,msg ;
    
  118.  String usernamePattern = "(
    

D**)@.**";
1.

  1. if(type == Message.Type.CHAT) {
  2. try {
  3.      System.out.println("Message From: " + message.getFrom() ); 
    
  4. Pattern pat=Pattern.compile(usernamePattern);
  5. Matcher matcher = pat.matcher (message.getFrom());
  6.      System.out.println("Result is " + matcher.find() *"   "* matcher.group(1)) ;
    
  7. userName=matcher.group(1);
  8. msg = message.getBody();
  9.      System.out.println("Message: " + message.getBody() *" hi ha  "*message.getTo()   );
    
  10.      String user = message.getFrom();
    
  11. // master.recieveMessage(msg, user, user, userName, “gtalk”);
  12. Chat chat = new Chat(connection, message.getFrom());
  13. chat.sendMessage("take it bck : "+message.getBody() );
  14. // chat.sendMessage(message.getBody());
  15.                  } catch (Exception e) {
    
  16. // TODO Auto-generated catch block
  17. e.printStackTrace();
  18. }
  19. }
  20. }
  21.  public String getChatFrom() {
    
  22. return chatFrom;
  23. }
  24.  public void setChatFrom(String chatFrom) {
    
  25. this.chatFrom = chatFrom;
  26. }
  27.  public String getChatMessage() {
    
  28. return chatMessage;
  29. }
  30.  public void setChatMessage(String chatMessage) {
    
  31. this.chatMessage = chatMessage;
  32. }
  33.  public void changePresence(String presence) {
    
  34. // TODO Auto-generated method stub
  35. }
  36.  public void recieveMesg(String user, String msg) {
    
  37. // TODO Auto-generated method stub
  38. }
  39.  public boolean sendMessage(String userid, String msg) {
    
  40. // TODO Auto-generated method stub
  41. return false;

Message was edited by: pagux2jabber

Message was edited by: pagux2jabber

Hey pagux2jabber,

I would say that your question is more a Java question than a Smack question. Note that you are using an infinite loop with no delay so there is no wonder why the CPU gets “hot”.

BTW, this is not the ideal forum for general Java questions. You can try posting to the Java Technology forums.

Regards,

– Gato

hi Gato ,

Thanks for your answer can pl tell how can i listen to incoming packets without infinite loop

With Smack you can do it a number of different ways.

You can add a Packetlistener to your connection or chat objects. The listener will then sit and listen out for packets. When you receive a packet you can then do what you want with it or you can use a PacketCollector. The collector will sit and collect packets, when you get one you are interested in you can do what you want with it.

See the smack documentation for examples of the above.

hth

Jon