KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmessaging > test > ConcurrentDeliveryUnitTestCase


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.test;
23
24 import javax.jms.Message JavaDoc;
25 import javax.jms.MessageListener JavaDoc;
26 import javax.jms.QueueConnection JavaDoc;
27 import javax.jms.QueueConnectionFactory JavaDoc;
28 import javax.jms.QueueReceiver JavaDoc;
29 import javax.jms.QueueSender JavaDoc;
30 import javax.jms.QueueSession JavaDoc;
31 import javax.jms.Session JavaDoc;
32 import javax.jms.TemporaryQueue JavaDoc;
33 import javax.naming.Context JavaDoc;
34
35 import org.jboss.test.jbossmessaging.JMSTestCase;
36
37 /**
38  * Concurrent delivery tests
39  *
40  * @author <a HREF="mailto:richard.achmatowicz@jboss.org">Richard Achmatowicz</a>
41  * @author <a HREF="mailto:adrian@jboss.org>Adrian Brock</a>
42  * @version <tt>$Revision: 37406 $</tt>
43  */

44 public class ConcurrentDeliveryUnitTestCase extends JMSTestCase
45 {
46    static String JavaDoc QUEUE_FACTORY = "ConnectionFactory";
47
48    QueueConnection JavaDoc queueConnection;
49
50    int completed = 0;
51    boolean inDelivery = false;
52    boolean concurrent = false;
53    
54    public ConcurrentDeliveryUnitTestCase(String JavaDoc name) throws Exception JavaDoc
55    {
56       super(name);
57    }
58
59    public void testConcurrentDelivery() throws Exception JavaDoc
60    {
61       connect();
62       try
63       {
64          MyMessageListener messageListener = new MyMessageListener();
65          
66          QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
67          TemporaryQueue JavaDoc queue1 = session.createTemporaryQueue();
68          QueueSender JavaDoc sender1 = session.createSender(queue1);
69          QueueReceiver JavaDoc receiver1 = session.createReceiver(queue1);
70          receiver1.setMessageListener(messageListener);
71          TemporaryQueue JavaDoc queue2 = session.createTemporaryQueue();
72          QueueSender JavaDoc sender2 = session.createSender(queue2);
73          QueueReceiver JavaDoc receiver2 = session.createReceiver(queue2);
74          receiver2.setMessageListener(messageListener);
75          Message JavaDoc message = session.createMessage();
76          queueConnection.start();
77
78          sender1.send(message);
79          sender2.send(message);
80
81          synchronized (messageListener)
82          {
83             while (completed < 2)
84             {
85                getLog().debug("Waiting for completion " + completed);
86                messageListener.wait();
87             }
88          }
89          getLog().debug("Completed");
90          
91          if (concurrent)
92             fail("Concurrent delivery");
93       }
94       finally
95       {
96          disconnect();
97       }
98    }
99
100    protected void connect() throws Exception JavaDoc
101    {
102       Context JavaDoc context = getInitialContext();
103       QueueConnectionFactory JavaDoc queueFactory = (QueueConnectionFactory JavaDoc) context.lookup(QUEUE_FACTORY);
104       queueConnection = queueFactory.createQueueConnection();
105
106       getLog().debug("Connection established.");
107    }
108
109    protected void disconnect()
110    {
111       try
112       {
113          if (queueConnection != null)
114             queueConnection.close();
115       }
116       catch (Throwable JavaDoc ignored)
117       {
118          getLog().warn("Ignored", ignored);
119       }
120
121       getLog().debug("Connection closed.");
122    }
123
124    public class MyMessageListener implements MessageListener JavaDoc
125    {
126       public void onMessage(Message JavaDoc message)
127       {
128          synchronized (this)
129          {
130             if (inDelivery)
131                concurrent = true;
132             inDelivery = true;
133             getLog().debug("In delivery " + message);
134          }
135          
136          try
137          {
138             Thread.sleep(10000);
139          }
140          catch (Throwable JavaDoc ignored)
141          {
142             getLog().warn("Ignored ", ignored);
143          }
144          
145          synchronized (this)
146          {
147             inDelivery = false;
148             ++completed;
149             notifyAll();
150             getLog().debug("Completed " + message);
151          }
152       }
153    }
154 }
155
156
Popular Tags