1 22 package org.jboss.test.jbossmessaging.perf; 23 24 import java.io.Serializable ; 25 import java.util.HashMap ; 26 27 import javax.jms.ExceptionListener ; 28 import javax.jms.JMSException ; 29 import javax.jms.Message ; 30 import javax.jms.Queue ; 31 import javax.jms.QueueConnection ; 32 import javax.jms.QueueConnectionFactory ; 33 import javax.jms.QueueReceiver ; 34 import javax.jms.QueueSender ; 35 import javax.jms.QueueSession ; 36 import javax.jms.Session ; 37 import javax.management.MBeanServerConnection ; 38 import javax.management.ObjectName ; 39 import javax.naming.InitialContext ; 40 41 import org.jboss.test.jbossmessaging.JMSTestCase; 42 import org.jboss.util.NestedRuntimeException; 43 44 51 public class ReceiveNackClientStressTestCase extends JMSTestCase implements ExceptionListener 52 { 53 protected QueueConnection queueConnection; 54 55 public ReceiveNackClientStressTestCase(String name) throws Exception 56 { 57 super(name); 58 } 59 60 public void onException(JMSException e) 61 { 62 log.error("Error: ", e); 63 try 64 { 65 queueConnection.close(); 66 } 67 catch (Exception ignored) 68 { 69 } 70 } 71 72 private void drainQueue(String name) throws Exception 73 { 74 InitialContext context = getInitialContext() ; 75 76 QueueSession session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 77 Queue queue = (Queue )context.lookup(name); 78 79 QueueReceiver receiver = session.createReceiver(queue); 80 queueConnection.start(); 81 Message message = receiver.receive(50); 82 int c = 0; 83 while (message != null) 84 { 85 message = receiver.receive(50); 86 c++; 87 } 88 89 if (c != 0) 90 getLog().debug(" Drained " + c + " messages from the queue"); 91 session.close(); 92 queueConnection.stop(); 93 94 } 95 96 public void testImpatient() throws Exception 97 { 98 int target = getIterationCount(); 99 createQueue("Impatient"); 100 drainQueue("Impatient") ; 101 try 102 { 103 InitialContext context = getInitialContext(); 104 QueueConnectionFactory queueFactory = (QueueConnectionFactory ) context.lookup("ConnectionFactory"); 105 Queue queue = (Queue ) context.lookup("Impatient"); 106 queueConnection = queueFactory.createQueueConnection(); 107 try 108 { 109 QueueSession session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 110 QueueSender sender = session.createSender(queue); 111 QueueReceiver receiver = session.createReceiver(queue); 112 Serializable payload = new HashMap (); 113 Message message = session.createObjectMessage(payload); 114 queueConnection.start(); 115 int count = 0; 116 int sendCount = 0; 117 while (count < target) 118 { 119 if (sendCount <= target) 120 { 121 for (int i = 0; i < 10 && ++sendCount <= target; ++i) 122 sender.send(message); 123 } 124 if (receiver.receive(1) != null) 125 ++count; 126 } 127 } 128 finally 129 { 130 queueConnection.close(); 131 } 132 } 133 finally 134 { 135 drainQueue("Impatient") ; 136 deleteQueue("Impatient"); 137 } 138 } 139 } 140 | Popular Tags |