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.cursors; 018 019import java.util.Map; 020 021import org.apache.activemq.broker.region.MessageReference; 022import org.apache.activemq.command.MessageId; 023import org.apache.activemq.management.SizeStatisticImpl; 024 025/** 026 * 027 * 028 */ 029public class PendingMessageHelper { 030 031 private final Map<MessageId, PendingNode> map; 032 private final SizeStatisticImpl messageSize; 033 034 public PendingMessageHelper(Map<MessageId, PendingNode> map, 035 SizeStatisticImpl messageSize) { 036 super(); 037 this.map = map; 038 this.messageSize = messageSize; 039 } 040 041 public void addToMap(MessageReference message, PendingNode node) { 042 PendingNode previous = this.map.put(message.getMessageId(), node); 043 if (previous != null) { 044 try { 045 messageSize.addSize(-previous.getMessage().getSize()); 046 } catch (Exception e) { 047 //expected for NullMessageReference 048 } 049 } 050 try { 051 messageSize.addSize(message.getSize()); 052 } catch (Exception e) { 053 //expected for NullMessageReference 054 } 055 } 056 057 public PendingNode removeFromMap(MessageReference message) { 058 PendingNode removed = this.map.remove(message.getMessageId()); 059 if (removed != null) { 060 try { 061 messageSize.addSize(-removed.getMessage().getSize()); 062 } catch (Exception e) { 063 //expected for NullMessageReference 064 } 065 } 066 return removed; 067 } 068}