You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by rg...@apache.org on 2006/12/14 22:55:52 UTC

svn commit: r487368 - in /incubator/qpid/trunk/qpid/java/client/src: main/java/org/apache/qpid/client/AMQSession.java test/java/org/apache/qpid/test/unit/basic/PubSubTwoConnectionTest.java

Author: rgreig
Date: Thu Dec 14 13:55:51 2006
New Revision: 487368

URL: http://svn.apache.org/viewvc?view=rev&rev=487368
Log:
QPID-193 Make createConsumer synchronous to avoid races in client code.

Added:
    incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/basic/PubSubTwoConnectionTest.java   (with props)
Modified:
    incubator/qpid/trunk/qpid/java/client/src/main/java/org/apache/qpid/client/AMQSession.java

Modified: incubator/qpid/trunk/qpid/java/client/src/main/java/org/apache/qpid/client/AMQSession.java
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/client/src/main/java/org/apache/qpid/client/AMQSession.java?view=diff&rev=487368&r1=487367&r2=487368
==============================================================================
--- incubator/qpid/trunk/qpid/java/client/src/main/java/org/apache/qpid/client/AMQSession.java (original)
+++ incubator/qpid/trunk/qpid/java/client/src/main/java/org/apache/qpid/client/AMQSession.java Thu Dec 14 13:55:51 2006
@@ -870,7 +870,7 @@
 
                 try
                 {
-                    registerConsumer(consumer);
+                    registerConsumer(consumer, false);
                 }
                 catch (AMQException e)
                 {
@@ -948,7 +948,7 @@
      * @return the consumer tag generated by the broker
      */
     private String consumeFromQueue(String queueName, AMQProtocolHandler protocolHandler, int prefetchHigh, int prefetchLow,
-                                    boolean noLocal, boolean exclusive, int acknowledgeMode) throws AMQException
+                                    boolean noLocal, boolean exclusive, int acknowledgeMode, boolean nowait) throws AMQException
     {
         //fixme prefetch values are not used here. Do we need to have them as parametsrs?
         //need to generate a consumer tag on the client so we can exploit the nowait flag
@@ -957,9 +957,15 @@
         AMQFrame jmsConsume = BasicConsumeBody.createAMQFrame(_channelId, 0,
                                                               queueName, tag, noLocal,
                                                               acknowledgeMode == Session.NO_ACKNOWLEDGE,
-                                                              exclusive, true);
-
-        protocolHandler.writeFrame(jmsConsume);
+                                                              exclusive, nowait);
+        if (nowait)
+        {
+            protocolHandler.writeFrame(jmsConsume);
+        }
+        else
+        {
+            protocolHandler.syncWrite(jmsConsume, BasicConsumeOkBody.class);
+        }
         return tag;
     }
 
@@ -1263,7 +1269,7 @@
      * @param consumer
      * @throws AMQException
      */
-    void registerConsumer(BasicMessageConsumer consumer) throws AMQException
+    void registerConsumer(BasicMessageConsumer consumer, boolean nowait) throws AMQException
     {
         AMQDestination amqd = consumer.getDestination();
 
@@ -1275,8 +1281,9 @@
 
         bindQueue(amqd, queueName, protocolHandler, consumer.getRawSelectorFieldTable());
 
-        String consumerTag = consumeFromQueue(queueName, protocolHandler, consumer.getPrefetchHigh(), consumer.getPrefetchLow(),
-                                              consumer.isNoLocal(), consumer.isExclusive(), consumer.getAcknowledgeMode());
+        String consumerTag = consumeFromQueue(queueName, protocolHandler, consumer.getPrefetchHigh(),
+                                              consumer.getPrefetchLow(), consumer.isNoLocal(), consumer.isExclusive(),
+                                              consumer.getAcknowledgeMode(), nowait);
 
         consumer.setConsumerTag(consumerTag);
         _consumers.put(consumerTag, consumer);
@@ -1338,7 +1345,7 @@
         for (Iterator it = consumers.iterator(); it.hasNext();)
         {
             BasicMessageConsumer consumer = (BasicMessageConsumer) it.next();
-            registerConsumer(consumer);
+            registerConsumer(consumer, true);
         }
     }
 

Added: incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/basic/PubSubTwoConnectionTest.java
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/basic/PubSubTwoConnectionTest.java?view=auto&rev=487368
==============================================================================
--- incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/basic/PubSubTwoConnectionTest.java (added)
+++ incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/basic/PubSubTwoConnectionTest.java Thu Dec 14 13:55:51 2006
@@ -0,0 +1,67 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.qpid.test.unit.basic;
+
+import junit.framework.TestCase;
+import org.apache.qpid.client.transport.TransportConnection;
+import org.apache.qpid.client.AMQConnection;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.client.AMQTopic;
+
+import javax.jms.*;
+
+/**
+ * @author Apache Software Foundation
+ */
+public class PubSubTwoConnectionTest extends TestCase
+{
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        TransportConnection.createVMBroker(1);
+    }
+
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+    }
+
+    /**
+     * This tests that a consumer is set up synchronously
+     * @throws Exception
+     */
+    public void testTwoConnections() throws Exception
+    {
+        Topic topic = new AMQTopic("MyTopic");
+        Connection con1 = new AMQConnection("vm://:1", "guest", "guest", "Client1", "/test_path");
+        Session session1 = con1.createSession(false, AMQSession.NO_ACKNOWLEDGE);
+        MessageProducer producer = session1.createProducer(topic);
+
+        Connection con2 = new AMQConnection("vm://:1", "guest", "guest", "Client2", "/test_path");
+        Session session2 = con2.createSession(false, AMQSession.NO_ACKNOWLEDGE);
+        MessageConsumer consumer = session2.createConsumer(topic);
+        con2.start();        
+        producer.send(session1.createTextMessage("Hello"));
+        TextMessage tm1 = (TextMessage) consumer.receive(2000);
+        assertNotNull(tm1);
+        assertEquals("Hello", tm1.getText());
+    }
+}

Propchange: incubator/qpid/trunk/qpid/java/client/src/test/java/org/apache/qpid/test/unit/basic/PubSubTwoConnectionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native