Exemple #1
0
  public void testGetLock() {
    WaitableBoolean b = new WaitableBoolean(true);
    assertSame(b, b.getLock());

    b = new WaitableBoolean(true, this);
    assertSame(this, b.getLock());
  }
Exemple #2
0
  public void testComplement() {
    assertFalse(TRUE.complement());
    assertFalse(TRUE.get());

    assertTrue(FALSE.complement());
    assertTrue(FALSE.get());
  }
Exemple #3
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());
  }
Exemple #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());
  }
Exemple #5
0
 public void forceStop() throws MuleException {
   if (!stopped.get()) {
     logger.debug("Stopping Service");
     stopping.set(true);
     fireServiceNotification(ServiceNotification.SERVICE_STOPPING);
     doForceStop();
     stopped.set(true);
     stopping.set(false);
     fireServiceNotification(ServiceNotification.SERVICE_STOPPED);
   }
 }
Exemple #6
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());
  }
Exemple #7
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());
  }
Exemple #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);
  }
Exemple #9
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());
  }
Exemple #10
0
  public void stop() throws MuleException {
    if (!stopped.get()) {
      logger.debug("Stopping Service");
      stopping.set(true);
      fireServiceNotification(ServiceNotification.SERVICE_STOPPING);

      // Unregister Listeners for the service
      unregisterListeners();

      // Resume if paused. (This is required so that stop() doesn't hand and so
      // message aren't lost in SedaQueue when service is stopped.
      if (isPaused()) {
        resume();
      }

      doStop();

      // Stop component.  We do this here in case there are any queues that need to be consumed
      // first.
      component.stop();

      stopped.set(true);
      fireServiceNotification(ServiceNotification.SERVICE_STOPPED);
      logger.info("Mule Service " + name + " has been stopped successfully");
    }
  }
Exemple #11
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();
    }
  }
Exemple #12
0
 public void onMessage(Message message) {
   this.message = message;
   latch.countDown();
   try {
     released.whenTrue(null);
   } catch (InterruptedException e) {
     // ignored
   }
 }
Exemple #13
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));
  }
Exemple #14
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);
      }
    }
  }
Exemple #15
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());
 }
Exemple #16
0
 public void testSet() {
   assertTrue(TRUE.set(true));
   assertTrue(TRUE.set(false));
   assertFalse(TRUE.set(true));
   assertFalse(FALSE.set(false));
   assertFalse(FALSE.set(true));
   assertTrue(FALSE.set(true));
 }
Exemple #17
0
  public void stop() throws MuleException {
    if (!stopped.get()) {
      logger.debug("Stopping Service");
      stopping.set(true);
      fireServiceNotification(ServiceNotification.SERVICE_STOPPING);

      // Unregister Listeners for the service
      unregisterListeners();

      component.stop();

      doStop();
      stopped.set(true);
      fireServiceNotification(ServiceNotification.SERVICE_STOPPED);
      logger.info("Mule Service " + name + " has been stopped successfully");
    }
  }
Exemple #18
0
  /**
   * Starts a Mule Service.
   *
   * @param startPaused - Start service in a "paused" state (messages are received but not
   *     processed).
   */
  protected void start(boolean startPaused) throws MuleException {

    // Ensure Component is started. If component was configured with spring and
    // is therefore in the registry it will get started automatically, if it was
    // set on the service directly then it won't be started automatically. So to
    // be sure we start it here.
    component.start();

    // Create the receivers for the service but do not start them yet.
    registerListeners();

    // We connect the receivers _before_ starting the service because there may
    // be
    // some initialization required for the service which needs to have them
    // connected.
    // For example, the org.mule.transport.soap.glue.GlueMessageReceiver adds
    // InitialisationCallbacks within its doConnect() method (see MULE-804).
    connectListeners();

    // Start (and pause) the service.
    if (stopped.get()) {
      stopped.set(false);
      paused.set(false);
      doStart();
    }
    fireServiceNotification(ServiceNotification.SERVICE_STARTED);
    if (startPaused) {
      pause();
    }

    // We start the receivers _after_ starting the service because if a message
    // gets routed to the service before it is started,
    // org.mule.model.AbstractComponent.dispatchEvent() will throw a
    // ServiceException with message COMPONENT_X_IS_STOPPED (see MULE-526).
    startListeners();
  }
Exemple #19
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);
  }
Exemple #20
0
 public final boolean isConnected() {
   return connected.get();
 }
Exemple #21
0
 public void release() {
   released.set(true);
 }
Exemple #22
0
 public boolean isStopping() {
   return stopping.get();
 }
Exemple #23
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);
 }
Exemple #24
0
 /**
  * Resumes a single Mule Service that has been paused. If the service is not paused nothing is
  * executed.
  */
 public final void resume() throws MuleException {
   doResume();
   paused.set(false);
   fireServiceNotification(ServiceNotification.SERVICE_RESUMED);
   logger.info("Mule Service " + name + " has been resumed successfully");
 }
Exemple #25
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();
 }
Exemple #26
0
 /**
  * Pauses event processing for a single Mule Service. Unlike stop(), a paused service will still
  * consume messages from the underlying transport, but those messages will be queued until the
  * service is resumed.
  */
 public final void pause() throws MuleException {
   doPause();
   paused.set(true);
   fireServiceNotification(ServiceNotification.SERVICE_PAUSED);
   logger.info("Mule Service " + name + " has been paused successfully");
 }
Exemple #27
0
 public void testGet() {
   assertTrue(TRUE.get());
   assertFalse(FALSE.get());
 }
Exemple #28
0
 public void testToString() {
   assertEquals("true", TRUE.toString());
   assertEquals("false", FALSE.toString());
 }
Exemple #29
0
 public void testEqualsObject() {
   assertFalse(TRUE.equals(FALSE));
   assertFalse(FALSE.equals(TRUE));
   assertTrue(TRUE.equals(new WaitableBoolean(true)));
   assertTrue(FALSE.equals(new WaitableBoolean(false)));
 }
Exemple #30
0
 public boolean isStarted() {
   return !stopped.get();
 }