Example #1
0
  public void testWhenTrue() throws InterruptedException {
    final WaitableBoolean blocker = new WaitableBoolean(false);
    final WaitableBoolean actionPerformed = new WaitableBoolean(false);

    Runnable switcher =
        new Runnable() {
          public void run() {
            try {
              Thread.sleep(500);
              blocker.set(true);
            } catch (InterruptedException iex) {
              // ignore
            }
          }
        };

    Runnable action =
        new Runnable() {
          public void run() {
            actionPerformed.set(true);
          }
        };

    new Thread(switcher).start();
    blocker.whenTrue(action);
    assertTrue(blocker.get());
    assertTrue(actionPerformed.get());
  }
Example #2
0
  public void testComplement() {
    assertFalse(TRUE.complement());
    assertFalse(TRUE.get());

    assertTrue(FALSE.complement());
    assertTrue(FALSE.get());
  }
Example #3
0
 public void testCommit() {
   assertTrue(TRUE.commit(true, true));
   assertTrue(TRUE.get());
   assertFalse(TRUE.commit(false, true));
   assertTrue(TRUE.commit(true, false));
   assertFalse(TRUE.get());
   assertTrue(TRUE.commit(false, true));
   assertTrue(TRUE.get());
 }
Example #4
0
  public void testOr() {
    assertTrue((new WaitableBoolean(true)).or(true));
    assertTrue((new WaitableBoolean(true)).or(false));
    assertFalse((new WaitableBoolean(false)).or(false));
    assertTrue((new WaitableBoolean(false)).or(true));

    assertTrue(TRUE.or(true));
    assertTrue(TRUE.get());
    assertTrue(TRUE.or(false));
    assertTrue(TRUE.get());
  }
Example #5
0
  public void testAnd() {
    assertTrue((new WaitableBoolean(true)).and(true));
    assertFalse((new WaitableBoolean(true)).and(false));
    assertFalse((new WaitableBoolean(false)).and(false));
    assertFalse((new WaitableBoolean(false)).and(true));

    assertTrue(TRUE.and(true));
    assertTrue(TRUE.get());
    assertFalse(TRUE.and(false));
    assertFalse(TRUE.get());
  }
Example #6
0
  public void testWhenTrueAlreadyTrue() throws InterruptedException {
    final WaitableBoolean blocker = new WaitableBoolean(true);
    final WaitableBoolean actionPerformed = new WaitableBoolean(false);

    Runnable action =
        new Runnable() {
          public void run() {
            actionPerformed.set(true);
          }
        };

    blocker.whenTrue(action);
    assertTrue(blocker.get());
    assertTrue(actionPerformed.get());
  }
Example #7
0
  public final synchronized void connect() throws Exception {
    // This method may be called to ensure transport is connected, if it is
    // already connected then just return.
    if (connected.get()) {
      return;
    }

    if (disposed.get()) {
      throw new IllegalStateException(
          "Requester/dispatcher has been disposed; cannot connect to resource");
    }

    retryTemplate.execute(
        new RetryCallback() {
          public void doWork(RetryContext context) throws Exception {
            try {
              doConnect();
              connected.set(true);
              if (startOnConnect) {
                start();
              }
            } catch (Exception e) {
              if (logger.isDebugEnabled()) {
                logger.debug(e);
              }
              throw e;
            }
          }

          public String getWorkDescription() {
            return getConnectionDescription();
          }
        },
        getWorkManager());
  }
Example #8
0
  public void dispatchEvent(MuleEvent event) throws MuleException {
    if (stopping.get() || stopped.get()) {
      throw new ServiceException(CoreMessages.componentIsStopped(name), event.getMessage(), this);
    }

    // Dispatching event to an inbound endpoint
    // in the DefaultMuleSession#dispatchEvent
    ImmutableEndpoint endpoint = event.getEndpoint();

    if (endpoint instanceof OutboundEndpoint) {
      try {
        ((OutboundEndpoint) endpoint).dispatch(event);
      } catch (Exception e) {
        throw new DispatchException(event.getMessage(), event.getEndpoint(), e);
      }

      return;
    }

    if (logger.isDebugEnabled()) {
      logger.debug(
          "Service: "
              + name
              + " has received asynchronous event on: "
              + event.getEndpoint().getEndpointURI());
    }

    // Dispatching event to the service
    if (stats.isEnabled()) {
      stats.incReceivedEventASync();
    }
    doDispatch(event);
  }
Example #9
0
  public final void start() throws MuleException {
    if (!connected.get()) {
      startOnConnect = true;

      // Make sure we are connected
      try {
        connect();
      } catch (Exception e) {
        throw new LifecycleException(e, this);
      }
      return;
    }

    if (stopped.compareAndSet(true, false)) {
      doStart();
    }
  }
Example #10
0
  public final synchronized void disconnect() throws Exception {
    if (!connected.get()) {
      return;
    }

    if (logger.isDebugEnabled()) {
      logger.debug("Disconnecting: " + this);
    }

    this.doDisconnect();
    connected.set(false);

    logger.info("Disconnected: " + this);

    connector.fireNotification(
        new ConnectionNotification(
            this, getConnectEventId(endpoint), ConnectionNotification.CONNECTION_DISCONNECTED));
  }
Example #11
0
  public final void stop() {
    try {
      if (connected.get()) {
        disconnect();
      }
    } catch (Exception e) {
      // TODO MULE-863: What should we really do?
      logger.error(e.getMessage(), e);
    }

    if (stopped.compareAndSet(false, true)) {
      try {
        doStop();
      } catch (MuleException e) {
        // TODO MULE-863: What should we really do?
        logger.error(e.getMessage(), e);
      }
    }
  }
Example #12
0
  public MuleMessage sendEvent(MuleEvent event) throws MuleException {
    if (stopping.get() || stopped.get()) {
      throw new ServiceException(CoreMessages.componentIsStopped(name), event.getMessage(), this);
    }

    try {
      waitIfPaused(event);
    } catch (InterruptedException e) {
      throw new ServiceException(event.getMessage(), this, e);
    }

    if (stats.isEnabled()) {
      stats.incReceivedEventSync();
    }
    if (logger.isDebugEnabled()) {
      logger.debug(
          "Service: "
              + name
              + " has received synchronous event on: "
              + event.getEndpoint().getEndpointURI());
    }
    event = OptimizedRequestContext.unsafeSetEvent(event);
    return doSend(event);
  }
Example #13
0
 public boolean isStopping() {
   return stopping.get();
 }
Example #14
0
 /**
  * Called before an event is sent or dispatched to a service, it will block until resume() is
  * called. Users can override this method if they want to handle pausing differently e.g.
  * implement a store and forward policy
  *
  * @param event the current event being passed to the service
  * @throws InterruptedException if the thread is interrupted
  */
 protected void waitIfPaused(MuleEvent event) throws InterruptedException {
   if (logger.isDebugEnabled() && paused.get()) {
     logger.debug("Service: " + name + " is paused. Blocking call until resume is called");
   }
   paused.whenFalse(null);
 }
Example #15
0
 /**
  * Determines if the service is in a paused state
  *
  * @return True if the service is in a paused state, false otherwise
  */
 public boolean isPaused() {
   return paused.get();
 }
Example #16
0
 public boolean isStarted() {
   return !stopped.get();
 }
Example #17
0
 public void testGet() {
   assertTrue(TRUE.get());
   assertFalse(FALSE.get());
 }
Example #18
0
 public final boolean isConnected() {
   return connected.get();
 }