Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (69 sloc) 1.97 KB

Example - Custom Stage

Stage Class:

package examples;

import com.mackenziehigh.cascade.Cascade;

 * This class demonstrates how easy one can create a custom Stage.
 * However, in reality, creating a thread per message would usually
 * be quite inefficient due to excessive thread-spawning, etc.
public final class HeavyStage
        extends Cascade.AbstractStage
    protected void onSubmit (final DefaultActor<?, ?> state)
        // The onSubmit() method is only called when
        // the ActorTask is safe to execute; therefore,
        // we can just spawn a thread immediately.
        // No need to worry about synchronization at all.
        final Thread thread = new Thread(state);

    protected void onClose ()
        // In this implementation, this method is unneeded.
        // In other implementations, such as those that have
        // a shared ExecutorService, this is where you would
        // place the logic needed to shut the service down.

Main Class:

package examples;

import com.mackenziehigh.cascade.Cascade.Stage;
import com.mackenziehigh.cascade.Cascade.Stage.Actor;

public final class Example
    public static void main (String[] args)
        final Example main = new Example();

    private void demo ()
        // Create a custom stage.
        final Stage stage = new HeavyStage();

        // Create an actor.
        final Actor<String, String> actor = stage
                .withConsumerScript((String msg) -> System.out.println("Welcome to " + msg + "!"))

        // Send messages to the actor.

Example Output:

Welcome to Mercury!
Welcome to Venus!
Welcome to Earth!
Welcome to Mars!
You can’t perform that action at this time.