1 21 package com.presumo.jms.test; 22 23 import com.presumo.jms.client.JmsTopicConnectionFactory; 24 25 import javax.jms.ExceptionListener ; 26 import javax.jms.Topic ; 27 import javax.jms.TopicConnection ; 28 import javax.jms.TopicSession ; 29 import javax.jms.TopicSubscriber ; 30 import javax.jms.TopicPublisher ; 31 import javax.jms.Message ; 32 import javax.jms.BytesMessage ; 33 import javax.jms.JMSException ; 34 import javax.jms.MessageListener ; 35 import javax.jms.Session ; 36 37 42 public class SimplePerfTest implements MessageListener , ExceptionListener 43 { 44 45 public final static int PUBLISHER = 0; 46 public final static int SUBSCRIBER = 1; 47 public final static int BOTH = 2; 48 49 public static String hostname = "localhost"; 50 public static int serverPort; 51 public static int localPort; 52 53 private final int numOfMessages; 54 private final int iterations; 55 private final int totalMessages;; 56 57 private long firstMessageReceived; 58 private int messagesReceived; 59 private int totalMessagesReceived; 60 private volatile boolean exceptionOccurred = false; 61 62 private final int type; 63 private TopicConnection connection; 64 65 SimplePerfTest(int type) 66 { 67 this(type, 10, 2); 68 } 69 70 SimplePerfTest(int type, int numOfMessages, int iterations) 71 { 72 this.type = type; 73 this.numOfMessages = numOfMessages; 74 this.iterations = iterations; 75 this.totalMessages = numOfMessages * iterations; 76 } 77 78 void startTest() throws Exception 79 { 80 JmsTopicConnectionFactory factory = new JmsTopicConnectionFactory(); 81 factory.setHost(hostname); 82 factory.setPort(2323); 83 84 if (type == PUBLISHER || type == SUBSCRIBER) { 85 } 88 89 connection = factory.createTopicConnection(); 90 connection.setExceptionListener(this); 91 92 connection.start(); 93 String topicName = "testing"; 94 95 if ( type == SUBSCRIBER || type == BOTH ) 96 { 97 TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 98 Topic topic = session.createTopic(topicName); 99 TopicSubscriber sub = session.createSubscriber(topic, null, false); 100 sub.setMessageListener(this); 101 System.out.println("Subscriber Created"); 102 } 103 104 if (type == PUBLISHER || type == BOTH ) 105 { 106 TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 107 Topic topic = session.createTopic(topicName); 108 TopicPublisher pub = session.createPublisher(topic); 109 pub.setDeliveryMode(javax.jms.DeliveryMode.PERSISTENT); 110 111 for (int i=0; i < iterations; i++) { 112 long startTime = System.currentTimeMillis(); 113 114 for (int j=0; j < numOfMessages; j++) { 115 Message msg = session.createMessage(); 118 msg.setIntProperty("Count", (i* numOfMessages + j)); 119 pub.publish(msg); 120 } 121 long finishTime = System.currentTimeMillis(); 122 long time = finishTime - startTime; 123 System.out.println("Published "+ numOfMessages +" messages in " + time +" millseconds."); 124 try { 125 Thread.sleep(2000); 126 } catch (InterruptedException ie) {} 127 } 128 129 if (type == PUBLISHER) { 130 try { Thread.sleep(10000); } catch (InterruptedException ie) {} 131 totalMessagesReceived = totalMessages; 132 } 133 134 } 135 } 136 137 public void onException(JMSException jmsex) 138 { 139 jmsex.printStackTrace(); 140 exceptionOccurred = true; 141 } 142 143 public void onMessage(Message message) 144 { 145 ++messagesReceived; 146 ++totalMessagesReceived; 147 if (messagesReceived == 1) { 148 firstMessageReceived = System.currentTimeMillis(); 149 } 150 if (messagesReceived == numOfMessages) { 151 long time = System.currentTimeMillis() - firstMessageReceived; 152 System.out.println("Received "+ numOfMessages +" messages in " + time +" milliseconds."); 153 messagesReceived = 0; 154 } 155 } 157 158 boolean isComplete() { 159 return ( exceptionOccurred || 160 totalMessagesReceived == totalMessages); 161 } 162 163 void close() throws Exception 164 { 165 if (connection != null) 166 connection.close(); 167 168 } 169 170 public static void main(String [] args) throws Exception 171 { 172 175 SimplePerfTest test = null; 176 if (args.length == 0) { 177 test = new SimplePerfTest(BOTH); 178 } 179 else if (args[0].equals("both")) { 180 test = new SimplePerfTest(BOTH, Integer.parseInt(args[1]), Integer.parseInt(args[2])); 181 } 182 else if (args[0].equals("pub")) { 183 test = new SimplePerfTest(PUBLISHER, Integer.parseInt(args[1]), Integer.parseInt(args[2])); 184 } 185 else if (args[0].equals("sub")) { 186 test = new SimplePerfTest(SUBSCRIBER, Integer.parseInt(args[1]), Integer.parseInt(args[2])); 187 } 188 if (args.length == 4) 189 hostname = args[3]; 190 191 test.startTest(); 192 193 while (!test.isComplete() ) { 194 try { Thread.sleep(1000); } catch (InterruptedException ie) {} 195 System.gc(); System.gc(); System.gc(); 196 } 197 198 System.out.println("Closing connection"); 199 test.close(); 200 System.out.println("Test completed"); 201 } 202 } | Popular Tags |