calling clojure from Java (Clojure Interop)

sqwale :

Calling Java from Clojoure is quite simple and straightforward but the inverse has proven to be unpredictable.

They seem to be two ways of doing it:

1)the following classes

      i) import clojure.java.api.Clojure; ,
     ii) import clojure.lang.IFn;

2)compile your clojure into an uberjar then import it into the java code.

I have opted for the 2nd option as it's more straight forward.

Here is the clojure code

(ns com.test.app.service
 (:gen-class
       :name com.test.app.service
       :main false
       :methods [^{:static true} [returned [int] int]]))

    (defn returned
      [number]
      (* 2 number))

    (defn -returned
      [number]
      (returned number))

Here is the Java code.

package com.s.profile;

import java.util.*;
import com.microsoft.azure.serverless.functions.annotation.*;
import com.microsoft.azure.serverless.functions.*;
import com.test.app.service;


/**
 * Azure Functions with HTTP Trigger.
 */
public class Function {
    /**
     * This function listens at endpoint "/api/hello". Two ways to invoke it using "curl" command in bash:
     * 1. curl -d "HTTP Body" {your host}/api/hello
     * 2. curl {your host}/api/hello?name=HTTP%20Query
     */
    @FunctionName("hello")
    public HttpResponseMessage<String> hello(
            @HttpTrigger(name = "req", methods = {"get", "post"}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");

        // Parse query parameter
        String query = request.getQueryParameters().get("name");
        String name = request.getBody().orElse(query);

        if (name == null) {
            return request.createResponse(400, "Please pass a name on the query string or in the request body");
        } else {
            service.returned(4);
            context.getLogger().info("process data" );
            return request.createResponse(200, "Hellos, " + name );
        }
    }
}

When ever I make the "service.returned(4);" the system never returns. I can't quite figure out why to me it comes off like the function doesn't return from Clojure but I can't see the cause.

Just to add some context I have tried it when its a simple hello world java app which just prints out the result and it works. It's when I try implement it in the Azure functions.

sqwale :

I followed these instructions and it seemed to resolve the error of class not found. It seems as though when running the command

mvn azure-functions:run

It doesn't automatically find all imported libraries. You either have to use

  1. maven-assembly-plugin
  2. maven-shade-plugin

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling Java from Clojure

From Java

Calling clojure from java

From Java

Calling java functions from Clojure

From Dev

Clojure custom Java Interop

From Java

Clojure and Java interop in a real world

From Dev

Practical Clojure: macros and Java interop

From Dev

NoClassDefFoundError calling Clojure from Java (Android, LibGDX)

From Java

Problems calling a variadic Java function from Clojure

From Dev

Calling a method on a Java class from Clojure

From Dev

Calling a Java static function from Clojure

From Dev

How to select data from Google BigQuery in Clojure via Java interop?

From Dev

No matching method with Clojure Java interop using reify

From Dev

Clojure: Java Interop IBM watson conversation service

From Dev

Clojure Java Interop of Enum Normalizer.Form

From Dev

Calling a Clojure Function from Haskell

From Dev

Calling C++ from clojure

From Dev

Calling Clojure from Java: Why is the "new" style (clojure.java.api.Clojure) better than the "old" one (gen-class)?

From Java

Calling a very simple clojure function from Java does not work

From Dev

Problems calling a clojure function that takes a map as parameter from java

From Dev

Calling a java function with an optionally null argument from clojure

From Java

clojure/scala interop?

From Dev

Calling a private macro from another namespace in Clojure?

From Dev

Clojure: calling a sequence of methods on a Java object

From Java

Clojure macro for calling Java setters based on a map?

From Dev

Calling java non-static method in Clojure

From Java

How to access static inner Java class via Clojure interop?

From Java

Passing Args to Clojure from Java

From Dev

Clojure interop for Scala objects and traits

From Dev

Could not locate proj/core.clj on classpath when calling clojure from java

Related Related

  1. 1

    Calling Java from Clojure

  2. 2

    Calling clojure from java

  3. 3

    Calling java functions from Clojure

  4. 4

    Clojure custom Java Interop

  5. 5

    Clojure and Java interop in a real world

  6. 6

    Practical Clojure: macros and Java interop

  7. 7

    NoClassDefFoundError calling Clojure from Java (Android, LibGDX)

  8. 8

    Problems calling a variadic Java function from Clojure

  9. 9

    Calling a method on a Java class from Clojure

  10. 10

    Calling a Java static function from Clojure

  11. 11

    How to select data from Google BigQuery in Clojure via Java interop?

  12. 12

    No matching method with Clojure Java interop using reify

  13. 13

    Clojure: Java Interop IBM watson conversation service

  14. 14

    Clojure Java Interop of Enum Normalizer.Form

  15. 15

    Calling a Clojure Function from Haskell

  16. 16

    Calling C++ from clojure

  17. 17

    Calling Clojure from Java: Why is the "new" style (clojure.java.api.Clojure) better than the "old" one (gen-class)?

  18. 18

    Calling a very simple clojure function from Java does not work

  19. 19

    Problems calling a clojure function that takes a map as parameter from java

  20. 20

    Calling a java function with an optionally null argument from clojure

  21. 21

    clojure/scala interop?

  22. 22

    Calling a private macro from another namespace in Clojure?

  23. 23

    Clojure: calling a sequence of methods on a Java object

  24. 24

    Clojure macro for calling Java setters based on a map?

  25. 25

    Calling java non-static method in Clojure

  26. 26

    How to access static inner Java class via Clojure interop?

  27. 27

    Passing Args to Clojure from Java

  28. 28

    Clojure interop for Scala objects and traits

  29. 29

    Could not locate proj/core.clj on classpath when calling clojure from java

HotTag

Archive