apache / openwhisk

Apache OpenWhisk is an open source serverless cloud platform
https://openwhisk.apache.org/
Apache License 2.0
6.54k stars 1.17k forks source link

Write documentation for Java actions #1104

Closed glikson closed 7 years ago

glikson commented 8 years ago

It seems that static fields in Java actions are initialized only on first invocation of an action.

import com.google.gson.JsonObject;
public class Static {
    static int field = 0;
    public static JsonObject main(JsonObject args) {
        JsonObject response = new JsonObject();
        response.addProperty("value", field++);
        return response;
    }
}
$ jar cf static.jar Static.class 
$ wsk action create static static.jar 
ok: created action static
$ wsk action invoke static --blocking --result
{
    "value": 0
}
$ wsk action invoke static --blocking --result
{
    "value": 1
}
$ wsk action invoke static --blocking --result
{
    "value": 2
}

Not entirely sure whether this is a bug or a feature (that should be properly documented), and whether it is environment-dependent.

rabbah commented 8 years ago

This is working as designed. The static field is "state" and if your action ends up reusing a container, state from previous invocations carries over to newer ones. You should not count on the container reuse but you can check for it (i.e. using a static). If you don't want this behavior then all static fields should be final and immutable.

glikson commented 8 years ago

OK, thanks. Sounds interesting. Is it documented somewhere?

rabbah commented 8 years ago

not yet.

sjfink commented 8 years ago

Changing title to reflect the actionable work: Document Java

SyCode7 commented 8 years ago

Is there a basic example for deploying a java application somewhere yet ?

glikson commented 8 years ago

https://github.com/openwhisk/openwhisk/blob/master/docs/actions.md#creating-java-actions

rabbah commented 7 years ago

@psuter has a recent blog post as well: https://psuter.net/2017/01/20/openwhisk-java-gradle