Class BaseService

java.lang.Object
org.apache.camel.support.service.BaseService
Direct Known Subclasses:
ServiceSupport

public abstract class BaseService extends Object
A useful base class which ensures that a service is only initialized once and provides some helper methods for enquiring of its status.

Implementations can extend this base class and implement SuspendableService in case they support suspend/resume.

Important: You should override the lifecycle methods that start with do, eg doStart()}, doStop(), etc. where you implement your logic. The methods start(), stop() should NOT be overridden as they are used internally to keep track of the state of this service and properly invoke the operation in a safe manner.

Since:
3.2
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final byte
     
    protected static final byte
     
    protected static final byte
     
    protected static final byte
     
    protected final Lock
     
    protected static final byte
     
    protected static final byte
     
    protected static final byte
     
    protected static final byte
     
    protected static final byte
     
    protected byte
     
    protected static final byte
     
    protected static final byte
     
    protected static final byte
     
    protected static final byte
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Builds the service.
    protected void
    Optional build phase of the service.
    protected void
    Implementations override this method to perform any action upon failure.
    protected void
    Initialize the service.
    protected @Nullable AutoCloseable
    Implementations may return an object that will be closed when the lifecycle action is completed.
    protected void
    Implementations override this method to support customized suspend/resume.
    protected void
    Implementations override this method to perform customized shutdown.
    protected void
    Implementations override this method to support customized start/stop.
    protected void
    Implementations override this method to support customized start/stop.
    protected void
    Implementations override this method to support customized suspend/resume.
    protected void
     
    protected Lock
     
    Returns the current status of the service.
    void
    Initializes the service.
    boolean
    Returns true if the service is in the BUILT state.
    boolean
    Returns true if the service is in the INITIALIZED state.
    boolean
    Returns true if the service is in the NEW state (not yet built).
    boolean
    Returns true if the service is allowed to process exchanges (started, starting, or suspended).
    boolean
    Returns true if the service is in the SHUTDOWN state.
    boolean
    Returns true if the service is in the STARTED state.
    boolean
    Returns true if the service is in the STARTING state.
    boolean
    Is the service in progress of being suspended or already suspended
    boolean
    Returns true if the service is stopped (not yet started or already stopped).
    boolean
    Returns true if the service is in the STOPPING state.
    boolean
    Is the service in progress of being stopped or already stopped
    boolean
    Returns true if the service is in the SUSPENDED state.
    boolean
    Returns true if the service is in the SUSPENDING state.
    boolean
    Is the service in progress of being suspended or already suspended
    void
    Important: You should override the lifecycle methods that start with do, eg doStart(), doStop(), etc. where you implement your logic.
    void
    Important: You should override the lifecycle methods that start with do, eg doStart(), doStop(), etc. where you implement your logic.
    void
    Important: You should override the lifecycle methods that start with do, eg doStart(), doStop(), etc. where you implement your logic.
    void
    Important: You should override the lifecycle methods that start with do, eg doStart(), doStop(), etc. where you implement your logic.
    void
    Important: You should override the lifecycle methods that start with do, eg doStart(), doStop(), etc. where you implement your logic.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • BaseService

      public BaseService()
  • Method Details

    • build

      public void build()
      Builds the service. This method will only be called once and transitions the service from NEW to BUILT state.
    • init

      public void init()
      Initializes the service. This method will only be called once before starting.
    • start

      public void start()
      Important: You should override the lifecycle methods that start with do, eg doStart(), doStop(), etc. where you implement your logic. The methods start(), stop() should NOT be overridden as they are used internally to keep track of the state of this service and properly invoke the operation in a safe manner.
    • stop

      public void stop()
      Important: You should override the lifecycle methods that start with do, eg doStart(), doStop(), etc. where you implement your logic. The methods start(), stop() should NOT be overridden as they are used internally to keep track of the state of this service and properly invoke the operation in a safe manner.
    • suspend

      public void suspend()
      Important: You should override the lifecycle methods that start with do, eg doStart(), doStop(), etc. where you implement your logic. The methods start(), stop() should NOT be overridden as they are used internally to keep track of the state of this service and properly invoke the operation in a safe manner.
    • resume

      public void resume()
      Important: You should override the lifecycle methods that start with do, eg doStart(), doStop(), etc. where you implement your logic. The methods start(), stop() should NOT be overridden as they are used internally to keep track of the state of this service and properly invoke the operation in a safe manner.
    • shutdown

      public void shutdown()
      Important: You should override the lifecycle methods that start with do, eg doStart(), doStop(), etc. where you implement your logic. The methods start(), stop() should NOT be overridden as they are used internally to keep track of the state of this service and properly invoke the operation in a safe manner.
    • getStatus

      public ServiceStatus getStatus()
      Returns the current status of the service.
    • isNew

      public boolean isNew()
      Returns true if the service is in the NEW state (not yet built).
    • isBuild

      public boolean isBuild()
      Returns true if the service is in the BUILT state.
    • isInit

      public boolean isInit()
      Returns true if the service is in the INITIALIZED state.
    • isStarted

      public boolean isStarted()
      Returns true if the service is in the STARTED state.
    • isStarting

      public boolean isStarting()
      Returns true if the service is in the STARTING state.
    • isStopping

      public boolean isStopping()
      Returns true if the service is in the STOPPING state.
    • isStopped

      public boolean isStopped()
      Returns true if the service is stopped (not yet started or already stopped).
    • isSuspending

      public boolean isSuspending()
      Returns true if the service is in the SUSPENDING state.
    • isSuspended

      public boolean isSuspended()
      Returns true if the service is in the SUSPENDED state.
    • isRunAllowed

      public boolean isRunAllowed()
      Returns true if the service is allowed to process exchanges (started, starting, or suspended).
    • isShutdown

      public boolean isShutdown()
      Returns true if the service is in the SHUTDOWN state.
    • isStoppingOrStopped

      public boolean isStoppingOrStopped()
      Is the service in progress of being stopped or already stopped
    • isSuspendingOrSuspended

      public boolean isSuspendingOrSuspended()
      Is the service in progress of being suspended or already suspended
    • isStartingOrStarted

      public boolean isStartingOrStarted()
      Is the service in progress of being suspended or already suspended
    • fail

      protected void fail(Exception e)
    • doBuild

      protected void doBuild() throws Exception
      Optional build phase of the service. This method will only be called by frameworks which supports pre-building projects such as camel-quarkus.
      Throws:
      Exception
    • doInit

      protected void doInit() throws Exception
      Initialize the service. This method will only be called once before starting.
      Throws:
      Exception
    • doStart

      protected void doStart() throws Exception
      Implementations override this method to support customized start/stop.

      Important: See doStop() for more details.

      Throws:
      Exception
      See Also:
    • doStop

      protected void doStop() throws Exception
      Implementations override this method to support customized start/stop.

      Important: Camel will invoke this doStop() method when the service is being stopped. This method will also be invoked if the service is still in uninitialized state (eg has not been started). The method is always called to allow the service to do custom logic when the service is being stopped, such as when CamelContext is shutting down.

      Throws:
      Exception
      See Also:
    • doSuspend

      protected void doSuspend() throws Exception
      Implementations override this method to support customized suspend/resume.
      Throws:
      Exception
    • doResume

      protected void doResume() throws Exception
      Implementations override this method to support customized suspend/resume.
      Throws:
      Exception
    • doShutdown

      protected void doShutdown() throws Exception
      Implementations override this method to perform customized shutdown.
      Throws:
      Exception
    • doFail

      protected void doFail(Exception e)
      Implementations override this method to perform any action upon failure.
    • doLifecycleChange

      protected @Nullable AutoCloseable doLifecycleChange()
      Implementations may return an object that will be closed when the lifecycle action is completed.
    • getInternalLock

      protected Lock getInternalLock()