项目作者: apache

项目描述 :
Apache OpenWhisk Runtime Java supports Apache OpenWhisk functions written in Java and other JVM-hosted languages
高级语言: Scala
项目地址: git://github.com/apache/openwhisk-runtime-java.git
创建时间: 2017-09-27T04:55:50Z
项目社区:https://github.com/apache/openwhisk-runtime-java

开源协议:Apache License 2.0

下载


Apache OpenWhisk runtimes for java

License
Continuous Integration

Changelogs

Quick Java Action

A Java action is a Java program with a method called main that has the exact signature as follows:

  1. public static com.google.gson.JsonObject main(com.google.gson.JsonObject);

For example, create a Java file called Hello.java with the following content:

  1. import com.google.gson.JsonObject;
  2. public class Hello {
  3. public static JsonObject main(JsonObject args) {
  4. String name = "stranger";
  5. if (args.has("name"))
  6. name = args.getAsJsonPrimitive("name").getAsString();
  7. JsonObject response = new JsonObject();
  8. response.addProperty("greeting", "Hello " + name + "!");
  9. return response;
  10. }
  11. }

In order to compile, test and archive Java files, you must have a JDK 8 installed locally.

Then, compile Hello.java into a JAR file hello.jar as follows:

  1. javac Hello.java
  1. jar cvf hello.jar Hello.class

Note: google-gson must exist in your Java CLASSPATH when compiling the Java file.

You need to specify the name of the main class using --main. An eligible main
class is one that implements a static main method as described above. If the
class is not in the default package, use the Java fully-qualified class name,
e.g., --main com.example.MyMain.

If needed you can also customize the method name of your Java action. This
can be done by specifying the Java fully-qualified method name of your action,
e.q., --main com.example.MyMain#methodName

Not only support return JsonObject but also support return JsonArray, the main function would be:

  1. import com.google.gson.JsonArray;
  2. import com.google.gson.JsonObject;
  3. public class HelloArray {
  4. public static JsonArray main(JsonObject args) {
  5. JsonArray jsonArray = new JsonArray();
  6. jsonArray.add("a");
  7. jsonArray.add("b");
  8. return jsonArray;
  9. }
  10. }

And support array result for sequence action as well, the first action’s array result can be used as next action’s input parameter.

So the function would be:

  1. import com.google.gson.JsonArray;
  2. public class Sort {
  3. public static JsonArray main(JsonArray args) {
  4. return args;
  5. }
  6. }

Create the Java Action

To use as a docker action:

  1. wsk action update helloJava hello.jar --main Hello --docker openwhisk/java8action

This works on any deployment of Apache OpenWhisk

To use on a deployment of OpenWhisk that contains the runtime as a kind:

  1. wsk action update helloJava hello.jar --main Hello --kind java:8

Invoke the Java Action

Action invocation is the same for Java actions as it is for Swift and JavaScript actions:

  1. wsk action invoke --result helloJava --param name World
  1. {
  2. "greeting": "Hello World!"
  3. }

Local development

Pre-requisites

  • Gradle
  • Docker Desktop (local builds)

Build and Push image to a local Docker registry

  1. Start Docker Desktop (i.e., Docker daemon)

  2. Build the Docker runtime image locally using Gradle:

    1. ./gradlew core:java8:distDocker

    This will produce the image whisk/java8action and push it to the local Docker Desktop registry with the latest tag.

  3. Verify the image was registered:

    1. $ docker images whisk/*
    2. REPOSITORY TAG IMAGE ID CREATED SIZE
    3. whisk/java8action latest 35f90453905a 7 minutes ago 521MB

Build and Push image to a remote Docker registry

Build the Docker runtime image locally using Gradle supplying the image Prefix and Registry domain (default port):

  1. docker login
  2. ./gradlew core:java8:distDocker -PdockerImagePrefix=$prefix-user -PdockerRegistry=docker.io

Deploying the Java runtime image to OpenWhisk

Deploy OpenWhisk using ansible environment that contains the kind java:8
Assuming you have OpenWhisk already deployed locally and OPENWHISK_HOME pointing to root directory of OpenWhisk core repository.

Set ROOTDIR to the root directory of this repository.

Redeploy OpenWhisk

  1. cd $OPENWHISK_HOME/ansible
  2. ANSIBLE_CMD="ansible-playbook -i ${ROOTDIR}/ansible/environments/local"
  3. $ANSIBLE_CMD setup.yml
  4. $ANSIBLE_CMD couchdb.yml
  5. $ANSIBLE_CMD initdb.yml
  6. $ANSIBLE_CMD wipe.yml
  7. $ANSIBLE_CMD openwhisk.yml

Or you can use wskdev and create a soft link to the target ansible environment, for example:

  1. ln -s ${ROOTDIR}/ansible/environments/local ${OPENWHISK_HOME}/ansible/environments/local-java
  2. wskdev fresh -t local-java

Testing

Install dependencies from the root directory on $OPENWHISK_HOME repository

  1. pushd $OPENWHISK_HOME
  2. ./gradlew install
  3. popd $OPENWHISK_HOME

Using gradle to run all tests

  1. ./gradlew :tests:test

Using gradle to run some tests

  1. ./gradlew :tests:test --tests *ActionContainerTests*

Using IntelliJ:

  • Import project as gradle project.
  • Make sure working directory is root of the project/repo

Using container image to test

To use as docker action push to your own dockerhub account

  1. docker tag whisk/java8action $user_prefix/java8action
  2. docker push $user_prefix/java8action

Then create the action using your the image from dockerhub

  1. wsk action update helloJava hello.jar --main Hello --docker $user_prefix/java8action

The $user_prefix is usually your dockerhub user id.


Troubleshooting

Gradle build fails with “Too many files open”

This may occur on MacOS as the default maximum # of file handles per session is 256. The gradle build requires many more and is unable to open more files (e.g., java.io.FileNotFoundException). For example, you may see something like:

  1. > java.io.FileNotFoundException: /Users/XXX/.gradle/caches/4.6/scripts-remapped/build_4mpzm2wl8gipqoxzlms7n6ctq/7gdodk7z6t5iivcgfvflmhqsm/cp_projdf5583fde4f7f1f2f3f5ea117e2cdff1/cache.properties (Too many open files)

You can see this limit by issuing:

  1. $ ulimit -a
  2. open files (-n) 256

In order to increase the limit, open a new terminal session and issue the command (and verify):

  1. $ ulimit -n 10000
  2. $ ulimit -a
  3. open files (-n) 10000

Gradle Task fails on :core:java8:tagImage

Docker daemon is not started and the Task is not able to push the image to your local registry.

License

Apache 2.0