Java actions are deployed from JAR files containing application class files. External libraries can be used by bundling those dependencies into a fat JAR file. The JAR file must be less than the maximum action size of 48MB.
So, what if the application uses lots of external libraries and the JAR file is larger than 48MB? 🤔
Apache OpenWhisk’s support for custom Docker runtimes provides a workaround. In a previous blog post, we showed how this feature could be used with Python applications which rely on lots of external libraries.
Using the same approach with Java, a custom Java runtime can be created with additional libraries pre-installed. Those libraries do not need to be included in the application jar, which will just contain private class files. This should hopefully reduce the JAR file to under the action size limit.
Let’s walk through an example to show how this works….
Example Java Class using External Libraries
1 2 3 4 5 6 7 8 9 10 11
This example Java action capitalises sentences from the input event. It uses the Apache Commons Text library to handle capitialisation of input strings. This external library will be installed in the runtime, rather than bundled in the application JAR file.
Build Custom Java Runtime
- Clone the existing Apache OpenWhisk Java runtime repository.
- Edit the
core/java8/proxy/build.gradlefile and update the
dependenciesconfiguration with extra dependencies needed in the runtime.
1 2 3 4
com.google.code.gson:gson:2.6.2 is used by the runtime to handle JSON encoding/decoding. Do not remove this dependency.
- Execute the following command to build the custom Docker image.
Push Image To Docker Hub
If the build process succeeds, a local Docker image named
java8action should be available. This needs to be pushed to Docker Hub to allow Apache OpenWhisk to use it.
- Push the tagged custom image to Docker Hub.
Create OpenWhisk Action With Custom Runtime
- Compile the Java source file.
- Create the application JAR from the class file.
- Create the Java action with the custom runtime.
--main is the class file name containing the action handler in the JAR file.
--docker is the Docker image name for the custom runtime.
Test it out!
- Execute the
capitializeaction with input text to returned capitalised sentences.
If this works, the following JSON should be printed to the console.
1 2 3
The external library has been used in the application without including it in the application JAR file! 💯💯💯
Apache OpenWhisk supports running Java applications using fat JARs, which bundle application source code and external dependencies. JAR files cannot be more than 48MB, which can be challenging when applications uses lots of external libraries.
If application source files and external libraries result in JAR files larger than this limit, Apache OpenWhisk’s support for custom Docker runtimes provide a solution for running large Java applications on the platform.
By building a custom Java runtime, extra libraries can be pre-installed in the runtime. These dependencies do not need to be included in the application JAR file, which reduces the file size to under the action size limit. 👍