Java Code Examples for org.apache.activemq.artemis.api.core.SimpleString#startsWith()

The following examples show how to use org.apache.activemq.artemis.api.core.SimpleString#startsWith() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TopicClusterTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private void checkInternalProperty(Message... msgs) throws Exception {
   boolean checked = false;
   for (Message m : msgs) {
      ActiveMQMessage hqMessage = (ActiveMQMessage) m;
      ClientMessage coreMessage = hqMessage.getCoreMessage();
      Set<SimpleString> coreProps = coreMessage.getPropertyNames();
      boolean exist = false;
      for (SimpleString prop : coreProps) {
         if (prop.startsWith(org.apache.activemq.artemis.api.core.Message.HDR_ROUTE_TO_IDS)) {
            exist = true;
            break;
         }
      }

      if (exist) {
         Enumeration enumProps = m.getPropertyNames();
         while (enumProps.hasMoreElements()) {
            String propName = (String) enumProps.nextElement();
            assertFalse("Shouldn't be in jms property: " + propName, propName.startsWith(org.apache.activemq.artemis.api.core.Message.HDR_ROUTE_TO_IDS.toString()));
         }
         checked = true;
      }
   }
   assertTrue(checked);
}
 
Example 2
Source File: ActiveMQCompatibleMessage.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
static Destination findCompatibleReplyTo(ClientMessage message) {
   SimpleString address = MessageUtil.getJMSReplyTo(message);
   if (address != null) {
      final SimpleString checkedAddress = checkPrefix1X(address);
      if (checkedAddress != null) {
         return ActiveMQDestination.fromPrefixed1XName(address.toString(), checkedAddress.toString());
      }
      String name = address.toString();
      // swap the old prefixes for the new ones so the proper destination type gets created
      if (address.startsWith(OLD_QUEUE_QUALIFIED_PREFIX)) {
         name = address.subSeq(OLD_QUEUE_QUALIFIED_PREFIX.length(), address.length()).toString();
      } else if (address.startsWith(OLD_TEMP_QUEUE_QUALIFED_PREFIX)) {
         name = address.subSeq(OLD_TEMP_QUEUE_QUALIFED_PREFIX.length(), address.length()).toString();
      } else if (address.startsWith(OLD_TOPIC_QUALIFIED_PREFIX)) {
         name = address.subSeq(OLD_TOPIC_QUALIFIED_PREFIX.length(), address.length()).toString();
      } else if (address.startsWith(OLD_TEMP_TOPIC_QUALIFED_PREFIX)) {
         name = address.subSeq(OLD_TEMP_TOPIC_QUALIFED_PREFIX.length(), address.length()).toString();
      }
      return ActiveMQDestination.fromPrefixedName(address.toString(), name);
   }

   return null;
}
 
Example 3
Source File: MessageUtil.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public static Set<String> getPropertyNames(Message message) {
   HashSet<String> set = new HashSet<>();

   for (SimpleString propName : message.getPropertyNames()) {
      if (propName.equals(Message.HDR_GROUP_ID)) {
         set.add(MessageUtil.JMSXGROUPID);
      } else if (propName.equals(Message.HDR_GROUP_SEQUENCE)) {
         set.add(MessageUtil.JMSXGROUPSEQ);
      } else if (propName.equals(Message.HDR_VALIDATED_USER)) {
         set.add(MessageUtil.JMSXUSERID);
      } else if ((!propName.startsWith(JMS) || propName.startsWith(JMSX) || propName.startsWith(JMS_)) && !propName.startsWith(CONNECTION_ID_PROPERTY_NAME) && !propName.equals(Message.HDR_ROUTING_TYPE) && !propName.startsWith(Message.HDR_ROUTE_TO_IDS)) {
         set.add(propName.toString());
      }
   }

   set.add(JMSXDELIVERYCOUNT);

   return set;
}
 
Example 4
Source File: FilterImpl.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public Object getProperty(SimpleString id) {
   Object result = null;
   if (id.startsWith(FilterConstants.ACTIVEMQ_PREFIX)) {
      result = getHeaderFieldValue(message, id);
   }
   if (id.startsWith(FederatedAddress.HDR_HOPS)) {
      byte[] bytes = message.getExtraBytesProperty(FederatedAddress.HDR_HOPS);
      result = bytes == null ? null : ByteUtil.bytesToInt(bytes);
   }
   if (result == null) {
      result = message.getObjectPropertyForFilter(id);
   }
   if (result != null) {
      if (result.getClass() == SimpleString.class) {
         result = result.toString();
      }
   }
   return result;
}
 
Example 5
Source File: AMQPMessage.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public Object getObjectPropertyForFilter(SimpleString key) {
   if (key.startsWith(ANNOTATION_AREA_PREFIX)) {
      key = key.subSeq(ANNOTATION_AREA_PREFIX.length(), key.length());
      return getAnnotation(key);
   }

   Object value = getObjectProperty(key);
   if (value == null) {
      TypedProperties extra = getExtraProperties();
      if (extra != null) {
         value = extra.getProperty(key);
      }
   }
   return value;
}
 
Example 6
Source File: QueueInfo.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public boolean matchesAddress(SimpleString address) {
   boolean containsAddress = false;

   if (address != null) {
      SimpleString[] split = address.split(',');
      for (SimpleString addressPart : split) {
         containsAddress = address.startsWith(addressPart);

         if (containsAddress) {
            break;
         }
      }
   }

   return containsAddress;
}
 
Example 7
Source File: PrefixUtil.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public static Pair<SimpleString, EnumSet<RoutingType>> getAddressAndRoutingTypes(SimpleString address,
                                                                                 EnumSet<RoutingType> defaultRoutingTypes,
                                                                                 Map<SimpleString, RoutingType> prefixes) {
   for (Map.Entry<SimpleString, RoutingType> entry : prefixes.entrySet()) {
      if (address.startsWith(entry.getKey())) {
         return new Pair<>(removePrefix(address, entry.getKey()), EnumSet.of(entry.getValue()));
      }
   }
   return new Pair<>(address, defaultRoutingTypes);
}
 
Example 8
Source File: ActiveMQCompatibleMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected static SimpleString checkPrefix1X(SimpleString address) {
   if (address != null) {
      if (address.startsWith(PacketImpl.OLD_QUEUE_PREFIX)) {
         return address.subSeq(PacketImpl.OLD_QUEUE_PREFIX.length(), address.length());
      } else if (address.startsWith(PacketImpl.OLD_TEMP_QUEUE_PREFIX)) {
         return address.subSeq(PacketImpl.OLD_TEMP_QUEUE_PREFIX.length(), address.length());
      } else if (address.startsWith(PacketImpl.OLD_TOPIC_PREFIX)) {
         return address.subSeq(PacketImpl.OLD_TOPIC_PREFIX.length(), address.length());
      } else if (address.startsWith(PacketImpl.OLD_TEMP_TOPIC_PREFIX)) {
         return address.subSeq(PacketImpl.OLD_TEMP_TOPIC_PREFIX.length(), address.length());
      }
   }

   return null;
}
 
Example 9
Source File: ServerSessionImpl.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public RoutingType getRoutingTypeFromPrefix(SimpleString address, RoutingType defaultRoutingType) {
   if (prefixEnabled) {
      for (Map.Entry<SimpleString, RoutingType> entry : prefixes.entrySet()) {
         if (address.startsWith(entry.getKey())) {
            return entry.getValue();
         }
      }
   }
   return defaultRoutingType;
}
 
Example 10
Source File: BucketMessageGroups.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private int getBucket(SimpleString key) {
   Object bucketKey = key;
   if (key.startsWith(_AMQ_GROUP_BUCKET_INT_KEY)) {
      bucketKey = retrieveBucketIntFromKey(key);
   }
   return getHashBucket(bucketKey, bucketCount);
}
 
Example 11
Source File: ClusterConnectionBridge.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
protected Message beforeForward(final Message message, final SimpleString forwardingAddress) {
   // We make a copy of the message, then we strip out the unwanted routing id headers and leave
   // only
   // the one pertinent for the address node - this is important since different queues on different
   // nodes could have same queue ids
   // Note we must copy since same message may get routed to other nodes which require different headers
   Message messageCopy = message.copy();

   if (logger.isTraceEnabled()) {
      logger.trace("Clustered bridge  copied message " + message + " as " + messageCopy + " before delivery");
   }

   // TODO - we can optimise this

   Set<SimpleString> propNames = new HashSet<>(messageCopy.getPropertyNames());

   byte[] queueIds = message.getExtraBytesProperty(idsHeaderName);

   if (queueIds == null) {
      // Sanity check only
      ActiveMQServerLogger.LOGGER.noQueueIdDefined(message, messageCopy, idsHeaderName);
      throw new IllegalStateException("no queueIDs defined");
   }

   for (SimpleString propName : propNames) {
      if (propName.startsWith(Message.HDR_ROUTE_TO_IDS)) {
         messageCopy.removeProperty(propName);
      }
   }

   messageCopy.putExtraBytesProperty(Message.HDR_ROUTE_TO_IDS, queueIds);

   messageCopy = super.beforeForwardingNoCopy(messageCopy, forwardingAddress);

   return messageCopy;
}
 
Example 12
Source File: QueueAbstractPacket.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public static SimpleString getOldPrefixedAddress(SimpleString address, RoutingType routingType) {
   if (routingType == RoutingType.MULTICAST && !address.startsWith(OLD_TOPIC_PREFIX)) {
      return OLD_TOPIC_PREFIX.concat(address);
   } else if (routingType == RoutingType.ANYCAST && !address.startsWith(OLD_QUEUE_PREFIX)) {
      return OLD_QUEUE_PREFIX.concat(address);
   }

   return address;
}
 
Example 13
Source File: QueueAbstractPacket.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private static SimpleString jmsPrefixOf(SimpleString address) {
   if (address.startsWith(OLD_QUEUE_PREFIX)) {
      return OLD_QUEUE_PREFIX;
   } else if (address.startsWith(OLD_TOPIC_PREFIX)) {
      return OLD_TOPIC_PREFIX;
   } else {
      return null;
   }
}
 
Example 14
Source File: QueueAbstractPacket.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * It converts the given {@code queueNames} using the JMS prefix found on {@link #address} when {@code clientVersion < }{@link #ADDRESSING_CHANGE_VERSION}.
 * If no conversion has occurred, it returns {@code queueNames}.
 *
 * @param clientVersion version of the client
 * @param queueNames    names of the queues to be converted
 * @return the converted queues names or {@code queueNames} when no conversion has occurred
 */
public final List<SimpleString> convertQueueNames(int clientVersion, List<SimpleString> queueNames) {
   if (clientVersion < ADDRESSING_CHANGE_VERSION) {
      final int names = queueNames.size();
      if (names == 0) {
         return Collections.emptyList();
      } else {
         final SimpleString prefix = jmsPrefixOf(this.address);
         if (prefix != null) {
            final List<SimpleString> prefixedQueueNames = new ArrayList<>(names);
            for (int i = 0; i < names; i++) {
               final SimpleString oldQueueName = queueNames.get(i);
               if (oldQueueName.startsWith(prefix)) {
                  prefixedQueueNames.add(oldQueueName);
               } else {
                  final SimpleString prefixedQueueName = prefix.concat(oldQueueName);
                  prefixedQueueNames.add(prefixedQueueName);
               }
            }
            return prefixedQueueNames;
         } else {
            return queueNames;
         }
      }
   } else {
      return queueNames;
   }
}
 
Example 15
Source File: PacketImpl.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public SimpleString convertName(SimpleString name) {
   if (name == null) {
      return null;
   }

   if (name.startsWith(OLD_QUEUE_PREFIX)) {
      return name.subSeq(OLD_QUEUE_PREFIX.length(), name.length());
   } else if (name.startsWith(OLD_TOPIC_PREFIX)) {
      return name.subSeq(OLD_TOPIC_PREFIX.length(), name.length());
   } else {
      return name;
   }
}
 
Example 16
Source File: PrefixUtil.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public static SimpleString getPrefix(SimpleString address, Map<SimpleString, RoutingType> prefixes) {
   for (Map.Entry<SimpleString, RoutingType> entry : prefixes.entrySet()) {
      if (address.startsWith(entry.getKey())) {
         return removeAddress(address, entry.getKey());
      }
   }
   return null;
}
 
Example 17
Source File: PrefixUtil.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public static SimpleString getAddress(SimpleString address, Map<SimpleString, RoutingType> prefixes) {
   for (Map.Entry<SimpleString, RoutingType> entry : prefixes.entrySet()) {
      if (address.startsWith(entry.getKey())) {
         return removePrefix(address, entry.getKey());
      }
   }
   return address;
}
 
Example 18
Source File: ServerSessionPacketHandler.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private RoutingType getRoutingTypeFromAddress(SimpleString address) {
   if (address.startsWith(PacketImpl.OLD_QUEUE_PREFIX) || address.startsWith(PacketImpl.OLD_TEMP_QUEUE_PREFIX)) {
      return RoutingType.ANYCAST;
   }
   return RoutingType.MULTICAST;
}
 
Example 19
Source File: QueueImpl.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"ArrayToString", "ArrayToStringConcatenation"})
private boolean moveBetweenSnFQueues(final SimpleString queueSuffix,
                                  final Transaction tx,
                                  final MessageReference ref) throws Exception {
   Message copyMessage = makeCopy(ref, false, false);

   byte[] oldRouteToIDs = null;
   String targetNodeID;
   Binding targetBinding;

   // remove the old route
   for (SimpleString propName : copyMessage.getPropertyNames()) {
      if (propName.startsWith(Message.HDR_ROUTE_TO_IDS)) {
         oldRouteToIDs = (byte[]) copyMessage.removeProperty(propName.toString());
         final String hashcodeToString = oldRouteToIDs.toString(); // don't use Arrays.toString(..) here
         logger.debug("Removed property from message: " + propName + " = " + hashcodeToString + " (" + ByteBuffer.wrap(oldRouteToIDs).getLong() + ")");

         // there should only be one of these properties so potentially save some loop iterations
         break;
      }
   }

   ByteBuffer oldBuffer = ByteBuffer.wrap(oldRouteToIDs);

   RoutingContext routingContext = new RoutingContextImpl(tx);

   /* this algorithm will look at the old route and find the new remote queue bindings where the messages should go
    * and route them there directly
    */
   while (oldBuffer.hasRemaining()) {
      long oldQueueID = oldBuffer.getLong();

      // look at all the bindings
      Pair<String, Binding> result = locateTargetBinding(queueSuffix, copyMessage, oldQueueID);
      targetBinding = result.getB();
      targetNodeID = result.getA();

      if (targetBinding == null) {
         ActiveMQServerLogger.LOGGER.unableToFindTargetQueue(targetNodeID);
      } else {
         logger.debug("Routing on binding: " + targetBinding);
         targetBinding.route(copyMessage, routingContext);
      }
   }

   postOffice.processRoute(copyMessage, routingContext, false);

   ref.handled();

   acknowledge(tx, ref);

   storageManager.afterCompleteOperations(new IOCallback() {

      @Override
      public void onError(final int errorCode, final String errorMessage) {
         ActiveMQServerLogger.LOGGER.ioErrorRedistributing(errorCode, errorMessage);
      }

      @Override
      public void done() {
         deliverAsync();
      }
   });

   return true;
}
 
Example 20
Source File: ScaleDownHandler.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private long scaleDownSNF(final SimpleString address,
                          final Set<Queue> queues,
                          final ClientProducer producer) throws Exception {
   long messageCount = 0;

   final String propertyEnd;

   // If this SNF is towards our targetNodeId
   boolean queueOnTarget = address.toString().endsWith(targetNodeId);

   if (queueOnTarget) {
      propertyEnd = targetNodeId;
   } else {
      propertyEnd = address.toString().substring(address.toString().lastIndexOf("."));
   }

   Transaction tx = new TransactionImpl(storageManager);

   for (Queue queue : queues) {
      // using auto-closeable
      try (LinkedListIterator<MessageReference> messagesIterator = queue.browserIterator()) {
         // loop through every message of this queue
         while (messagesIterator.hasNext()) {
            MessageReference messageRef = messagesIterator.next();
            Message message = messageRef.getMessage().copy();

            /* Here we are taking messages out of a store-and-forward queue and sending them to the corresponding
             * address on the scale-down target server.  However, we have to take the existing _AMQ_ROUTE_TOsf.*
             * property and put its value into the _AMQ_ROUTE_TO property so the message is routed properly.
             */

            byte[] oldRouteToIDs = null;

            List<SimpleString> propertiesToRemove = new ArrayList<>();
            message.removeProperty(Message.HDR_ROUTE_TO_IDS.toString());
            for (SimpleString propName : message.getPropertyNames()) {
               if (propName.startsWith(Message.HDR_ROUTE_TO_IDS)) {
                  if (propName.toString().endsWith(propertyEnd)) {
                     oldRouteToIDs = message.getBytesProperty(propName.toString());
                  }
                  propertiesToRemove.add(propName);
               }
            }

            // TODO: what if oldRouteToIDs == null ??

            for (SimpleString propertyToRemove : propertiesToRemove) {
               message.removeProperty(propertyToRemove.toString());
            }

            if (queueOnTarget) {
               message.putBytesProperty(Message.HDR_ROUTE_TO_IDS.toString(), oldRouteToIDs);
            } else {
               message.putBytesProperty(Message.HDR_SCALEDOWN_TO_IDS.toString(), oldRouteToIDs);
            }

            logger.debug("Scaling down message " + message + " from " + address + " to " + message.getAddress() + " on node " + targetNodeId);

            producer.send(message.getAddress(), message);

            messageCount++;

            messagesIterator.remove();

            ackMessageOnQueue(tx, queue, messageRef);
         }
      } catch (NoSuchElementException ignored) {
         // this could happen through paging browsing
      }
   }

   tx.commit();

   return messageCount;
}