4. Erlang integration

4.1 Introduction

There is an open source project called JInterface that provides a way for Java applications to communicate with an Erlang process. The API is very low level and rather tedious to use and throws checked exceptions. The Spring Erlang module makes accessing functions in Erlang from Java easy, often they can be one liners.

4.2 Communicating with Erlang processes

4.2.1 Executing RPC

The interface ErlangOperations is the high level API for interacting with an Erlang process.

public interface ErlangOperations {

    <T> T execute(ConnectionCallback<T> action) throws OtpException;

    OtpErlangObject executeErlangRpc(String module, String function, OtpErlangList args)
            throws OtpException;

    OtpErlangObject executeErlangRpc(String module, String function,
        OtpErlangObject... args) throws OtpException;

    OtpErlangObject executeRpc(String module, String function, Object... args)
            throws OtpException;

    Object executeAndConvertRpc(String module, String function,
            ErlangConverter converterToUse, Object... args) throws OtpException;

    // Sweet!
    Object executeAndConvertRpc(String module, String function, Object... args)
            throws OtpException;

}

The class that implements this interface is called ErlangTemplate. There are a few convenience methods, most notably executeAndConvertRpc, as well as the execute method which gives you access to the 'native' API of the JInterface project. For simple functions, you can invoke executeAndConvertRpc with the appropriate Erlang module name, function, and arguments in a one-liner. For example, here is the implementation of the RabbitBrokerAdmin method 'DeleteUser'

@ManagedOperation
public void deleteUser(String username) {
    erlangTemplate.executeAndConvertRpc(
        "rabbit_access_control", "delete_user", username.getBytes());
}

As the JInterface library uses specific classes such as OtpErlangDouble and OtpErlangString to represent the primitive types in Erlang RPC calls, there is a converter class that works in concert with ErlangTemplate that knows how to translate from Java primitive types to their Erlang class equivalents. You can also create custom converters and register them with the ErlangTemplate to handle more complex data format translations.

4.2.2 ErlangConverter

The ErlangConverter interface is shown below.

public interface ErlangConverter {

    /**
     * Convert a Java object to a Erlang data type.
     * @param object the object to convert
     * @return the Erlang data type
     * @throws ErlangConversionException in case of conversion failure
     */
    OtpErlangObject toErlang(Object object) throws ErlangConversionException;

    /**
     * Convert from a Erlang data type to a Java object.
     * @param erlangObject the Erlang object to convert
     * @return the converted Java object
     * @throws ErlangConversionException in case of conversion failure
     */
    Object fromErlang(OtpErlangObject erlangObject) throws ErlangConversionException;

    /**
     * The return value from executing the Erlang RPC.
     */
    Object fromErlangRpc(String module, String function, OtpErlangObject erlangObject)
        throws ErlangConversionException;
}

4.3 Exceptions

The JInterface checked exception hierarchy is translated into a parallel runtime exception hierarchy when executing operations through ErlangTemplate.