Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/bug/AMQ336Test.java


1   /**
2    * 
3    * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.activemq.bug;
19  
20  import EDU.oswego.cs.dl.util.concurrent.CountDown;
21  
22  import org.activemq.ActiveMQConnectionFactory;
23  
24  import javax.jms.Connection;
25  import javax.jms.DeliveryMode;
26  import javax.jms.JMSException;
27  import javax.jms.Message;
28  import javax.jms.MessageConsumer;
29  import javax.jms.MessageListener;
30  import javax.jms.MessageProducer;
31  import javax.jms.Queue;
32  import javax.jms.Session;
33  
34  import junit.framework.TestCase;
35  
36  /**
37   * Test case for AMQ-336
38   * 
39   * @author Matthew Vincent 
40   * @version $Revision: 1.1 $
41   */
42  public class AMQ336Test extends TestCase {
43      CountDown outstandingMessages = new CountDown(3);
44      Inbox in;
45  
46      class Inbox implements MessageListener {
47          private final ActiveMQConnectionFactory connectionFactory;
48  
49          private Connection connection;
50          private Session session;
51          private Queue destination;
52          private MessageProducer producer;
53          private MessageConsumer consumer;
54          private final boolean durable = false;
55          private String clientID = "clientid";
56  
57          public Inbox() throws JMSException {
58              this.connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
59              connection = connectionFactory.createConnection();
60              if (durable && clientID != null) {
61                  connection.setClientID(clientID);
62              }
63              connection.start();
64              session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
65              destination = session.createQueue("subject");
66              producer = session.createProducer(destination);
67              if (durable) {
68                  producer.setDeliveryMode(DeliveryMode.PERSISTENT);
69              }
70              else {
71                  producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
72              }
73              consumer = session.createConsumer(destination);
74          }
75  
76          public void stop() throws JMSException {
77              if (consumer != null)
78                  consumer.close();
79              if (session != null)
80                  session.close();
81              if (connection != null)
82                  connection.close();
83          }
84  
85          public void receive(int message) throws JMSException {
86              Message jmsMsg = session.createObjectMessage(new Integer(message));
87              // jmsMsg.setJMSPriority(message);
88              producer.send(jmsMsg);
89          }
90  
91          public void startDeliveringMessages() throws JMSException {
92              consumer.setMessageListener(this);
93          }
94  
95          public void onMessage(Message arg0) {
96              outstandingMessages.release();
97          }
98  
99      }
100 
101     protected void setUp() throws Exception {
102         in = new Inbox();
103     }
104 
105     protected void tearDown() throws Exception {
106         if (in != null)
107             in.stop();
108     }
109 
110     public void testDeadlock() throws Exception {
111         in.receive(2);
112         in.receive(1);
113         in.receive(3);
114         in.startDeliveringMessages();
115         outstandingMessages.attempt(5000);
116         assertEquals(0, outstandingMessages.currentCount());
117     }
118 
119 }