Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AMQ-9455] DestinationPolicy support for MessageInterceptorStrategy #1182

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
import org.apache.activemq.broker.ConnectionContext;
import org.apache.activemq.broker.ProducerBrokerExchange;
import org.apache.activemq.broker.region.policy.DeadLetterStrategy;
import org.apache.activemq.broker.region.policy.MessageInterceptorStrategy;
import org.apache.activemq.broker.region.policy.SlowConsumerStrategy;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.command.Message;
import org.apache.activemq.command.MessageAck;
import org.apache.activemq.command.MessageDispatchNotification;
import org.apache.activemq.command.ProducerInfo;
import org.apache.activemq.filter.NonCachedMessageEvaluationContext;
import org.apache.activemq.security.SecurityContext;
import org.apache.activemq.state.ProducerState;
import org.apache.activemq.store.MessageStore;
Expand Down Expand Up @@ -99,6 +99,7 @@ public abstract class BaseDestination implements Destination {
private int maxExpirePageSize = MAX_BROWSE_PAGE_SIZE;
protected int cursorMemoryHighWaterMark = 70;
protected int storeUsageHighWaterMark = 100;
private MessageInterceptorStrategy messageInterceptorStrategy;
private SlowConsumerStrategy slowConsumerStrategy;
private boolean prioritizedMessages;
private long inactiveTimeoutBeforeGC = DEFAULT_INACTIVE_TIMEOUT_BEFORE_GC;
Expand Down Expand Up @@ -942,4 +943,12 @@ public boolean isPersistJMSRedelivered() {
public SystemUsage getSystemUsage() {
return systemUsage;
}

public MessageInterceptorStrategy getMessageInterceptorStrategy() {
return this.messageInterceptorStrategy;
}

public void setMessageInterceptorStrategy(MessageInterceptorStrategy messageInterceptorStrategy) {
this.messageInterceptorStrategy = messageInterceptorStrategy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

import jakarta.jms.InvalidSelectorException;
import jakarta.jms.JMSException;
import jakarta.jms.MessageFormatException;
import jakarta.jms.MessageFormatRuntimeException;
import jakarta.jms.ResourceAllocationException;

import org.apache.activemq.broker.BrokerService;
Expand Down Expand Up @@ -625,6 +627,15 @@ public void send(final ProducerBrokerExchange producerExchange, final Message me
final ProducerInfo producerInfo = producerExchange.getProducerState().getInfo();
final boolean sendProducerAck = !message.isResponseRequired() && producerInfo.getWindowSize() > 0
&& !context.isInRecoveryMode();

if(getMessageInterceptorStrategy() != null) {
try {
getMessageInterceptorStrategy().process(producerExchange, message);
} catch (MessageFormatRuntimeException e) {
throw new MessageFormatException(e.getMessage(), e.getErrorCode());
}
}

if (message.isExpired()) {
// message not stored - or added to stats yet - so chuck here
broker.getRoot().messageExpired(context, message, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import org.slf4j.LoggerFactory;

import jakarta.jms.JMSException;
import jakarta.jms.MessageFormatException;
import jakarta.jms.MessageFormatRuntimeException;

import static org.apache.activemq.transaction.Transaction.IN_USE_STATE;

Expand Down Expand Up @@ -371,6 +373,14 @@ public void send(final ProducerBrokerExchange producerExchange, final Message me

message.setRegionDestination(this);

if(getMessageInterceptorStrategy() != null) {
try {
getMessageInterceptorStrategy().process(producerExchange, message);
} catch (MessageFormatRuntimeException e) {
throw new MessageFormatException(e.getMessage(), e.getErrorCode());
}
}

// There is delay between the client sending it and it arriving at the
// destination.. it may have expired.
if (message.isExpired()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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.policy;

import java.util.Arrays;

import org.apache.activemq.broker.ProducerBrokerExchange;
import org.apache.activemq.command.Message;

import jakarta.jms.MessageFormatRuntimeException;

/**
* Configurable chain of MessageInterceptorStrategies
*
* @org.apache.xbean.XBean
*/
public class ChainMessageInterceptorStrategy implements MessageInterceptorStrategy {

private MessageInterceptorStrategy[] messageInterceptorStrategies;

@Override
public void process(ProducerBrokerExchange producerBrokerExchange, Message message) throws MessageFormatRuntimeException {
if(messageInterceptorStrategies == null || messageInterceptorStrategies.length == 0) {
return;
}

Arrays.stream(messageInterceptorStrategies).forEach(m -> m.process(producerBrokerExchange, message));
}

public void setMessageStrategies(MessageInterceptorStrategy[] messageInterceptorStrategies) {
this.messageInterceptorStrategies = messageInterceptorStrategies;
}

public MessageInterceptorStrategy[] getMessageInterceptorStrategies() {
return this.messageInterceptorStrategies;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
* 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.policy;

import org.apache.activemq.broker.ProducerBrokerExchange;
import org.apache.activemq.command.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.jms.MessageFormatRuntimeException;

/**
* Enforce message policies for JMS Header values
*
* @org.apache.xbean.XBean
*/
public class HeaderMessageInterceptorStrategy implements MessageInterceptorStrategy {

private static final Logger LOG = LoggerFactory.getLogger(HeaderMessageInterceptorStrategy.class);

boolean forceDeliveryMode = false;

boolean persistent = true;

boolean forceExpiration = false;

/**
* variable which (when non-zero) is used to override
* the expiration date for messages that arrive with
* no expiration date set (in Milliseconds).
*/
long zeroExpirationOverride = 0;

/**
* variable which (when non-zero) is used to limit
* the expiration date (in Milliseconds).
*/
long expirationCeiling = 0;

/**
* If true, the plugin will not update timestamp to past values
* False by default
*/
boolean futureOnly = false;

/**
* if true, update timestamp even if message has passed through a network
* default false
*/
boolean processNetworkMessages = false;

/**
* setter method for zeroExpirationOverride
*/
public void setZeroExpirationOverride(long ttl)
{
this.zeroExpirationOverride = ttl;
}

/**
* setter method for expirationCeiling
*/
public void setExpirationCeiling(long expirationCeiling)
{
this.expirationCeiling = expirationCeiling;
}

public void setFutureOnly(boolean futureOnly) {
this.futureOnly = futureOnly;
}

public void setProcessNetworkMessages(Boolean processNetworkMessages) {
this.processNetworkMessages = processNetworkMessages;
}

@Override
public void process(final ProducerBrokerExchange producerBrokerExchange, final Message message) throws MessageFormatRuntimeException {
if(!isProcessNetworkMessages() && producerBrokerExchange.getConnectionContext().isNetworkConnection()) {
// Message passed through a network and processNetworkMessages=true is not set
return;
}

if(isForceExpiration()) {
if (message.getTimestamp() > 0 && !message.getDestination().isDLQ()) {
long oldExpiration = message.getExpiration();
long newTimeStamp = System.currentTimeMillis();
long timeToLive = zeroExpirationOverride;
long oldTimestamp = message.getTimestamp();
if (oldExpiration > 0) {
timeToLive = oldExpiration - oldTimestamp;
}
if (timeToLive > 0 && expirationCeiling > 0 && timeToLive > expirationCeiling) {
timeToLive = expirationCeiling;
}
long expiration = timeToLive + newTimeStamp;
// In the scenario that the Broker is behind the clients we never want to set the
// Timestamp and Expiration in the past
if(!futureOnly || (expiration > oldExpiration)) {
if (timeToLive > 0 && expiration > 0) {
message.setExpiration(expiration);
}
message.setTimestamp(newTimeStamp);
LOG.debug("Set message {} timestamp from {} to {}", message.getMessageId(), oldTimestamp, newTimeStamp);
}
}
}

if(forceDeliveryMode) {
message.setPersistent(isPersistent());
}
}

public void setForceDeliveryMode(boolean forceDeliveryMode) {
this.forceDeliveryMode = forceDeliveryMode;
}

public boolean isForceDeliveryMode() {
return this.forceDeliveryMode;
}

public void setForceExpiration(boolean forceExpiration) {
this.forceExpiration = forceExpiration;
}

public boolean isForceExpiration() {
return this.forceExpiration;
}

public void setPersistent(boolean persistent) {
this.persistent = persistent;
}

public boolean isPersistent() {
return this.persistent;
}

public void setProcessNetworkMessages(boolean processNetworkMessages) {
this.processNetworkMessages = processNetworkMessages;
}

public boolean isProcessNetworkMessages() {
return this.processNetworkMessages;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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.policy;

import org.apache.activemq.broker.ProducerBrokerExchange;
import org.apache.activemq.command.Message;
import jakarta.jms.MessageFormatRuntimeException;

public interface MessageInterceptorStrategy {

/**
* When a PolicyEntry is configured with a MessageInterceptorStrategy, the
* process method is invoked with the current ProducerBrokerExchange and Message before
* the message is stored in any destination cache or persistence store.
*
* Implementations may reference data from the ProducerBrokerExchange and may check or
* modify headers, properties, body or other metadata on the Message.
*
* Any change to the message must adhere to OpenWire and ActiveMQ requirements or risk
* issues with memory usage, compatibility, and general correct functioning.
*
* Implementations shall not copy, or clone the message.
*
* Implementations may throw a <tt>MessageFormatRuntimeException</tt>
* that is returned to the client to indicate a message should not be added to the queue.
*
* @param producerBrokerExchange
* @param message
* @return
* @throws MessageFormatRuntimeException
*/
void process(final ProducerBrokerExchange producerBrokerExchange, final Message message) throws MessageFormatRuntimeException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public class PolicyEntry extends DestinationMapEntry {
private int sendFailIfNoSpace = -1;
private long sendFailIfNoSpaceAfterTimeout = -1;

private MessageInterceptorStrategy messageInterceptorStrategy = null;

public void configure(Broker broker,Queue queue) {
baseConfiguration(broker,queue);
Expand All @@ -139,6 +140,7 @@ public void configure(Broker broker,Queue queue) {
queue.setConsumersBeforeDispatchStarts(getConsumersBeforeDispatchStarts());
queue.setAllConsumersExclusiveByDefault(isAllConsumersExclusiveByDefault());
queue.setPersistJMSRedelivered(isPersistJMSRedelivered());
queue.setMessageInterceptorStrategy(getMessageInterceptorStrategy());
}

public void update(Queue queue) {
Expand Down Expand Up @@ -201,6 +203,7 @@ public void configure(Broker broker,Topic topic) {
topic.getMemoryUsage().setLimit(memoryLimit);
}
topic.setLazyDispatch(isLazyDispatch());
topic.setMessageInterceptorStrategy(getMessageInterceptorStrategy());
}

public void update(Topic topic) {
Expand Down Expand Up @@ -1165,4 +1168,12 @@ public boolean isUseTopicSubscriptionInflightStats() {
public void setUseTopicSubscriptionInflightStats(boolean useTopicSubscriptionInflightStats) {
this.useTopicSubscriptionInflightStats = useTopicSubscriptionInflightStats;
}

public void setMessageInterceptorStrategy(MessageInterceptorStrategy messageInterceptorStrategy) {
this.messageInterceptorStrategy = messageInterceptorStrategy;
}

public MessageInterceptorStrategy getMessageInterceptorStrategy() {
return this.messageInterceptorStrategy;
}
}