Skip to content

Creating a Bialetti Application

Alessandro Salerno edited this page Aug 24, 2022 · 1 revision

Bialetti provides an abstraction layer to build applications. Bialetti Applications are called Runnable Services and are intended to be used within the library, but can Las be used by the user.

The BialettiManagedService

A BialettiManagedService handles methods in a way that allows you to run code at startup, shut down and during the execution. To mark a method as an "on-startup method", you can use the @BialettiInitMethod annotation.

// Runs when the service is started
@BialettiInitMethod
public void onStart() {
    System.out.println("Service started");
}

To mark a method as an "on-shutdown method", you can use the @BialettiEndMethod annotation.

// Runs when the service is shut down
@BialettiEndMethod
public void onShutdown() {
    System.out.println("Service shut down");
}

To mark a method for continuous execution on a separate thread, you can used the @BialettiHandleMethod annotation.

// Runs forever until the service is stopped
@BialettiHandleMethod
public void handle() {
    BufferedReader mr = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(mr.readline());
}

To mark a method as an exception handler, you can use the @BialettiExceptionHandlerMethod annotation. Exception handler methods MUST follow a naming convention: onExceptionName(ExceptionName exceptionName). Exception handler methods can only handle exceptions thrown by init/handle/shutdown methods that have a throws clause. If no method is defined for a particular exception type, then the default onThrowable(Throwable throwable) method will be called (This method can also be overridden).

@BialettiHandleMethod
public void doSomething() throws Exception {
    // Does something that may throw an exception
}

// Runs when a SocketException is thrown
@BialettiExceptionHandlerMethod
public void onSocketException(SocketException se) {
    System.out.println("Socket Exception Raised: " + se.getMessage());
}

// Runs when an IllegalArgumentException is thrown
@BialettiExceptionHandlerMethod
public void onIllegalArgumentException(IllegalArgumentException iae) {
    System.out.println("Illegal Argument Exception Raised: " + iae.getMessage());
}

To use these features, you're going to need a class that extends BialettiManagedService.

public class MyManagedService extends BialettiManagedService {
    ...
}

You're also going to need an instance of the class.

public class Main {
    public static void main(String[] args) {
        MyManagedService s = new MyManagedService();
        s.run(); // Sets up the service and runs all init methods
    }
}

The BialettiRunnableService

The main issue with a BialettiManagedService is that it has no way of preventing the program from shutting down: if the main thread exits, the service is terminated. A BialettiRunnableService solves this by creating a dummy thread that runs forever. To use a BialettiRunnableService just create a class that extends it, everything else is the same as a BialettiManagedService.

public class MyRunnableService extends BialettiRunnableService {
    ...
}