001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.broker.region.policy;
018
019import org.apache.activemq.ActiveMQMessageAudit;
020import org.apache.activemq.command.Message;
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023
024/**
025 * A strategy for choosing which destination is used for dead letter queue
026 * messages.
027 *
028 */
029public abstract class AbstractDeadLetterStrategy implements DeadLetterStrategy {
030    private static final Logger LOG = LoggerFactory.getLogger(AbstractDeadLetterStrategy.class);
031    private boolean processNonPersistent = false;
032    private boolean processExpired = true;
033    private boolean enableAudit = true;
034    private final ActiveMQMessageAudit messageAudit = new ActiveMQMessageAudit();
035    private long expiration;
036
037    @Override
038    public void rollback(Message message) {
039        if (message != null && this.enableAudit) {
040            messageAudit.rollback(message);
041        }
042    }
043
044    @Override
045    public boolean isSendToDeadLetterQueue(Message message) {
046        boolean result = false;
047        if (message != null) {
048            result = true;
049            if (enableAudit && messageAudit.isDuplicate(message)) {
050                result = false;
051                LOG.debug("Not adding duplicate to DLQ: {}, dest: {}", message.getMessageId(), message.getDestination());
052            }
053            if (!message.isPersistent() && !processNonPersistent) {
054                result = false;
055            }
056            if (message.isExpired() && !processExpired) {
057                result = false;
058            }
059        }
060        return result;
061    }
062
063    /**
064     * @return the processExpired
065     */
066    @Override
067    public boolean isProcessExpired() {
068        return this.processExpired;
069    }
070
071    /**
072     * @param processExpired the processExpired to set
073     */
074    @Override
075    public void setProcessExpired(boolean processExpired) {
076        this.processExpired = processExpired;
077    }
078
079    /**
080     * @return the processNonPersistent
081     */
082    @Override
083    public boolean isProcessNonPersistent() {
084        return this.processNonPersistent;
085    }
086
087    /**
088     * @param processNonPersistent the processNonPersistent to set
089     */
090    @Override
091    public void setProcessNonPersistent(boolean processNonPersistent) {
092        this.processNonPersistent = processNonPersistent;
093    }
094
095    public boolean isEnableAudit() {
096        return enableAudit;
097    }
098
099    public void setEnableAudit(boolean enableAudit) {
100        this.enableAudit = enableAudit;
101    }
102
103    public long getExpiration() {
104        return expiration;
105    }
106
107    public void setExpiration(long expiration) {
108        this.expiration = expiration;
109    }
110
111    public int getMaxProducersToAudit() {
112        return messageAudit.getMaximumNumberOfProducersToTrack();
113    }
114
115    public void setMaxProducersToAudit(int maxProducersToAudit) {
116        messageAudit.setMaximumNumberOfProducersToTrack(maxProducersToAudit);
117    }
118
119    public void setMaxAuditDepth(int maxAuditDepth) {
120        messageAudit.setAuditDepth(maxAuditDepth);
121    }
122
123    public int getMaxAuditDepth() {
124        return messageAudit.getAuditDepth();
125    }
126
127}