예제 #1
0
  @Override
  public boolean allocate(List<? extends Resource<?, ?>> resources, ResourceConsumer consumer) {
    checkNotNull(resources);
    checkNotNull(consumer);

    TransactionContext tx = service.transactionContextBuilder().build();
    tx.begin();

    try {
      TransactionalMap<Resource<?, ?>, ResourceConsumer> txMap =
          tx.getTransactionalMap(MAP_NAME, SERIALIZER);
      for (Resource<?, ?> resource : resources) {
        ResourceConsumer existing = txMap.putIfAbsent(resource, consumer);
        // if the resource is already allocated to another consumer, the whole allocation fails
        if (existing != null) {
          tx.abort();
          return false;
        }
      }
      tx.commit();
      return true;
    } catch (Exception e) {
      log.error("Exception thrown, abort the transaction", e);
      tx.abort();
      return false;
    }
  }
예제 #2
0
  @Override
  public boolean release(
      List<? extends Resource<?, ?>> resources, List<ResourceConsumer> consumers) {
    checkNotNull(resources);
    checkNotNull(consumers);
    checkArgument(resources.size() == consumers.size());

    TransactionContext tx = service.transactionContextBuilder().build();
    tx.begin();

    try {
      TransactionalMap<Resource<?, ?>, ResourceConsumer> txMap =
          tx.getTransactionalMap(MAP_NAME, SERIALIZER);
      Iterator<? extends Resource<?, ?>> resourceIte = resources.iterator();
      Iterator<ResourceConsumer> consumerIte = consumers.iterator();

      while (resourceIte.hasNext() && consumerIte.hasNext()) {
        Resource<?, ?> resource = resourceIte.next();
        ResourceConsumer consumer = consumerIte.next();

        // if this single release fails (because the resource is allocated to another consumer,
        // the whole release fails
        if (!txMap.remove(resource, consumer)) {
          tx.abort();
          return false;
        }
      }

      return true;
    } catch (TransactionException e) {
      log.error("Exception thrown, abort the transaction", e);
      tx.abort();
      return false;
    }
  }
예제 #3
0
 /**
  * Abort the transaction.
  *
  * @param tx transaction context
  * @return always false
  */
 private boolean abortTransaction(TransactionContext tx) {
   tx.abort();
   return false;
 }