KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmq > perf > ReceiveNackClientStressTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jbossmq.perf;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 import javax.jms.ExceptionListener JavaDoc;
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.Message JavaDoc;
30 import javax.jms.Queue JavaDoc;
31 import javax.jms.QueueConnection JavaDoc;
32 import javax.jms.QueueConnectionFactory JavaDoc;
33 import javax.jms.QueueReceiver JavaDoc;
34 import javax.jms.QueueSender JavaDoc;
35 import javax.jms.QueueSession JavaDoc;
36 import javax.jms.Session JavaDoc;
37 import javax.management.MBeanServerConnection JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39 import javax.naming.InitialContext JavaDoc;
40
41 import org.jboss.test.JBossTestCase;
42 import org.jboss.util.NestedRuntimeException;
43
44 /**
45  * A stress test for an impatient receiver
46  *
47  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
48  * @version $Revision: 38400 $
49  */

50 public class ReceiveNackClientStressTestCase extends JBossTestCase implements ExceptionListener JavaDoc
51 {
52    protected static final ObjectName JavaDoc destinationManager;
53    
54    protected QueueConnection JavaDoc queueConnection;
55    
56    static
57    {
58       try
59       {
60          destinationManager = new ObjectName JavaDoc("jboss.mq:service=DestinationManager");
61       }
62       catch (Exception JavaDoc e)
63       {
64          throw new NestedRuntimeException(e);
65       }
66    }
67
68    public ReceiveNackClientStressTestCase(String JavaDoc name) throws Exception JavaDoc
69    {
70       super(name);
71    }
72
73    public void onException(JMSException JavaDoc e)
74    {
75       log.error("Error: ", e);
76       try
77       {
78          queueConnection.close();
79       }
80       catch (Exception JavaDoc ignored)
81       {
82       }
83    }
84    
85    public void createQueue(String JavaDoc name) throws Exception JavaDoc
86    {
87       MBeanServerConnection JavaDoc server = getServer();
88       try
89       {
90          server.invoke(destinationManager, "createQueue", new Object JavaDoc[] { name, name }, new String JavaDoc[] { String JavaDoc.class.getName(), String JavaDoc.class.getName() } );
91       }
92       catch (Exception JavaDoc ignored)
93       {
94          log.debug("Ignored", ignored);
95       }
96       ObjectName JavaDoc queueName = new ObjectName JavaDoc("jboss.mq.destination:service=Queue,name=" + name);
97       server.invoke(queueName, "removeAllMessages", null, null);
98    }
99
100    public void deleteQueue(String JavaDoc name) throws Exception JavaDoc
101    {
102       MBeanServerConnection JavaDoc server = getServer();
103       ObjectName JavaDoc queueName = new ObjectName JavaDoc("jboss.mq.destination:service=Queue,name=" + name);
104       server.invoke(queueName, "removeAllMessages", null, null);
105       server.invoke(destinationManager, "destroyQueue", new Object JavaDoc[] { name }, new String JavaDoc[] { String JavaDoc.class.getName() } );
106    }
107
108    public void testImpatient() throws Exception JavaDoc
109    {
110       int target = getIterationCount();
111       createQueue("Impatient");
112       try
113       {
114          InitialContext JavaDoc context = getInitialContext();
115          QueueConnectionFactory JavaDoc queueFactory = (QueueConnectionFactory JavaDoc) context.lookup("ConnectionFactory");
116          Queue JavaDoc queue = (Queue JavaDoc) context.lookup("Impatient");
117          queueConnection = queueFactory.createQueueConnection();
118          try
119          {
120             QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
121             QueueSender JavaDoc sender = session.createSender(queue);
122             QueueReceiver JavaDoc receiver = session.createReceiver(queue);
123             Serializable JavaDoc payload = new HashMap JavaDoc();
124             Message JavaDoc 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