Concurrency with Akka

Concurrency with Akka

Introduction

Reading a CSV file containing 100 and 10 million lines and writing it to a database may seem like a straightforward task, but it’s not as easy as it looks. A simple Java program that starts a while loop until the end of file (EOF) and makes JDBC calls to store the value can take an hour to complete, and the program’s creation time is later than the actual completion time. This indicates that the task is not as simple as it appears.

Method 1: Simple Java Program

A simple Java program can be created to start a while loop until EOF and make JDBC calls to store the value. However, this approach has its limitations, as it can take a long time to complete and may not be efficient.

// Simple Java program
public class SimpleJavaProgram {
    public static void main(String[] args) {
        // Read CSV file
        while (/* EOF */) {
            // Make JDBC calls to store the value
        }
    }
}

Method 2: Threaded Java Program

Threaded programming can be used to improve the efficiency of the task. However, thread synchronization can be complicated, and the concept of “Mutex”, “Semaphores”, and “Monitors” can be intimidating. Java provides support for thread synchronization using Monitors, which allow multiple threads to share data processed independently without mutual interference.

// Threaded Java program
public class ThreadedJavaProgram {
    public static void main(String[] args) {
        // Create a thread pool
        ExecutorService executor = Executors.newFixedThreadPool(10);
        
        // Submit tasks to the thread pool
        for (int i = 0; i < 100000; i++) {
            executor.submit(() -> {
                // Make JDBC calls to store the value
            });
        }
    }
}

Method 3: Using Actors with Java

Performing tasks like reading a CSV file and writing it to a database can become complex and difficult to maintain. Java-generated threads can be limited, and error handling can be challenging. To address these issues, a framework like Akka can be used to provide a concurrent processing framework that allows for easy focus on business logic.

Actors

Akka is a Java framework that provides a concurrent processing framework based on the Erlang actor model. It allows for easy creation of concurrent tasks and provides a scalable and fault-tolerant architecture.

Introduction to Akka

Akka provides a simple and advanced concurrency and parallelism abstraction, allowing for asynchronous, non-blocking, and high-performance event-driven programming. It is very lightweight, with millions of actors per GB of heap memory.

Hello World Program with Akka

A simple “Hello World” program can be created using Akka. The program defines an actor, Greeter, which extends UntypedActor and provides a simple implementation of the onReceive method.

// Hello World program with Akka
public class Greeter extends UntypedActor {
    private String greeting = "";
    
    public void onReceive(Object message) {
        if (message instanceof WhoToGreet) {
            greeting = "hello" + ((WhoToGreet) message).who;
        } else if (message instanceof Greet) {
            // Send the current greeting back to the sender
            getSender().tell(new Greeting(greeting), getSelf());
        } else {
            unhandled(message);
        }
    }
}

Creating an Actor System

To create an actor system, an instance of ActorSystem is created, and an actor is created using the actorOf method.

// Creating an actor system
final ActorSystem system = ActorSystem.create("helloakka");
final ActorRef greeter = system.actorOf(Props.create(Greeter.class), "greeter");
greeter.tell(new WhoToGreet("akka"));

Conclusion

Akka provides a scalable and fault-tolerant architecture for concurrent processing, allowing for easy creation of concurrent tasks and providing a simple and advanced concurrency and parallelism abstraction. It is very lightweight, with millions of actors per GB of heap memory, and can be easily added to a project as a simple jar file dependency.