resolve AMQ-2033 - patch applied with thanks

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@727353 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary Tully 2008-12-17 12:00:40 +00:00
parent 70f4eba372
commit 9a432f22f9
2 changed files with 290 additions and 238 deletions

View File

@ -94,6 +94,7 @@ public class ActiveMQMessageProducer extends ActiveMQMessageProducerSupport impl
if (session.connection.getProtocolVersion() >= 3 && this.info.getWindowSize() > 0) { if (session.connection.getProtocolVersion() >= 3 && this.info.getWindowSize() > 0) {
producerWindow = new MemoryUsage("Producer Window: " + producerId); producerWindow = new MemoryUsage("Producer Window: " + producerId);
producerWindow.setLimit(this.info.getWindowSize()); producerWindow.setLimit(this.info.getWindowSize());
producerWindow.start();
} }
this.defaultDeliveryMode = Message.DEFAULT_DELIVERY_MODE; this.defaultDeliveryMode = Message.DEFAULT_DELIVERY_MODE;
@ -151,6 +152,9 @@ public class ActiveMQMessageProducer extends ActiveMQMessageProducerSupport impl
public void dispose() { public void dispose() {
if (!closed) { if (!closed) {
this.session.removeProducer(this); this.session.removeProducer(this);
if (producerWindow != null) {
producerWindow.stop();
}
closed = true; closed = true;
} }
} }

View File

@ -1,238 +1,286 @@
/** /**
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. * this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 * 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 not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.activemq; package org.apache.activemq;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import javax.jms.ConnectionFactory; import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode; import javax.jms.DeliveryMode;
import javax.jms.JMSException; import javax.jms.JMSException;
import javax.jms.MessageConsumer; import javax.jms.MessageConsumer;
import javax.jms.MessageProducer; import javax.jms.MessageProducer;
import javax.jms.Session; import javax.jms.Session;
import javax.jms.TextMessage; import javax.jms.TextMessage;
import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.TransportConnector; import org.apache.activemq.broker.TransportConnector;
import org.apache.activemq.broker.region.policy.PolicyEntry; import org.apache.activemq.broker.region.policy.PolicyEntry;
import org.apache.activemq.broker.region.policy.PolicyMap; import org.apache.activemq.broker.region.policy.PolicyMap;
import org.apache.activemq.broker.region.policy.VMPendingQueueMessageStoragePolicy; import org.apache.activemq.broker.region.policy.VMPendingQueueMessageStoragePolicy;
import org.apache.activemq.broker.region.policy.VMPendingSubscriberMessageStoragePolicy; import org.apache.activemq.broker.region.policy.VMPendingSubscriberMessageStoragePolicy;
import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.transport.tcp.TcpTransport; import org.apache.activemq.transport.tcp.TcpTransport;
public class ProducerFlowControlTest extends JmsTestSupport { public class ProducerFlowControlTest extends JmsTestSupport {
ActiveMQQueue queueA = new ActiveMQQueue("QUEUE.A"); ActiveMQQueue queueA = new ActiveMQQueue("QUEUE.A");
ActiveMQQueue queueB = new ActiveMQQueue("QUEUE.B"); ActiveMQQueue queueB = new ActiveMQQueue("QUEUE.B");
private TransportConnector connector; private TransportConnector connector;
private ActiveMQConnection connection; private ActiveMQConnection connection;
public void test2ndPubisherWithProducerWindowSendConnectionThatIsBlocked() throws Exception { public void test2ndPubisherWithProducerWindowSendConnectionThatIsBlocked() throws Exception {
ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory)createConnectionFactory(); ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory)createConnectionFactory();
factory.setProducerWindowSize(1024 * 64); factory.setProducerWindowSize(1024 * 64);
connection = (ActiveMQConnection)factory.createConnection(); connection = (ActiveMQConnection)factory.createConnection();
connections.add(connection); connections.add(connection);
connection.start(); connection.start();
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queueB); MessageConsumer consumer = session.createConsumer(queueB);
// Test sending to Queue A // Test sending to Queue A
// 1 few sends should not block until the producer window is used up. // 1 few sends should not block until the producer window is used up.
fillQueue(queueA); fillQueue(queueA);
// Test sending to Queue B it should not block since the connection // Test sending to Queue B it should not block since the connection
// should not be blocked. // should not be blocked.
CountDownLatch pubishDoneToQeueuB = asyncSendTo(queueB, "Message 1"); CountDownLatch pubishDoneToQeueuB = asyncSendTo(queueB, "Message 1");
assertTrue(pubishDoneToQeueuB.await(2, TimeUnit.SECONDS)); assertTrue(pubishDoneToQeueuB.await(2, TimeUnit.SECONDS));
TextMessage msg = (TextMessage)consumer.receive(); TextMessage msg = (TextMessage)consumer.receive();
assertEquals("Message 1", msg.getText()); assertEquals("Message 1", msg.getText());
msg.acknowledge(); msg.acknowledge();
pubishDoneToQeueuB = asyncSendTo(queueB, "Message 2"); pubishDoneToQeueuB = asyncSendTo(queueB, "Message 2");
assertTrue(pubishDoneToQeueuB.await(2, TimeUnit.SECONDS)); assertTrue(pubishDoneToQeueuB.await(2, TimeUnit.SECONDS));
msg = (TextMessage)consumer.receive(); msg = (TextMessage)consumer.receive();
assertEquals("Message 2", msg.getText()); assertEquals("Message 2", msg.getText());
msg.acknowledge(); msg.acknowledge();
} }
public void test2ndPubisherWithSyncSendConnectionThatIsBlocked() throws Exception { public void testPubisherRecoverAfterBlock() throws Exception {
ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory)createConnectionFactory(); ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory)createConnectionFactory();
factory.setAlwaysSyncSend(true); factory.setProducerWindowSize(1024 * 64);
connection = (ActiveMQConnection)factory.createConnection(); factory.setUseAsyncSend(true);
connections.add(connection); connection = (ActiveMQConnection)factory.createConnection();
connection.start(); connections.add(connection);
connection.start();
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queueB); final Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
final MessageProducer producer = session.createProducer(queueA);
// Test sending to Queue A
// 1st send should not block. But the rest will. final AtomicBoolean done = new AtomicBoolean(true);
fillQueue(queueA); final AtomicBoolean keepGoing = new AtomicBoolean(true);
Thread thread = new Thread("Filler") {
// Test sending to Queue B it should not block. @Override
CountDownLatch pubishDoneToQeueuB = asyncSendTo(queueB, "Message 1"); public void run() {
assertTrue(pubishDoneToQeueuB.await(2, TimeUnit.SECONDS)); while (keepGoing.get()) {
done.set(false);
TextMessage msg = (TextMessage)consumer.receive(); try {
assertEquals("Message 1", msg.getText()); producer.send(session.createTextMessage("Test message"));
msg.acknowledge(); } catch (JMSException e) {
}
pubishDoneToQeueuB = asyncSendTo(queueB, "Message 2"); }
assertTrue(pubishDoneToQeueuB.await(2, TimeUnit.SECONDS)); }
};
msg = (TextMessage)consumer.receive(); thread.start();
assertEquals("Message 2", msg.getText()); while (true) {
msg.acknowledge(); Thread.sleep(1000);
} // the producer is blocked once the done flag stays true.
if (done.get()) {
public void testSimpleSendReceive() throws Exception { break;
ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory)createConnectionFactory(); }
factory.setAlwaysSyncSend(true); done.set(true);
connection = (ActiveMQConnection)factory.createConnection(); }
connections.add(connection);
connection.start(); // after receiveing messges, producer should continue sending messages
// (done == false)
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(queueA);
MessageConsumer consumer = session.createConsumer(queueA); TextMessage msg;
for (int idx = 0; idx < 5; ++idx) {
// Test sending to Queue B it should not block. msg = (TextMessage) consumer.receive(1000);
CountDownLatch pubishDoneToQeueuA = asyncSendTo(queueA, "Message 1"); msg.acknowledge();
assertTrue(pubishDoneToQeueuA.await(2, TimeUnit.SECONDS)); }
Thread.sleep(1000);
TextMessage msg = (TextMessage)consumer.receive(); keepGoing.set(false);
assertEquals("Message 1", msg.getText());
msg.acknowledge(); assertFalse(done.get());
}
pubishDoneToQeueuA = asyncSendTo(queueA, "Message 2"); public void test2ndPubisherWithSyncSendConnectionThatIsBlocked() throws Exception {
assertTrue(pubishDoneToQeueuA.await(2, TimeUnit.SECONDS)); ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory)createConnectionFactory();
factory.setAlwaysSyncSend(true);
msg = (TextMessage)consumer.receive(); connection = (ActiveMQConnection)factory.createConnection();
assertEquals("Message 2", msg.getText()); connections.add(connection);
msg.acknowledge(); connection.start();
}
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
public void test2ndPubisherWithStandardConnectionThatIsBlocked() throws Exception { MessageConsumer consumer = session.createConsumer(queueB);
ConnectionFactory factory = createConnectionFactory();
connection = (ActiveMQConnection)factory.createConnection(); // Test sending to Queue A
connections.add(connection); // 1st send should not block. But the rest will.
connection.start(); fillQueue(queueA);
// Test sending to Queue A // Test sending to Queue B it should not block.
// 1st send should not block. CountDownLatch pubishDoneToQeueuB = asyncSendTo(queueB, "Message 1");
fillQueue(queueA); assertTrue(pubishDoneToQeueuB.await(2, TimeUnit.SECONDS));
// Test sending to Queue B it should block. TextMessage msg = (TextMessage)consumer.receive();
// Since even though the it's queue limits have not been reached, the assertEquals("Message 1", msg.getText());
// connection msg.acknowledge();
// is blocked.
CountDownLatch pubishDoneToQeueuB = asyncSendTo(queueB, "Message 1"); pubishDoneToQeueuB = asyncSendTo(queueB, "Message 2");
assertFalse(pubishDoneToQeueuB.await(2, TimeUnit.SECONDS)); assertTrue(pubishDoneToQeueuB.await(2, TimeUnit.SECONDS));
}
msg = (TextMessage)consumer.receive();
private void fillQueue(final ActiveMQQueue queue) throws JMSException, InterruptedException { assertEquals("Message 2", msg.getText());
final AtomicBoolean done = new AtomicBoolean(true); msg.acknowledge();
final AtomicBoolean keepGoing = new AtomicBoolean(true); }
// Starts an async thread that every time it publishes it sets the done public void testSimpleSendReceive() throws Exception {
// flag to false. ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory)createConnectionFactory();
// Once the send starts to block it will not reset the done flag factory.setAlwaysSyncSend(true);
// anymore. connection = (ActiveMQConnection)factory.createConnection();
new Thread("Fill thread.") { connections.add(connection);
public void run() { connection.start();
Session session = null;
try { Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(queueA);
MessageProducer producer = session.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // Test sending to Queue B it should not block.
while (keepGoing.get()) { CountDownLatch pubishDoneToQeueuA = asyncSendTo(queueA, "Message 1");
done.set(false); assertTrue(pubishDoneToQeueuA.await(2, TimeUnit.SECONDS));
producer.send(session.createTextMessage("Hello World"));
} TextMessage msg = (TextMessage)consumer.receive();
} catch (JMSException e) { assertEquals("Message 1", msg.getText());
} finally { msg.acknowledge();
safeClose(session);
} pubishDoneToQeueuA = asyncSendTo(queueA, "Message 2");
} assertTrue(pubishDoneToQeueuA.await(2, TimeUnit.SECONDS));
}.start();
msg = (TextMessage)consumer.receive();
while (true) { assertEquals("Message 2", msg.getText());
Thread.sleep(1000); msg.acknowledge();
// the producer is blocked once the done flag stays true. }
if (done.get()) {
break; public void test2ndPubisherWithStandardConnectionThatIsBlocked() throws Exception {
} ConnectionFactory factory = createConnectionFactory();
done.set(true); connection = (ActiveMQConnection)factory.createConnection();
} connections.add(connection);
keepGoing.set(false); connection.start();
}
// Test sending to Queue A
private CountDownLatch asyncSendTo(final ActiveMQQueue queue, final String message) throws JMSException { // 1st send should not block.
final CountDownLatch done = new CountDownLatch(1); fillQueue(queueA);
new Thread("Send thread.") {
public void run() { // Test sending to Queue B it should block.
Session session = null; // Since even though the it's queue limits have not been reached, the
try { // connection
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // is blocked.
MessageProducer producer = session.createProducer(queue); CountDownLatch pubishDoneToQeueuB = asyncSendTo(queueB, "Message 1");
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); assertFalse(pubishDoneToQeueuB.await(2, TimeUnit.SECONDS));
producer.send(session.createTextMessage(message)); }
done.countDown();
} catch (JMSException e) { private void fillQueue(final ActiveMQQueue queue) throws JMSException, InterruptedException {
} finally { final AtomicBoolean done = new AtomicBoolean(true);
safeClose(session); final AtomicBoolean keepGoing = new AtomicBoolean(true);
}
} // Starts an async thread that every time it publishes it sets the done
}.start(); // flag to false.
return done; // Once the send starts to block it will not reset the done flag
} // anymore.
new Thread("Fill thread.") {
protected BrokerService createBroker() throws Exception { public void run() {
BrokerService service = new BrokerService(); Session session = null;
service.setPersistent(false); try {
service.setUseJmx(false); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
// Setup a destination policy where it takes only 1 message at a time. producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
PolicyMap policyMap = new PolicyMap(); while (keepGoing.get()) {
PolicyEntry policy = new PolicyEntry(); done.set(false);
policy.setMemoryLimit(1); producer.send(session.createTextMessage("Hello World"));
policy.setPendingSubscriberPolicy(new VMPendingSubscriberMessageStoragePolicy()); }
policy.setPendingQueuePolicy(new VMPendingQueueMessageStoragePolicy()); } catch (JMSException e) {
policyMap.setDefaultEntry(policy); } finally {
service.setDestinationPolicy(policyMap); safeClose(session);
}
connector = service.addConnector("tcp://localhost:0"); }
return service; }.start();
}
while (true) {
protected void tearDown() throws Exception { Thread.sleep(1000);
TcpTransport t = (TcpTransport)connection.getTransport().narrow(TcpTransport.class); // the producer is blocked once the done flag stays true.
t.getTransportListener().onException(new IOException("Disposed.")); if (done.get()) {
connection.getTransport().stop(); break;
super.tearDown(); }
} done.set(true);
}
protected ConnectionFactory createConnectionFactory() throws Exception { keepGoing.set(false);
return new ActiveMQConnectionFactory(connector.getConnectUri()); }
}
} private CountDownLatch asyncSendTo(final ActiveMQQueue queue, final String message) throws JMSException {
final CountDownLatch done = new CountDownLatch(1);
new Thread("Send thread.") {
public void run() {
Session session = null;
try {
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.send(session.createTextMessage(message));
done.countDown();
} catch (JMSException e) {
} finally {
safeClose(session);
}
}
}.start();
return done;
}
protected BrokerService createBroker() throws Exception {
BrokerService service = new BrokerService();
service.setPersistent(false);
service.setUseJmx(false);
// Setup a destination policy where it takes only 1 message at a time.
PolicyMap policyMap = new PolicyMap();
PolicyEntry policy = new PolicyEntry();
policy.setMemoryLimit(1);
policy.setPendingSubscriberPolicy(new VMPendingSubscriberMessageStoragePolicy());
policy.setPendingQueuePolicy(new VMPendingQueueMessageStoragePolicy());
policyMap.setDefaultEntry(policy);
service.setDestinationPolicy(policyMap);
connector = service.addConnector("tcp://localhost:0");
return service;
}
protected void tearDown() throws Exception {
TcpTransport t = (TcpTransport)connection.getTransport().narrow(TcpTransport.class);
t.getTransportListener().onException(new IOException("Disposed."));
connection.getTransport().stop();
super.tearDown();
}
protected ConnectionFactory createConnectionFactory() throws Exception {
return new ActiveMQConnectionFactory(connector.getConnectUri());
}
}