Example #1
0
 @SuppressWarnings("LockAcquiredButNotSafelyReleased")
 protected void handleLock(String[] args) {
   String lockStr = args[0];
   String key = args[1];
   Lock lock = hazelcast.getLock(key);
   if (lockStr.equalsIgnoreCase("lock")) {
     lock.lock();
     println("true");
   } else if (lockStr.equalsIgnoreCase("unlock")) {
     lock.unlock();
     println("true");
   } else if (lockStr.equalsIgnoreCase("trylock")) {
     String timeout = args.length > 2 ? args[2] : null;
     if (timeout == null) {
       println(lock.tryLock());
     } else {
       long time = Long.valueOf(timeout);
       try {
         println(lock.tryLock(time, TimeUnit.SECONDS));
       } catch (InterruptedException e) {
         e.printStackTrace();
       }
     }
   }
 }
Example #2
0
 protected void handleQPoll(String[] args) {
   long timeout = 0;
   if (args.length > 1) {
     timeout = Long.valueOf(args[1]);
   }
   try {
     println(getQueue().poll(timeout, TimeUnit.SECONDS));
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
 }
Example #3
0
 protected void handleQOffer(String[] args) {
   long timeout = 0;
   if (args.length > 2) {
     timeout = Long.valueOf(args[2]);
   }
   try {
     boolean offered = getQueue().offer(args[1], timeout, TimeUnit.SECONDS);
     println(offered);
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
 }
Example #4
0
 protected void handleMapTryLock(String[] args) {
   String key = args[1];
   long time = (args.length > 2) ? Long.valueOf(args[2]) : 0;
   boolean locked = false;
   if (time == 0) locked = getMap().tryLock(key);
   else
     try {
       locked = getMap().tryLock(key, time, TimeUnit.SECONDS);
     } catch (InterruptedException e) {
       locked = false;
     }
   println(locked);
 }
  /**
   * Attempts to receive a message using the given strategy. The method will continue to attempt to
   * receive until either {@link ReceiveStrategy#isRetryable()} returns false, a message is
   * received, or the consumer is stopped.
   *
   * @param strategy the strategy to use for receiving the message and determining retries
   * @return the message or null if no message was received
   */
  private HazelcastMQMessage doReceive(ReceiveStrategy strategy) {

    HazelcastMQMessage msg = null;

    do {
      receiveLock.lock();
      try {

        IQueue<byte[]> queue = hazelcastMQContext.resolveQueue(destination);

        if (queue == null && topicListener == null) {
          throw new HazelcastMQException(
              format("Destination cannot be resolved [%s].", destination));
        } else if (queue == null) {
          queue = topicListener.getQueue();
        }

        byte[] msgData = strategy.receive(queue);
        if (msgData != null) {
          msg = config.getMessageConverter().toMessage(msgData);
        }

        // Check for message expiration if we have a message with expiration
        // time.
        if (msg != null && msg.getHeaders().get(Headers.EXPIRATION) != null) {
          long expirationTime = Long.parseLong(msg.getHeaders().get(Headers.EXPIRATION));

          if (expirationTime != 0 && expirationTime <= System.currentTimeMillis()) {
            log.info("Dropping message [{}] because it has expired.", msg.getId());
            msg = null;
          }
        }
      } finally {
        receiveLock.unlock();
      }
    } while (msg == null && !closed && strategy.isRetryable());

    return msg;
  }
Example #6
0
 private void handleAtomicNumberSet(String[] args) {
   long v = 0;
   if (args.length > 1) v = Long.valueOf(args[1]);
   getAtomicNumber().set(v);
   println(getAtomicNumber().get());
 }