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.command; 018 019/** 020 * @openwire:marshaller code="110" 021 * 022 */ 023public class MessageId implements DataStructure, Comparable<MessageId> { 024 025 public static final byte DATA_STRUCTURE_TYPE = CommandTypes.MESSAGE_ID; 026 027 protected ProducerId producerId; 028 protected long producerSequenceId; 029 protected long brokerSequenceId; 030 031 private transient String key; 032 private transient int hashCode; 033 034 public MessageId() { 035 this.producerId = new ProducerId(); 036 } 037 038 public MessageId(ProducerInfo producerInfo, long producerSequenceId) { 039 this.producerId = producerInfo.getProducerId(); 040 this.producerSequenceId = producerSequenceId; 041 } 042 043 public MessageId(String messageKey) { 044 setValue(messageKey); 045 } 046 047 public MessageId(String producerId, long producerSequenceId) { 048 this(new ProducerId(producerId), producerSequenceId); 049 } 050 051 public MessageId(ProducerId producerId, long producerSequenceId) { 052 this.producerId = producerId; 053 this.producerSequenceId = producerSequenceId; 054 } 055 056 /** 057 * Sets the value as a String 058 */ 059 public void setValue(String messageKey) { 060 key = messageKey; 061 // Parse off the sequenceId 062 int p = messageKey.lastIndexOf(":"); 063 if (p >= 0) { 064 producerSequenceId = Long.parseLong(messageKey.substring(p + 1)); 065 messageKey = messageKey.substring(0, p); 066 } 067 producerId = new ProducerId(messageKey); 068 } 069 070 /** 071 * Sets the transient text view of the message which will be ignored if the 072 * message is marshaled on a transport; so is only for in-JVM changes to 073 * accommodate foreign JMS message IDs 074 */ 075 public void setTextView(String key) { 076 this.key = key; 077 } 078 079 public byte getDataStructureType() { 080 return DATA_STRUCTURE_TYPE; 081 } 082 083 public boolean equals(Object o) { 084 if (this == o) { 085 return true; 086 } 087 if (o == null || o.getClass() != getClass()) { 088 return false; 089 } 090 091 MessageId id = (MessageId)o; 092 return producerSequenceId == id.producerSequenceId && producerId.equals(id.producerId); 093 } 094 095 public int hashCode() { 096 if (hashCode == 0) { 097 hashCode = producerId.hashCode() ^ (int)producerSequenceId; 098 } 099 return hashCode; 100 } 101 102 public String toString() { 103 if (key == null) { 104 key = producerId.toString() + ":" + producerSequenceId; 105 } 106 return key; 107 } 108 109 /** 110 * @openwire:property version=1 cache=true 111 */ 112 public ProducerId getProducerId() { 113 return producerId; 114 } 115 116 public void setProducerId(ProducerId producerId) { 117 this.producerId = producerId; 118 } 119 120 /** 121 * @openwire:property version=1 122 */ 123 public long getProducerSequenceId() { 124 return producerSequenceId; 125 } 126 127 public void setProducerSequenceId(long producerSequenceId) { 128 this.producerSequenceId = producerSequenceId; 129 } 130 131 /** 132 * @openwire:property version=1 133 */ 134 public long getBrokerSequenceId() { 135 return brokerSequenceId; 136 } 137 138 public void setBrokerSequenceId(long brokerSequenceId) { 139 this.brokerSequenceId = brokerSequenceId; 140 } 141 142 public boolean isMarshallAware() { 143 return false; 144 } 145 146 public MessageId copy() { 147 MessageId copy = new MessageId(producerId, producerSequenceId); 148 copy.key = key; 149 copy.brokerSequenceId = brokerSequenceId; 150 return copy; 151 } 152 153 /** 154 * @param o 155 * @return 156 * @see java.lang.Comparable#compareTo(java.lang.Object) 157 */ 158 public int compareTo(MessageId other) { 159 int result = -1; 160 if (other != null) { 161 result = this.toString().compareTo(other.toString()); 162 } 163 return result; 164 } 165}