001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jbossmq.perf;
023:
024: import java.io.Serializable;
025: import java.util.HashMap;
026:
027: import javax.jms.ExceptionListener;
028: import javax.jms.JMSException;
029: import javax.jms.Message;
030: import javax.jms.Queue;
031: import javax.jms.QueueConnection;
032: import javax.jms.QueueConnectionFactory;
033: import javax.jms.QueueReceiver;
034: import javax.jms.QueueSender;
035: import javax.jms.QueueSession;
036: import javax.jms.Session;
037: import javax.management.MBeanServerConnection;
038: import javax.management.ObjectName;
039: import javax.naming.InitialContext;
040:
041: import org.jboss.test.JBossTestCase;
042: import org.jboss.util.NestedRuntimeException;
043:
044: /**
045: * A stress test for an impatient receiver
046: *
047: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
048: * @version $Revision: 57211 $
049: */
050: public class ReceiveNackClientStressTestCase extends JBossTestCase
051: implements ExceptionListener {
052: protected static final ObjectName destinationManager;
053:
054: protected QueueConnection queueConnection;
055:
056: static {
057: try {
058: destinationManager = new ObjectName(
059: "jboss.mq:service=DestinationManager");
060: } catch (Exception e) {
061: throw new NestedRuntimeException(e);
062: }
063: }
064:
065: public ReceiveNackClientStressTestCase(String name)
066: throws Exception {
067: super (name);
068: }
069:
070: public void onException(JMSException e) {
071: log.error("Error: ", e);
072: try {
073: queueConnection.close();
074: } catch (Exception ignored) {
075: }
076: }
077:
078: public void createQueue(String name) throws Exception {
079: MBeanServerConnection server = getServer();
080: try {
081: server.invoke(destinationManager, "createQueue",
082: new Object[] { name, name }, new String[] {
083: String.class.getName(),
084: String.class.getName() });
085: } catch (Exception ignored) {
086: log.debug("Ignored", ignored);
087: }
088: ObjectName queueName = new ObjectName(
089: "jboss.mq.destination:service=Queue,name=" + name);
090: server.invoke(queueName, "removeAllMessages", null, null);
091: }
092:
093: public void deleteQueue(String name) throws Exception {
094: MBeanServerConnection server = getServer();
095: ObjectName queueName = new ObjectName(
096: "jboss.mq.destination:service=Queue,name=" + name);
097: server.invoke(queueName, "removeAllMessages", null, null);
098: server.invoke(destinationManager, "destroyQueue",
099: new Object[] { name }, new String[] { String.class
100: .getName() });
101: }
102:
103: public void testImpatient() throws Exception {
104: int target = getIterationCount();
105: createQueue("Impatient");
106: try {
107: InitialContext context = getInitialContext();
108: QueueConnectionFactory queueFactory = (QueueConnectionFactory) context
109: .lookup("ConnectionFactory");
110: Queue queue = (Queue) context.lookup("Impatient");
111: queueConnection = queueFactory.createQueueConnection();
112: try {
113: QueueSession session = queueConnection
114: .createQueueSession(false,
115: Session.AUTO_ACKNOWLEDGE);
116: QueueSender sender = session.createSender(queue);
117: QueueReceiver receiver = session.createReceiver(queue);
118: Serializable payload = new HashMap();
119: Message message = session.createObjectMessage(payload);
120: queueConnection.start();
121: int count = 0;
122: int sendCount = 0;
123: while (count < target) {
124: if (sendCount <= target) {
125: for (int i = 0; i < 10 && ++sendCount <= target; ++i)
126: sender.send(message);
127: }
128: if (receiver.receive(1) != null)
129: ++count;
130: }
131: } finally {
132: queueConnection.close();
133: }
134: } finally {
135: deleteQueue("Impatient");
136: }
137: }
138: }
|