1 23 package cluster.queue; 24 25 import javax.jms.*; 26 27 30 public class MsgListener implements MessageListener 31 { 32 String ident = null; 33 int nbMsg = 0; 34 long startTime = -1; 35 int sleep; 36 int nbMsgSleep; 37 38 public MsgListener() {} 39 40 public MsgListener(String ident) { 41 this.ident = ident; 42 int sleep = Integer.getInteger("sleep", 0).intValue(); 43 int nbMsgSleep = Integer.getInteger("nbMsgSleep", 10).intValue(); 44 System.out.println("sleep = " + sleep + ", nbMsgSleep=" + nbMsgSleep); 45 } 46 47 public void onMessage(Message msg) { 48 try { 49 nbMsg++; 50 if (nbMsg == 1) 51 startTime = System.currentTimeMillis(); 52 long time = System.currentTimeMillis(); 53 System.out.println("LastTime = " + time); 54 time = time - startTime; 55 System.out.println("time = " + time + " nbMsg=" + nbMsg); 56 57 if (msg instanceof TextMessage) { 58 if (ident == null) 59 System.out.println(((TextMessage) msg).getText()); 60 else 61 System.out.println(ident + ": " + ((TextMessage) msg).getText()); 62 } else if (msg instanceof ObjectMessage) { 63 if (ident == null) 64 System.out.println(((ObjectMessage) msg).getObject()); 65 else 66 System.out.println(ident + ": " + ((ObjectMessage) msg).getObject()); 67 } 68 69 if (sleep > 0 && (nbMsg % nbMsgSleep) == 0) { 70 try { 71 Thread.sleep(sleep); 72 } catch (Exception e) {} 73 } 74 75 } catch (JMSException jE) { 76 System.err.println("Exception in listener: " + jE); 77 } 78 } 79 } 80 | Popular Tags |