Kumar
Kumar

Reputation: 961

not able to get result json from ibm cloud function action through docker java run time

I am trying to run Java standalone app as docker image in IBM Cloud function.

Java Code :

import com.google.gson.JsonObject;

public class Hello {

    public static JsonObject main(JsonObject args) {
        String name = "stranger";
        if (args.has("name"))
            name = args.getAsJsonPrimitive("name").getAsString();
        JsonObject response = new JsonObject();
        response.addProperty("greeting", "Hello, " + name + "!");
        return response;
    }

    public static void main(String...strings) {
        String name = "stranger";
         JsonObject response = new JsonObject();
         if(strings.length > 0)
             name = strings[0];
         response.addProperty("greeting", "Hello, " + name + "!");
         System.out.println(response);
    }
}

POM.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.qs</groupId>
    <artifactId>qs-action</artifactId>
    <version>1.0.0</version>
    <name>java-action</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

    </dependencies>

    <!-- <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> -->
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.qs.Hello</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Docker File :

FROM java:8
#ADD ./target/qs-action-1.0.0.jar /qs-action-1.0.0.jar
ADD ./target/qs-action-1.0.0-jar-with-dependencies.jar /qs-action-1.0.0-jar-with-dependencies.jar
ADD ./run.sh /run.sh
RUN chmod a+x /run.sh
CMD /run.sh

run.sh:

java -jar /qs-action-1.0.0-jar-with-dependencies.jar

I am able to build and compile the project in a single executable jar. Now after this, I use to build docker image through command

docker build -t dockerrepo/imagename:tag .

after building image and running through docker run imageid, the result is displayed of java main method. like : {"greeting":"Hello, stranger!"}. After pushing image to docker hub, I am creating a cloud function : ibmcloud fn create action jaction --docker docker-repo/image:tag --main com.qs.Hello and thus ibm action created. After this, I am trying to invoke it through UI and command line as well but error thrown out.

ok: invoked /_/jaction, but the request has not yet finished, with id XXXXXXXX.

When I run through IBM cloud action UI, the main method response displays in log but does not return any json response string.

Could any one of you please help me ou to resolve this.

Upvotes: 0

Views: 176

Answers (1)

data_henrik
data_henrik

Reputation: 17118

It seems that you are mixing Java-based actions with Docker-based actions. Both are deployed through the CLI, see the CLI reference for creating an action for the details on options.

When you are deploying a Java action, then you need to identify the method name. When it is a Docker image, you need to specify the application or script:

--main ENTRY_METHOD_NAME
If the action's entry method is not main, specify the custom name. This flag is required when the entry method is not main. For some runtimes, such as Java, the name must be the fully qualified method.

In your provided details, it seems that you specified the method, but needed to point to the script run.sh.

Upvotes: 0

Related Questions