git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@661295 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Davies 2008-05-29 11:27:33 +00:00
parent 4dde3239ff
commit 0f1ef01d1b
3 changed files with 165 additions and 282 deletions

View File

@ -1062,7 +1062,12 @@ public class Queue extends BaseDestination implements Task {
}
final void sendMessage(final ConnectionContext context, Message msg) throws Exception {
if (!msg.isPersistent() && messages.getSystemUsage() != null) {
messages.getSystemUsage().getTempUsage().waitForSpace();
}
synchronized(messages) {
messages.addMessageLast(msg);
}
destinationStatistics.getEnqueues().increment();
destinationStatistics.getMessages().increment();
messageDelivered(context, msg);

View File

@ -21,7 +21,6 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.ConnectionContext;
@ -60,7 +59,6 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
private boolean flushRequired;
private AtomicBoolean started = new AtomicBoolean();
private MessageReference last = null;
private ReentrantLock lock = new ReentrantLock(true);
/**
* @param name
@ -95,9 +93,7 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
/**
* @return true if there are no pending messages
*/
public boolean isEmpty() {
lock.lock();
try {
public synchronized boolean isEmpty() {
if(memoryList.isEmpty() && isDiskListEmpty()){
return true;
}
@ -112,9 +108,6 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
// We can remove dropped references.
iterator.remove();
}
} finally {
lock.unlock();
}
return isDiskListEmpty();
}
@ -123,36 +116,21 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
/**
* reset the cursor
*/
public void reset() {
lock.lock();
try {
public synchronized void reset() {
iterating = true;
last = null;
iter = isDiskListEmpty() ? memoryList.iterator() : getDiskList().listIterator();
} finally {
lock.unlock();
}
}
public void release() {
lock.lock();
try {
synchronized(this) {
public synchronized void release() {
iterating = false;
this.notifyAll();
}
if (flushRequired) {
flushRequired = false;
flushToDisk();
}
} finally {
lock.unlock();
}
}
public void destroy() throws Exception {
lock.lock();
try {
public synchronized void destroy() throws Exception {
stop();
for (Iterator<MessageReference> i = memoryList.iterator(); i.hasNext();) {
Message node = (Message)i.next();
@ -162,16 +140,11 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
if (!isDiskListEmpty()) {
getDiskList().clear();
}
} finally {
lock.unlock();
}
}
public LinkedList<MessageReference> pageInList(int maxItems) {
int count = 0;
public synchronized LinkedList<MessageReference> pageInList(int maxItems) {
LinkedList<MessageReference> result = new LinkedList<MessageReference>();
lock.lock();
try {
int count = 0;
for (Iterator<MessageReference> i = memoryList.iterator(); i.hasNext() && count < maxItems;) {
result.add(i.next());
count++;
@ -186,9 +159,6 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
count++;
}
}
} finally {
lock.unlock();
}
return result;
}
@ -197,20 +167,9 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
*
* @param node
*/
public void addMessageLast(MessageReference node) {
public synchronized void addMessageLast(MessageReference node) {
if (!node.isExpired()) {
try {
lock.lock();
try {
while (iterating) {
lock.unlock();
synchronized(this) {
try {
this.wait();
} catch (InterruptedException ie) {}
}
lock.lock();
}
regionDestination = node.getMessage().getRegionDestination();
if (isDiskListEmpty()) {
if (hasSpace() || this.store==null) {
@ -231,18 +190,12 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
}
}
}
if (systemUsage.getTempUsage().isFull()) {
lock.unlock();
systemUsage.getTempUsage().waitForSpace();
lock.lock();
}
getDiskList().add(node);
} finally {
lock.unlock();
}
} catch (Exception e) {
LOG.error("Caught an Exception adding a message: " + node
+ " last to FilePendingMessageCursor ", e);
+ " first to FilePendingMessageCursor ", e);
throw new RuntimeException(e);
}
} else {
@ -255,20 +208,9 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
*
* @param node
*/
public void addMessageFirst(MessageReference node) {
public synchronized void addMessageFirst(MessageReference node) {
if (!node.isExpired()) {
try {
lock.lock();
try {
while (iterating) {
lock.unlock();
synchronized(this) {
try {
this.wait();
} catch (InterruptedException ie) {}
}
lock.lock();
}
regionDestination = node.getMessage().getRegionDestination();
if (isDiskListEmpty()) {
if (hasSpace()) {
@ -289,16 +231,9 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
}
}
}
if (systemUsage.getTempUsage().isFull()) {
lock.unlock();
systemUsage.getTempUsage().waitForSpace();
lock.lock();
}
node.decrementReferenceCount();
getDiskList().addFirst(node);
} finally {
lock.unlock();
}
} catch (Exception e) {
LOG.error("Caught an Exception adding a message: " + node
@ -313,25 +248,15 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
/**
* @return true if there pending messages to dispatch
*/
public boolean hasNext() {
boolean result;
lock.lock();
try {
result = iter.hasNext();
} finally {
lock.unlock();
}
return result;
public synchronized boolean hasNext() {
return iter.hasNext();
}
/**
* @return the next pending message
*/
public MessageReference next() {
Message message;
lock.lock();
try {
message = (Message)iter.next();
public synchronized MessageReference next() {
Message message = (Message)iter.next();
last = message;
if (!isDiskListEmpty()) {
// got from disk
@ -339,86 +264,53 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
message.setMemoryUsage(this.getSystemUsage().getMemoryUsage());
message.incrementReferenceCount();
}
} finally {
lock.unlock();
}
return message;
}
/**
* remove the message at the cursor position
*/
public void remove() {
lock.lock();
try {
public synchronized void remove() {
iter.remove();
if (last != null) {
last.decrementReferenceCount();
}
} finally {
lock.unlock();
}
}
/**
* @param node
* @see org.apache.activemq.broker.region.cursors.AbstractPendingMessageCursor#remove(org.apache.activemq.broker.region.MessageReference)
*/
public void remove(MessageReference node) {
lock.lock();
try {
public synchronized void remove(MessageReference node) {
if (memoryList.remove(node)) {
node.decrementReferenceCount();
}
if (!isDiskListEmpty()) {
getDiskList().remove(node);
}
} finally {
lock.unlock();
}
}
/**
* @return the number of pending messages
*/
public int size() {
int result;
lock.lock();
try {
result = memoryList.size() + (isDiskListEmpty() ? 0 : getDiskList().size());
} finally {
lock.unlock();
}
return result;
public synchronized int size() {
return memoryList.size() + (isDiskListEmpty() ? 0 : getDiskList().size());
}
/**
* clear all pending messages
*/
public void clear() {
lock.lock();
try {
public synchronized void clear() {
memoryList.clear();
if (!isDiskListEmpty()) {
getDiskList().clear();
}
last=null;
} finally {
lock.unlock();
}
}
public boolean isFull() {
boolean result;
lock.lock();
try {
public synchronized boolean isFull() {
// we always have space - as we can persist to disk
// TODO: not necessarily true.
result = false;
} finally {
lock.unlock();
}
return result;
return false;
}
public boolean hasMessagesBufferedToDeliver() {
@ -432,8 +324,7 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
public void onUsageChanged(Usage usage, int oldPercentUsage,
int newPercentUsage) {
if (newPercentUsage >= getMemoryUsageHighWaterMark()) {
lock.lock();
try {
synchronized (this) {
flushRequired = true;
if (!iterating) {
expireOldMessages();
@ -442,8 +333,6 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
flushRequired = false;
}
}
} finally {
lock.unlock();
}
}
}
@ -456,9 +345,7 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
return hasSpace() && isDiskListEmpty();
}
protected void expireOldMessages() {
lock.lock();
try {
protected synchronized void expireOldMessages() {
if (!memoryList.isEmpty()) {
LinkedList<MessageReference> tmpList = new LinkedList<MessageReference>(this.memoryList);
this.memoryList = new LinkedList<MessageReference>();
@ -471,14 +358,11 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
}
}
}
} finally {
lock.unlock();
}
}
protected void flushToDisk() {
lock.lock();
try {
protected synchronized void flushToDisk() {
if (!memoryList.isEmpty()) {
while (!memoryList.isEmpty()) {
MessageReference node = memoryList.removeFirst();
@ -487,9 +371,6 @@ public class FilePendingMessageCursor extends AbstractPendingMessageCursor imple
}
memoryList.clear();
}
} finally {
lock.unlock();
}
}
protected boolean isDiskListEmpty() {

View File

@ -16,10 +16,12 @@
*/
package org.apache.activemq.broker.region.cursors;
import org.apache.activemq.ActiveMQMessageAudit;
import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.region.MessageReference;
import org.apache.activemq.broker.region.Queue;
import org.apache.activemq.command.Message;
import org.apache.activemq.kaha.Store;
import org.apache.activemq.usage.SystemUsage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -87,7 +89,7 @@ public class StoreQueueCursor extends AbstractPendingMessageCursor {
pendingCount = 0;
}
public void addMessageLast(MessageReference node) throws Exception {
public synchronized void addMessageLast(MessageReference node) throws Exception {
if (node != null) {
Message msg = node.getMessage();
if (started) {
@ -102,7 +104,7 @@ public class StoreQueueCursor extends AbstractPendingMessageCursor {
}
}
public void addMessageFirst(MessageReference node) throws Exception {
public synchronized void addMessageFirst(MessageReference node) throws Exception {
if (node != null) {
Message msg = node.getMessage();
if (started) {
@ -141,11 +143,6 @@ public class StoreQueueCursor extends AbstractPendingMessageCursor {
return result;
}
public synchronized void release() {
nonPersistent.release();
persistent.release();
}
public synchronized void remove() {
if (currentCursor != null) {
currentCursor.remove();
@ -162,7 +159,7 @@ public class StoreQueueCursor extends AbstractPendingMessageCursor {
pendingCount--;
}
public void reset() {
public synchronized void reset() {
nonPersistent.reset();
persistent.reset();
}