This closes #116

This commit is contained in:
Timothy Bish 2015-06-17 19:10:31 -04:00
commit 1e8621fede
2 changed files with 130 additions and 29 deletions

View File

@ -314,10 +314,18 @@ public abstract class AbstractStoreCursor extends AbstractPendingMessageCursor i
}
private void setLastCachedId(final int index, MessageId candidate) {
if (lastCachedIds[index] == null || lastCachedIds[index].getFutureOrSequenceLong() == null) { // possibly null for topics
MessageId lastCacheId = lastCachedIds[index];
if (lastCacheId == null) {
lastCachedIds[index] = candidate;
} else if (Long.compare(((Long) candidate.getFutureOrSequenceLong()), ((Long) lastCachedIds[index].getFutureOrSequenceLong())) > 0) {
} else {
Object lastCacheFutureOrSequenceLong = lastCacheId.getFutureOrSequenceLong();
Object candidateOrSequenceLong = candidate.getFutureOrSequenceLong();
if (lastCacheFutureOrSequenceLong == null) { // possibly null for topics
lastCachedIds[index] = candidate;
} else if (candidateOrSequenceLong != null &&
Long.compare(((Long) candidateOrSequenceLong), ((Long) lastCacheFutureOrSequenceLong)) > 0) {
lastCachedIds[index] = candidate;
}
}
}

View File

@ -0,0 +1,93 @@
/**
* 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.activemq.broker.region.cursors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.apache.activemq.test.TestSupport;
/**
* This test shows that a null pointer exception will not occur when unsubscribing from a
* subscription while a producer is sending messages rapidly to the topic. A null pointer
* exception was occurring in the setLastCachedId method of AbstractMessageCursor due to
* a race condition. If this test is run before the patch that is applied in this commit
* on AbstractStoreCusor, it will consistently fail with a NPE.
*
*/
public class AbstractStoreCursorNpeTest extends TestSupport {
protected Connection connection;
protected Session session;
protected MessageConsumer consumer;
protected MessageProducer producer;
protected Topic destination;
public void testSetLastCachedIdNPE() throws Exception {
connection = createConnection();
connection.setClientID("clientId");
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createTopic("test.topic");
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
Connection durableCon = createConnection();
durableCon.setClientID("testCons");
durableCon.start();
final Session durSession = durableCon.createSession(false, Session.AUTO_ACKNOWLEDGE);
//In a new thread rapidly subscribe and unsubscribe to a durable
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(new Runnable() {
@Override
public void run() {
try{
//Repeatedly create a durable subscription and then unsubscribe which used to
//cause a NPE while messages were sending
while(true) {
MessageConsumer cons = durSession.createDurableSubscriber(durSession.createTopic("test.topic"), "sub1");
Thread.sleep(100);
cons.close();
durSession.unsubscribe("sub1");
}
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
});
TextMessage myMessage = new ActiveMQTextMessage();
myMessage.setText("test");
//Make sure that we can send a bunch of messages without a NPE
//This would fail if the patch is not applied
for (int i = 0; i < 10000; i++) {
producer.send(myMessage);
}
}
}