Example #1
0
  protected List<QueueBinding> getLocalQueueBindings(
      final PostOffice postOffice, final String address) throws Exception {
    ArrayList<QueueBinding> bindingsFound = new ArrayList<QueueBinding>();

    Bindings bindings = postOffice.getBindingsForAddress(new SimpleString(address));

    for (Binding binding : bindings.getBindings()) {
      if (binding instanceof LocalQueueBinding) {
        bindingsFound.add((QueueBinding) binding);
      }
    }
    return bindingsFound;
  }
 private synchronized void removeAndUpdateAddressMap(final Address address) throws Exception {
   // we only remove if there are no bindings left
   Bindings bindings = super.getBindingsForRoutingAddress(address.getAddress());
   if (bindings == null || bindings.getBindings().size() == 0) {
     List<Address> addresses = address.getLinkedAddresses();
     for (Address address1 : addresses) {
       address1.removeLinkedAddress(address);
       Bindings linkedBindings = super.getBindingsForRoutingAddress(address1.getAddress());
       if (linkedBindings == null || linkedBindings.getBindings().size() == 0) {
         removeAddress(address1);
       }
     }
     removeAddress(address);
   }
 }
Example #3
0
 public String[] getBindingNames() throws Exception {
   clearIO();
   try {
     Bindings bindings = postOffice.getBindingsForAddress(address);
     String[] bindingNames = new String[bindings.getBindings().size()];
     int i = 0;
     for (Binding binding : bindings.getBindings()) {
       bindingNames[i++] = binding.getUniqueName().toString();
     }
     return bindingNames;
   } catch (Throwable t) {
     throw new IllegalStateException(t.getMessage());
   } finally {
     blockOnIO();
   }
 }
Example #4
0
 public String[] getQueueNames() throws Exception {
   clearIO();
   try {
     Bindings bindings = postOffice.getBindingsForAddress(address);
     List<String> queueNames = new ArrayList<String>();
     for (Binding binding : bindings.getBindings()) {
       if (binding instanceof QueueBinding) {
         queueNames.add(binding.getUniqueName().toString());
       }
     }
     return queueNames.toArray(new String[queueNames.size()]);
   } catch (Throwable t) {
     throw new IllegalStateException(t.getMessage());
   } finally {
     blockOnIO();
   }
 }
Example #5
0
 public long getNumberOfMessages() throws Exception {
   clearIO();
   long totalMsgs = 0;
   try {
     Bindings bindings = postOffice.getBindingsForAddress(address);
     List<String> queueNames = new ArrayList<String>();
     for (Binding binding : bindings.getBindings()) {
       if (binding instanceof QueueBinding) {
         totalMsgs += ((QueueBinding) binding).getQueue().getMessageCount();
       }
     }
     return totalMsgs;
   } catch (Throwable t) {
     throw new IllegalStateException(t.getMessage());
   } finally {
     blockOnIO();
   }
 }
 /**
  * If the address to add the binding to contains a wildcard then a copy of the binding (with the
  * same underlying queue) will be added to the actual mappings. Otherwise the binding is added as
  * normal.
  *
  * @param binding the binding to add
  * @return true if the address was a new mapping
  */
 @Override
 public boolean addBinding(final Binding binding) throws Exception {
   boolean exists = super.addBinding(binding);
   if (!exists) {
     Address add = addAndUpdateAddressMap(binding.getAddress());
     if (add.containsWildCard()) {
       for (Address destAdd : add.getLinkedAddresses()) {
         super.addMappingInternal(destAdd.getAddress(), binding);
       }
     } else {
       for (Address destAdd : add.getLinkedAddresses()) {
         Bindings bindings = super.getBindingsForRoutingAddress(destAdd.getAddress());
         for (Binding b : bindings.getBindings()) {
           super.addMappingInternal(binding.getAddress(), b);
         }
       }
     }
   }
   return exists;
 }
  @Override
  public Bindings getBindingsForRoutingAddress(final SimpleString address) throws Exception {
    Bindings bindings = super.getBindingsForRoutingAddress(address);

    // this should only happen if we're routing to an address that has no mappings when we're
    // running checkAllowable
    if (bindings == null && !wildCardAddresses.isEmpty()) {
      Address add = addAndUpdateAddressMap(address);
      if (!add.containsWildCard()) {
        for (Address destAdd : add.getLinkedAddresses()) {
          Bindings b = super.getBindingsForRoutingAddress(destAdd.getAddress());
          if (b != null) {
            Collection<Binding> theBindings = b.getBindings();
            for (Binding theBinding : theBindings) {
              super.addMappingInternal(address, theBinding);
            }
          }
        }
      }
      bindings = super.getBindingsForRoutingAddress(address);
    }
    return bindings;
  }
 /**
  * If the address is a wild card then the binding will be removed from the actual mappings for any
  * linked address. otherwise it will be removed as normal.
  *
  * @param uniqueName the name of the binding to remove
  * @return true if this was the last mapping for a specific address
  */
 @Override
 public Binding removeBinding(final SimpleString uniqueName) throws Exception {
   Binding binding = super.removeBinding(uniqueName);
   if (binding != null) {
     Address add = getAddress(binding.getAddress());
     if (!add.containsWildCard()) {
       for (Address theAddress : add.getLinkedAddresses()) {
         Bindings bindings = super.getBindingsForRoutingAddress(theAddress.getAddress());
         if (bindings != null) {
           for (Binding b : bindings.getBindings()) {
             super.removeBindingInternal(binding.getAddress(), b.getUniqueName());
           }
         }
       }
     } else {
       for (Address theAddress : add.getLinkedAddresses()) {
         super.removeBindingInternal(theAddress.getAddress(), uniqueName);
       }
     }
     removeAndUpdateAddressMap(add);
   }
   return binding;
 }
Example #9
0
  public BindingQueryResult executeBindingQuery(final SimpleString address) throws Exception {
    if (address == null) {
      throw HornetQMessageBundle.BUNDLE.addressIsNull();
    }

    List<SimpleString> names = new ArrayList<SimpleString>();

    // make an exception for the management address (see HORNETQ-29)
    if (address.equals(managementAddress)) {
      return new BindingQueryResult(true, names);
    }

    Bindings bindings = postOffice.getMatchingBindings(address);

    for (Binding binding : bindings.getBindings()) {
      if (binding.getType() == BindingType.LOCAL_QUEUE
          || binding.getType() == BindingType.REMOTE_QUEUE) {
        names.add(binding.getUniqueName());
      }
    }

    return new BindingQueryResult(!names.isEmpty(), names);
  }
Example #10
0
    private synchronized void doBindingAdded(final ClientMessage message) throws Exception {
      if (HornetQServerLogger.LOGGER.isTraceEnabled()) {
        HornetQServerLogger.LOGGER.trace(ClusterConnectionImpl.this + " Adding binding " + message);
      }
      if (!message.containsProperty(ManagementHelper.HDR_DISTANCE)) {
        throw new IllegalStateException("distance is null");
      }

      if (!message.containsProperty(ManagementHelper.HDR_ADDRESS)) {
        throw new IllegalStateException("queueAddress is null");
      }

      if (!message.containsProperty(ManagementHelper.HDR_CLUSTER_NAME)) {
        throw new IllegalStateException("clusterName is null");
      }

      if (!message.containsProperty(ManagementHelper.HDR_ROUTING_NAME)) {
        throw new IllegalStateException("routingName is null");
      }

      if (!message.containsProperty(ManagementHelper.HDR_BINDING_ID)) {
        throw new IllegalStateException("queueID is null");
      }

      Integer distance = message.getIntProperty(ManagementHelper.HDR_DISTANCE);

      SimpleString queueAddress = message.getSimpleStringProperty(ManagementHelper.HDR_ADDRESS);

      SimpleString clusterName = message.getSimpleStringProperty(ManagementHelper.HDR_CLUSTER_NAME);

      SimpleString routingName = message.getSimpleStringProperty(ManagementHelper.HDR_ROUTING_NAME);

      SimpleString filterString =
          message.getSimpleStringProperty(ManagementHelper.HDR_FILTERSTRING);

      Long queueID = message.getLongProperty(ManagementHelper.HDR_BINDING_ID);

      RemoteQueueBinding binding =
          new RemoteQueueBindingImpl(
              server.getStorageManager().generateUniqueID(),
              queueAddress,
              clusterName,
              routingName,
              queueID,
              filterString,
              queue,
              bridge.getName(),
              distance + 1);

      if (postOffice.getBinding(clusterName) != null) {
        // Sanity check - this means the binding has already been added via another bridge, probably
        // max
        // hops is too high
        // or there are multiple cluster connections for the same address

        HornetQServerLogger.LOGGER.remoteQueueAlreadyBoundOnClusterConnection(this, clusterName);

        return;
      }

      if (isTrace) {
        HornetQServerLogger.LOGGER.trace(
            "Adding binding " + clusterName + " into " + ClusterConnectionImpl.this);
      }

      bindings.put(clusterName, binding);

      try {
        postOffice.addBinding(binding);
      } catch (Exception ignore) {
      }

      Bindings theBindings = postOffice.getBindingsForAddress(queueAddress);

      theBindings.setRouteWhenNoConsumers(routeWhenNoConsumers);
    }
Example #11
0
  @Test
  public void testPageCounter() throws Throwable {
    boolean persistentMessages = true;

    Configuration config = createDefaultConfig();

    config.setJournalSyncNonTransactional(false);

    HornetQServer server =
        createServer(true, config, PAGE_SIZE, PAGE_MAX, new HashMap<String, AddressSettings>());

    server.start();

    final int messageSize = 1024;

    final int numberOfMessages = 500;

    ServerLocator locator = createInVMNonHALocator();

    locator.setClientFailureCheckPeriod(1000);
    locator.setConnectionTTL(2000);
    locator.setReconnectAttempts(0);

    locator.setBlockOnNonDurableSend(true);
    locator.setBlockOnDurableSend(true);
    locator.setBlockOnAcknowledge(true);
    locator.setConsumerWindowSize(1024 * 1024);

    ClientSessionFactory sf = createSessionFactory(locator);

    ClientSession session = sf.createSession(false, false, false);

    Queue q1 = server.createQueue(ADDRESS, ADDRESS, null, true, false);

    Queue q2 = server.createQueue(ADDRESS, new SimpleString("inactive"), null, true, false);

    ClientProducer producer = session.createProducer(PagingTest.ADDRESS);

    byte[] body = new byte[messageSize];

    ByteBuffer bb = ByteBuffer.wrap(body);

    for (int j = 1; j <= messageSize; j++) {
      bb.put(getSamplebyte(j));
    }

    final AtomicInteger errors = new AtomicInteger(0);

    Thread t1 =
        new Thread() {
          @Override
          public void run() {
            try {
              ServerLocator sl = createInVMNonHALocator();
              ClientSessionFactory sf = sl.createSessionFactory();
              ClientSession sess = sf.createSession(true, true, 0);
              sess.start();
              ClientConsumer cons = sess.createConsumer(ADDRESS);
              for (int i = 0; i < numberOfMessages; i++) {
                ClientMessage msg = cons.receive(5000);
                assertNotNull(msg);
                assertEquals(i, msg.getIntProperty("id").intValue());
                msg.acknowledge();
              }

              assertNull(cons.receiveImmediate());
              sess.close();
              sl.close();
            } catch (Throwable e) {
              e.printStackTrace();
              errors.incrementAndGet();
            }
          }
        };

    t1.start();

    for (int i = 0; i < numberOfMessages; i++) {
      ClientMessage message = session.createMessage(persistentMessages);

      HornetQBuffer bodyLocal = message.getBodyBuffer();

      bodyLocal.writeBytes(body);

      message.putIntProperty(new SimpleString("id"), i);

      producer.send(message);
      if (i % 20 == 0) {
        session.commit();
      }
    }

    session.commit();

    t1.join();

    assertEquals(0, errors.get());

    assertEquals(numberOfMessages, q2.getMessageCount());
    assertEquals(numberOfMessages, q2.getMessagesAdded());
    assertEquals(0, q1.getMessageCount());
    assertEquals(numberOfMessages, q1.getMessagesAdded());

    session.close();
    sf.close();
    locator.close();

    server.stop();

    server.start();

    Bindings bindings = server.getPostOffice().getBindingsForAddress(ADDRESS);

    q1 = null;
    q2 = null;

    for (Binding bind : bindings.getBindings()) {
      if (bind instanceof LocalQueueBinding) {
        LocalQueueBinding qb = (LocalQueueBinding) bind;
        if (qb.getQueue().getName().equals(ADDRESS)) {
          q1 = qb.getQueue();
        }

        if (qb.getQueue().getName().equals(new SimpleString("inactive"))) {
          q2 = qb.getQueue();
        }
      }
    }

    assertNotNull(q1);

    assertNotNull(q2);

    assertEquals("q2 msg count", numberOfMessages, q2.getMessageCount());
    assertEquals("q2 msgs added", numberOfMessages, q2.getMessagesAdded());
    assertEquals("q1 msg count", 0, q1.getMessageCount());
    // 0, since nothing was sent to the queue after the server was restarted
    assertEquals("q1 msgs added", 0, q1.getMessagesAdded());
  }