Source

Core:

The library is a complement to the java.util.concurrent package introduced in 1.5 and should be used for message based concurrency similar to event based actors in Scala.

The library does not provide remote messaging capabilities. It is designed specifically for high performance in-memory messaging. Features

Examples

Example

Fiber fiber = new ThreadFiber();
fiber.start();
final CountDownLatch latch = new CountDownLatch(2);
Runnable toRun = new Runnable(){
    public void run(){
        latch.countDown();
    }
};
//enqueue runnable for execution
fiber.execute(toRun);
//repeat to trigger latch a 2nd time
fiber.execute(toRun);
latch.await(10, TimeUnit.SECONDS);
//shutdown thread
fiber.dispose();

Channel Example

// start thread backed receiver. 
// Lighweight fibers can also be created using a thread pool
Fiber receiver = new ThreadFiber();
receiver.start();

// create java.util.concurrent.CountDownLatch to notify when message arrives
final CountDownLatch latch = new CountDownLatch(1);

// create channel to message between threads
Channel<String> channel = new MemoryChannel<String>();

Callback<String> onMsg = new Callback<String>() {
    public void onMessage(String message) {
        //open latch
        latch.countDown();
    }
};
//add subscription for message on receiver thread
channel.subscribe(receiver, onMsg);

//publish message to receive thread. the publish method is thread safe.
channel.publish("Hello");

//wait for receiving thread to receive message
latch.await(10, TimeUnit.SECONDS);

//shutdown thread
receiver.dispose();

Remoting: async distributed messaging


Tags: library   jvm   actors   distribution   message-passing  

Last modified 28 July 2021