use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.
the class ServerConsumerImpl method removeReferenceByID.
@Override
public synchronized MessageReference removeReferenceByID(final long messageID) throws Exception {
if (browseOnly) {
return null;
}
synchronized (lock) {
// But first we need to make sure deliveringRefs isn't empty
if (deliveringRefs.isEmpty()) {
return null;
}
if (deliveringRefs.peek().getMessage().getMessageID() == messageID) {
return deliveringRefs.poll();
}
Iterator<MessageReference> iter = deliveringRefs.iterator();
MessageReference ref = null;
while (iter.hasNext()) {
MessageReference theRef = iter.next();
if (theRef.getMessage().getMessageID() == messageID) {
iter.remove();
ref = theRef;
break;
}
}
return ref;
}
}
use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.
the class ServerConsumerImpl method cancelRefs.
@Override
public LinkedList<MessageReference> cancelRefs(final boolean failed, final boolean lastConsumedAsDelivered, final Transaction tx) throws Exception {
boolean performACK = lastConsumedAsDelivered;
try {
LargeMessageDeliverer pendingLargeMessageDeliverer = largeMessageDeliverer;
if (pendingLargeMessageDeliverer != null) {
pendingLargeMessageDeliverer.finish();
}
} catch (Throwable e) {
ActiveMQServerLogger.LOGGER.errorResttingLargeMessage(e, largeMessageDeliverer);
} finally {
largeMessageDeliverer = null;
}
LinkedList<MessageReference> refs = new LinkedList<>();
synchronized (lock) {
if (!deliveringRefs.isEmpty()) {
for (MessageReference ref : deliveringRefs) {
if (performACK) {
ref.acknowledge(tx);
performACK = false;
} else {
refs.add(ref);
updateDeliveryCountForCanceledRef(ref, failed);
}
if (logger.isTraceEnabled()) {
logger.trace("ServerConsumerImpl::" + this + " Preparing Cancelling list for messageID = " + ref.getMessage().getMessageID() + ", ref = " + ref);
}
}
deliveringRefs.clear();
}
}
return refs;
}
use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.
the class ServerConsumerImpl method close.
@Override
public void close(final boolean failed) throws Exception {
if (logger.isTraceEnabled()) {
logger.trace("ServerConsumerImpl::" + this + " being closed with failed=" + failed, new Exception("trace"));
}
if (server.hasBrokerPlugins()) {
server.callBrokerPlugins(plugin -> plugin.beforeCloseConsumer(this, failed));
}
setStarted(false);
LargeMessageDeliverer del = largeMessageDeliverer;
if (del != null) {
del.finish();
}
removeItself();
LinkedList<MessageReference> refs = cancelRefs(failed, false, null);
Iterator<MessageReference> iter = refs.iterator();
Transaction tx = new TransactionImpl(storageManager);
while (iter.hasNext()) {
MessageReference ref = iter.next();
if (logger.isTraceEnabled()) {
logger.trace("ServerConsumerImpl::" + this + " cancelling reference " + ref);
}
ref.getQueue().cancel(tx, ref, true);
}
tx.rollback();
messageQueue.recheckRefCount(session.getSessionContext());
if (!browseOnly) {
TypedProperties props = new TypedProperties();
props.putSimpleStringProperty(ManagementHelper.HDR_ADDRESS, binding.getAddress());
props.putSimpleStringProperty(ManagementHelper.HDR_CLUSTER_NAME, binding.getClusterName());
props.putSimpleStringProperty(ManagementHelper.HDR_ROUTING_NAME, binding.getRoutingName());
props.putSimpleStringProperty(ManagementHelper.HDR_FILTERSTRING, filter == null ? null : filter.getFilterString());
props.putIntProperty(ManagementHelper.HDR_DISTANCE, binding.getDistance());
props.putIntProperty(ManagementHelper.HDR_CONSUMER_COUNT, messageQueue.getConsumerCount());
// HORNETQ-946
props.putSimpleStringProperty(ManagementHelper.HDR_USER, SimpleString.toSimpleString(session.getUsername()));
props.putSimpleStringProperty(ManagementHelper.HDR_REMOTE_ADDRESS, SimpleString.toSimpleString(((ServerSessionImpl) session).getRemotingConnection().getRemoteAddress()));
props.putSimpleStringProperty(ManagementHelper.HDR_SESSION_NAME, SimpleString.toSimpleString(session.getName()));
Notification notification = new Notification(null, CoreNotificationType.CONSUMER_CLOSED, props);
managementService.sendNotification(notification);
}
if (server.hasBrokerPlugins()) {
server.callBrokerPlugins(plugin -> plugin.afterCloseConsumer(this, failed));
}
}
use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.
the class ServerConsumerImpl method acknowledge.
@Override
public synchronized void acknowledge(Transaction tx, final long messageID) throws Exception {
if (browseOnly) {
return;
}
// Acknowledge acknowledges all refs delivered by the consumer up to and including the one explicitly
// acknowledged
// We use a transaction here as if the message is not found, we should rollback anything done
// This could eventually happen on retries during transactions, and we need to make sure we don't ACK things we are not supposed to acknowledge
boolean startedTransaction = false;
if (tx == null) {
startedTransaction = true;
tx = new TransactionImpl(storageManager);
}
try {
MessageReference ref;
do {
synchronized (lock) {
ref = deliveringRefs.poll();
}
if (logger.isTraceEnabled()) {
logger.trace("ACKing ref " + ref + " on tx= " + tx + ", consumer=" + this);
}
if (ref == null) {
ActiveMQIllegalStateException ils = ActiveMQMessageBundle.BUNDLE.consumerNoReference(id, messageID, messageQueue.getName());
tx.markAsRollbackOnly(ils);
throw ils;
}
ref.acknowledge(tx);
acks++;
} while (ref.getMessageID() != messageID);
if (startedTransaction) {
tx.commit();
}
} catch (ActiveMQException e) {
if (startedTransaction) {
tx.rollback();
} else {
tx.markAsRollbackOnly(e);
}
throw e;
} catch (Throwable e) {
ActiveMQServerLogger.LOGGER.errorAckingMessage((Exception) e);
ActiveMQException activeMQIllegalStateException = new ActiveMQIllegalStateException(e.getMessage());
if (startedTransaction) {
tx.rollback();
} else {
tx.markAsRollbackOnly(activeMQIllegalStateException);
}
throw activeMQIllegalStateException;
}
}
use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.
the class ServerConsumerImpl method reject.
@Override
public synchronized void reject(final long messageID) throws Exception {
if (browseOnly) {
return;
}
MessageReference ref = removeReferenceByID(messageID);
if (ref == null) {
// nothing to be done
return;
}
ref.getQueue().sendToDeadLetterAddress(null, ref);
}
Aggregations