1 18 package org.apache.activemq.benchmark; 19 20 import javax.jms.Destination ; 21 import javax.jms.JMSException ; 22 import javax.jms.Message ; 23 import javax.jms.MessageConsumer ; 24 import javax.jms.MessageListener ; 25 import javax.jms.Session ; 26 import javax.jms.TextMessage ; 27 import javax.jms.Topic ; 28 29 33 public class Consumer extends BenchmarkSupport implements MessageListener { 34 35 public static void main(String [] args) { 36 Consumer tool = new Consumer(); 37 if (args.length > 0) { 38 tool.setUrl(args[0]); 39 } 40 if (args.length > 1) { 41 tool.setTopic(parseBoolean(args[1])); 42 } 43 if (args.length > 2) { 44 tool.setSubject(args[2]); 45 } 46 if (args.length > 3) { 47 tool.setDurable(parseBoolean(args[3])); 48 } 49 if (args.length > 4) { 50 tool.setConnectionCount(Integer.parseInt(args[4])); 51 } 52 53 try { 54 tool.run(); 55 } 56 catch (Exception e) { 57 System.out.println("Caught: " + e); 58 e.printStackTrace(); 59 } 60 } 61 62 public Consumer() { 63 } 64 65 public void run() throws JMSException { 66 start(); 67 subscribe(); 68 } 69 70 protected void subscribe() throws JMSException { 71 for (int i = 0; i < subjects.length; i++) { 72 subscribe(subjects[i]); 73 } 74 } 75 76 protected void subscribe(String subject) throws JMSException { 77 Session session = createSession(); 78 79 Destination destination = createDestination(session, subject); 80 81 System.out.println("Consuming on : " + destination + " of type: " + destination.getClass().getName()); 82 83 MessageConsumer consumer = null; 84 if (isDurable() && isTopic()) { 85 consumer = session.createDurableSubscriber((Topic ) destination, getClass().getName()); 86 } 87 else { 88 consumer = session.createConsumer(destination); 89 } 90 consumer.setMessageListener(this); 91 addResource(consumer); 92 } 93 94 public void onMessage(Message message) { 95 try { 96 TextMessage textMessage = (TextMessage ) message; 97 98 String text = textMessage.getText(); 100 count(1); 101 102 104 } 106 catch (JMSException e) { 107 e.printStackTrace(); 109 } 110 } 111 112 } 113 | Popular Tags |