private Object[] listSubscribersInfos(final DurabilityType durability) {
    List<QueueControl> queues = getQueues(durability);
    List<Object[]> subInfos = new ArrayList<>(queues.size());

    for (QueueControl queue : queues) {
      String clientID = null;
      String subName = null;

      if (queue.isDurable()) {
        Pair<String, String> pair =
            ActiveMQDestination.decomposeQueueNameForDurableSubscription(queue.getName());
        clientID = pair.getA();
        subName = pair.getB();
      }

      String filter = queue.getFilter() != null ? queue.getFilter() : null;

      Object[] subscriptionInfo = new Object[6];
      subscriptionInfo[0] = queue.getName();
      subscriptionInfo[1] = clientID;
      subscriptionInfo[2] = subName;
      subscriptionInfo[3] = queue.isDurable();
      subscriptionInfo[4] = queue.getMessageCount();
      subscriptionInfo[5] = filter;
      subInfos.add(subscriptionInfo);
    }
    return subInfos.toArray(new Object[subInfos.size()]);
  }
 @Override
 public void dropAllSubscriptions() throws Exception {
   ActiveMQServerControl serverControl =
       (ActiveMQServerControl) managementService.getResource(ResourceNames.CORE_SERVER);
   String[] queues = addressControl.getQueueNames();
   for (String queue : queues) {
     // Drop all subscription shouldn't delete the dummy queue used to identify if the topic exists
     // on the core queues.
     // we will just ignore this queue
     if (!queue.equals(managedTopic.getAddress())) {
       serverControl.destroyQueue(queue);
     }
   }
 }
Exemplo n.º 3
0
  public static void main(String[] args) throws Exception {
    ConnectionFactory factory = new ActiveMQJMSConnectionFactory("tcp://localhost:61616");
    Destination destination = ActiveMQDestination.fromPrefixedName("queue://orders");

    try (Connection conn = factory.createConnection()) {
      Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer producer = session.createProducer(destination);
      ObjectMessage message = session.createObjectMessage();

      Order order = new Order("Bill", "$199.99", "iPhone4");
      message.setObject(order);
      producer.send(message);
    }
  }
 @Override
 public void dropDurableSubscription(final String clientID, final String subscriptionName)
     throws Exception {
   String queueName =
       ActiveMQDestination.createQueueNameForDurableSubscription(true, clientID, subscriptionName);
   QueueControl coreQueueControl =
       (QueueControl) managementService.getResource(ResourceNames.CORE_QUEUE + queueName);
   if (coreQueueControl == null) {
     throw new IllegalArgumentException(
         "No subscriptions with name " + queueName + " for clientID " + clientID);
   }
   ActiveMQServerControl serverControl =
       (ActiveMQServerControl) managementService.getResource(ResourceNames.CORE_SERVER);
   serverControl.destroyQueue(queueName);
 }
 @Override
 public int countMessagesForSubscription(
     final String clientID, final String subscriptionName, final String filterStr)
     throws Exception {
   String queueName =
       ActiveMQDestination.createQueueNameForDurableSubscription(true, clientID, subscriptionName);
   QueueControl coreQueueControl =
       (QueueControl) managementService.getResource(ResourceNames.CORE_QUEUE + queueName);
   if (coreQueueControl == null) {
     throw new IllegalArgumentException(
         "No subscriptions with name " + queueName + " for clientID " + clientID);
   }
   String filter = JMSTopicControlImpl.createFilterFromJMSSelector(filterStr);
   return coreQueueControl.listMessages(filter).length;
 }
  private String listSubscribersInfosAsJSON(final DurabilityType durability) throws Exception {
    try {
      List<QueueControl> queues = getQueues(durability);
      JSONArray array = new JSONArray();

      for (QueueControl queue : queues) {
        String clientID = null;
        String subName = null;

        if (queue.isDurable() && !queue.getName().startsWith(ResourceNames.JMS_TOPIC)) {
          Pair<String, String> pair =
              ActiveMQDestination.decomposeQueueNameForDurableSubscription(queue.getName());
          clientID = pair.getA();
          subName = pair.getB();
        } else if (queue.getName().startsWith(ResourceNames.JMS_TOPIC)) {
          // in the case of heirarchical topics the queue name will not follow the <part>.<part>
          // pattern of normal
          // durable subscribers so skip decomposing the name for the client ID and subscription
          // name and just
          // hard-code it
          clientID = "ActiveMQ";
          subName = "ActiveMQ";
        }

        String filter = queue.getFilter() != null ? queue.getFilter() : null;

        JSONObject info = new JSONObject();

        info.put("queueName", queue.getName());
        info.put("clientID", clientID);
        info.put("selector", filter);
        info.put("name", subName);
        info.put("durable", queue.isDurable());
        info.put("messageCount", queue.getMessageCount());
        info.put("deliveringCount", queue.getDeliveringCount());
        info.put("consumers", new JSONArray(queue.listConsumersAsJSON()));
        array.put(info);
      }

      return array.toString();
    } catch (Exception e) {
      e.printStackTrace();
      return e.toString();
    }
  }
 @Override
 public String getAddress() {
   return managedTopic.getAddress();
 }
 @Override
 public boolean isTemporary() {
   return managedTopic.isTemporary();
 }
 @Override
 public String getName() {
   return managedTopic.getName();
 }
 @Override
 public String[] getRegistryBindings() {
   return jmsServerManager.getBindingsOnTopic(managedTopic.getName());
 }
 @Override
 public void addBinding(String binding) throws Exception {
   jmsServerManager.addTopicToBindingRegistry(managedTopic.getName(), binding);
 }
Exemplo n.º 12
0
  public void init() throws Exception {
    AMQServerSession coreSession = session.getCoreSession();

    SimpleString selector =
        info.getSelector() == null ? null : new SimpleString(info.getSelector());

    nativeId = session.getCoreServer().getStorageManager().generateID();

    SimpleString address = new SimpleString(this.actualDest.getPhysicalName());

    if (this.actualDest.isTopic()) {
      String physicalName = this.actualDest.getPhysicalName();
      if (physicalName.contains(".>")) {
        // wildcard
        physicalName = OpenWireUtil.convertWildcard(physicalName);
      }

      // on recreate we don't need to create queues
      address = new SimpleString("jms.topic." + physicalName);
      if (info.isDurable()) {
        subQueueName =
            new SimpleString(
                ActiveMQDestination.createQueueNameForDurableSubscription(
                    true, info.getClientId(), info.getSubscriptionName()));

        QueueQueryResult result = coreSession.executeQueueQuery(subQueueName);
        if (result.isExists()) {
          // Already exists
          if (result.getConsumerCount() > 0) {
            throw new IllegalStateException(
                "Cannot create a subscriber on the durable subscription since it already has subscriber(s)");
          }

          SimpleString oldFilterString = result.getFilterString();

          boolean selectorChanged =
              selector == null && oldFilterString != null
                  || oldFilterString == null && selector != null
                  || oldFilterString != null
                      && selector != null
                      && !oldFilterString.equals(selector);

          SimpleString oldTopicName = result.getAddress();

          boolean topicChanged = !oldTopicName.equals(address);

          if (selectorChanged || topicChanged) {
            // Delete the old durable sub
            coreSession.deleteQueue(subQueueName);

            // Create the new one
            coreSession.createQueue(address, subQueueName, selector, false, true);
          }

        } else {
          coreSession.createQueue(address, subQueueName, selector, false, true);
        }
      } else {
        subQueueName = new SimpleString(UUID.randomUUID().toString());

        coreSession.createQueue(address, subQueueName, selector, true, false);
      }

      coreSession.createConsumer(nativeId, subQueueName, null, info.isBrowser(), false, -1);
    } else {
      SimpleString queueName = new SimpleString("jms.queue." + this.actualDest.getPhysicalName());
      coreSession.createConsumer(nativeId, queueName, selector, info.isBrowser(), false, -1);
    }

    if (info.isBrowser()) {
      AMQServerConsumer coreConsumer = coreSession.getConsumer(nativeId);
      coreConsumer.setBrowserListener(this);
    }
  }