1 22 package org.jboss.test.jbossmq.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.JBossTestCase; 42 import org.jboss.util.NestedRuntimeException; 43 44 50 public class ReceiveNackClientStressTestCase extends JBossTestCase implements ExceptionListener 51 { 52 protected static final ObjectName destinationManager; 53 54 protected QueueConnection queueConnection; 55 56 static 57 { 58 try 59 { 60 destinationManager = new ObjectName ("jboss.mq:service=DestinationManager"); 61 } 62 catch (Exception e) 63 { 64 throw new NestedRuntimeException(e); 65 } 66 } 67 68 public ReceiveNackClientStressTestCase(String name) throws Exception 69 { 70 super(name); 71 } 72 73 public void onException(JMSException e) 74 { 75 log.error("Error: ", e); 76 try 77 { 78 queueConnection.close(); 79 } 80 catch (Exception ignored) 81 { 82 } 83 } 84 85 public void createQueue(String name) throws Exception 86 { 87 MBeanServerConnection server = getServer(); 88 try 89 { 90 server.invoke(destinationManager, "createQueue", new Object [] { name, name }, new String [] { String .class.getName(), String .class.getName() } ); 91 } 92 catch (Exception ignored) 93 { 94 log.debug("Ignored", ignored); 95 } 96 ObjectName queueName = new ObjectName ("jboss.mq.destination:service=Queue,name=" + name); 97 server.invoke(queueName, "removeAllMessages", null, null); 98 } 99 100 public void deleteQueue(String name) throws Exception 101 { 102 MBeanServerConnection server = getServer(); 103 ObjectName queueName = new ObjectName ("jboss.mq.destination:service=Queue,name=" + name); 104 server.invoke(queueName, "removeAllMessages", null, null); 105 server.invoke(destinationManager, "destroyQueue", new Object [] { name }, new String [] { String .class.getName() } ); 106 } 107 108 public void testImpatient() throws Exception 109 { 110 int target = getIterationCount(); 111 createQueue("Impatient"); 112 try 113 { 114 InitialContext context = getInitialContext(); 115 QueueConnectionFactory queueFactory = (QueueConnectionFactory ) context.lookup("ConnectionFactory"); 116 Queue queue = (Queue ) context.lookup("Impatient"); 117 queueConnection = queueFactory.createQueueConnection(); 118 try 119 { 120 QueueSession session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 121 QueueSender sender = session.createSender(queue); 122 QueueReceiver receiver = session.createReceiver(queue); 123 Serializable payload = new HashMap (); 124 Message message = session.createObjectMessage(payload); 125 queueConnection.start(); 126 int count = 0; 127 int sendCount = 0; 128 while (count < target) 129 { 130 if (sendCount <= target) 131 { 132 for (int i = 0; i < 10 && ++sendCount <= target; ++i) 133 sender.send(message); 134 } 135 if (receiver.receive(1) != null) 136 ++count; 137 } 138 } 139 finally 140 { 141 queueConnection.close(); 142 } 143 } 144 finally 145 { 146 deleteQueue("Impatient"); 147 } 148 } 149 } 150 | Popular Tags |