Replacing Executors by Actors

Disclaimer: I am talking about ActiveMQ Artemis internals here, giving tips on how you could achieve something similar on any system.

Systems today make heavy usage of Executors. No matter what system you chose, they are always there... Executors everywhere.

One pitfall they usually bring is: you will have to create one Runnable every time you call the executor:

Executor executor; // pretend this is already initialized

String data = "Hello world";
executor.execute(new Runnable() {
    System.out.println(data);
});

If you call this millions of time, you will have lots of Runnable instantiated that will need to be cleared by the Garbage Collector. (poor guy)

To make it easier, we haven recently introduce a new type of Executor on ActiveMQ Artemis internals. With the usage of lambdas it gets really elegant:

Executor parentExecutor; // pretend this is already initialized
Actor<String> actor = new Actor<>(parentExecutor, ::onMessage);

// this will call onMessage..
// an executor will be used underneath but no new Runnables are created
actor.act("Hello world");

public void onMessage(String message) {
   System.out.println(message);
}

Look at the code yourself if you want to have a similar pattern on your system:

Our commons packet

Code in use


To view or add a comment, sign in

More articles by Clebert Suconic

  • Vote for the new ActiveMQ Logo

    https://goo.gl/forms/u5IPPfaQYnOo29g42

  • Apache ActiveMQ Internals

    ActiveMQ Artemis is not just a Messaging broker. With its paging structure it can scale to infinite queues (up of…

  • Apache ActiveMQ Call For Logo

    The Apache ActiveMQ logo was created a few years ago. While the project continues to evolve, the logo needs to be…

  • Artemis 1.0.0 released

    The HornetQ code base was donated to the Apache ActiveMQ community late last year and now resides as a sub project…

Explore content categories