/**
 * 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.util;

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

/**
 * A Broker interceptor which updates a JMS Client's timestamp on the message
 * with a broker timestamp. Useful when the clocks on client machines are known
 * to not be correct and you can only trust the time set on the broker machines.
 * 
 * Enabling this plugin will break JMS compliance since the timestamp that the
 * producer sees on the messages after as send() will be different from the
 * timestamp the consumer will observe when he receives the message. This plugin
 * is not enabled in the default ActiveMQ configuration.
 * 
 * @org.apache.xbean.XBean element="timeStampingBrokerPlugin"
 * 
 * @version $Revision$
 */
public class TimeStampingBrokerPlugin extends BrokerPluginSupport {
    public void send(ProducerBrokerExchange producerExchange, Message message) throws Exception {
        if (message.getTimestamp() > 0
            && (message.getBrokerPath() == null || message.getBrokerPath().length == 0)) {
            // timestamp not been disabled and has not passed through a network
            long oldExpiration = message.getExpiration();
            long newTimeStamp = System.currentTimeMillis();
            if (oldExpiration > 0) {
                long oldTimestamp = message.getTimestamp();
                long timeToLive = oldExpiration-oldTimestamp;
                long expiration = timeToLive+newTimeStamp;
                message.setExpiration(expiration);
            }
            message.setTimestamp(newTimeStamp);
        }
        super.send(producerExchange, message);
    }
}

@@ -995,16 +995,101 @@
 Message;
+%0Aimport org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;
 %0A%0A/**%0A *
@@ -1633,165 +1633,1326 @@
 %0A * 
-@org.apache.xbean.XBean element=%22timeStampingBrokerPlugin%22%0A * %0A * @version $Revision$%0A */%0Apublic class TimeStampingBrokerPlugin extends BrokerPluginSupport %7B
+2 new attributes have been added which will allow the administrator some override control%0A * over the expiration time for incoming messages:%0A *%0A * Attribute 'zeroExpirationOverride' can be used to apply an expiration%0A * time to incoming messages with no expiration defined (messages that would never expire)%0A *%0A * Attribute 'ttlCeiling' can be used to apply a limit to the expiration time%0A *%0A * @org.apache.xbean.XBean element=%22timeStampingBrokerPlugin%22%0A * %0A * @version $Revision$%0A */%0Apublic class TimeStampingBrokerPlugin extends BrokerPluginSupport %7B%0A%0A    private static final Log LOG = LogFactory.getLog(TimeStampingBrokerPlugin.class);%0A%0A    /** %0A    * variable which (when non-zero) is used to override%0A    * the expiration date for messages that arrive with%0A    * no expiration date set (in Milliseconds).%0A    */%0A    long zeroExpirationOverride = 0;%0A%0A    /** %0A    * variable which (when non-zero) is used to limit%0A    * the expiration date (in Milliseconds).  %0A    */%0A    long ttlCeiling = 0;%0A%0A    /** %0A    * setter method for zeroExpirationOverride%0A    */%0A    public void setZeroExpirationOverride(long ttl)%0A    %7B%0A        this.zeroExpirationOverride = ttl;%0A    %7D%0A%0A    /** %0A    * setter method for ttlCeiling%0A    */%0A    public void setTtlCeiling(long ttlCeiling)%0A    %7B%0A        this.ttlCeiling = ttlCeiling;%0A    %7D%0A
 %0A   
@@ -3370,24 +3370,126 @@
 meMillis();%0A
+            message.setTimestamp(newTimeStamp);%0A            long timeToLive = zeroExpirationOverride;%0A
             
@@ -3581,37 +3581,32 @@
                 
-long 
 timeToLive = old
@@ -3615,17 +3615,19 @@
 piration
--
+ - 
 oldTimes
@@ -3628,32 +3628,176 @@
 dTimestamp;%0A    
+        %7D%0A            if (timeToLive %3E 0 && ttlCeiling %3E 0 && timeToLive %3E ttlCeiling) %7B%0A                timeToLive = ttlCeiling;%0A            %7D%0A
             long
@@ -3824,9 +3824,11 @@
 Live
-+
+ + 
 newT
@@ -3833,24 +3833,76 @@
 wTimeStamp;%0A
+            if (timeToLive %3E 0 && expiration %3E 0) %7B%0A
             
@@ -3958,56 +3958,8 @@
   %7D%0A
-            message.setTimestamp(newTimeStamp);%0A
     

fix for https://issues.apache.org/activemq/browse/AMQ-2221 - improvments for timestamp plugin