KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmessaging > 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.jbossmessaging.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.jbossmessaging.JMSTestCase;
42 import org.jboss.util.NestedRuntimeException;
43
44 /**
45  * A stress test for an impatient receiver
46  *
47  * @author <a HREF="mailto:richard.achmatowicz@jboss.com">Richard Achmatowicz</a>
48  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
49  * @version $Revision: 38400 $
50  */

51 public class ReceiveNackClientStressTestCase extends JMSTestCase implements ExceptionListener JavaDoc
52 {
53    protected QueueConnection JavaDoc queueConnection;
54    
55    public ReceiveNackClientStressTestCase(String JavaDoc name) throws Exception JavaDoc
56    {
57       super(name);
58    }
59
60    public void onException(JMSException JavaDoc e)
61    {
62       log.error("Error: ", e);
63       try
64       {
65          queueConnection.close();
66       }
67       catch (Exception JavaDoc ignored)
68       {
69       }
70    }
71    
72     private void drainQueue(String JavaDoc name) throws Exception JavaDoc
73     {
74     InitialContext JavaDoc context = getInitialContext() ;
75
76     QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
77     Queue JavaDoc queue = (Queue JavaDoc)context.lookup(name);
78
79     QueueReceiver JavaDoc receiver = session.createReceiver(queue);
80     queueConnection.start();
81     Message JavaDoc 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 JavaDoc
97    {
98       int target = getIterationCount();
99       createQueue("Impatient");
100       drainQueue("Impatient") ;
101       try
102       {
103          InitialContext JavaDoc context = getInitialContext();
104          QueueConnectionFactory JavaDoc queueFactory = (QueueConnectionFactory JavaDoc) context.lookup("ConnectionFactory");
105          Queue JavaDoc queue = (Queue JavaDoc) context.lookup("Impatient");
106          queueConnection = queueFactory.createQueueConnection();
107          try
108          {
109             QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
110             QueueSender JavaDoc sender = session.createSender(queue);
111             QueueReceiver JavaDoc receiver = session.createReceiver(queue);
112             Serializable JavaDoc payload = new HashMap JavaDoc();
113             Message JavaDoc 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