Clojure java class
Why? Since clojure can call java classes, but sometimes these java classes require you to implement a subclass or pass a custom java object as a parameter, You need to compile clojure code into java code.
Do you still remember the gen-class mentioned earlier? In (ns ...), use (: gen-class), in (ns ..) (gen-class)
The following is an example. The content of the file MoveDailyAction. clj is as follows:
(ns kafka2hdfs.MoveDailyAction (:import [org.apache.hadoop.fs FileSystem Path] [java.io.IOException] [java.text DateFormat SimpleDateFormat] [java.util.Date]))(gen-class :name kafka2hdfs.MoveDailyAction :implements [org.apache.storm.hdfs.common.rotation.RotationAction] :state dest :init init :constructors {[String][]})(defn -init [dest] [[] dest])
This class is called MoveDailyAction and implements the RotationAction interface. There is an immutable public member variable dest, and an init member function will be called within the constructor. The constructor does not need to be implemented. You only need to declare the parameter form.
To automatically load and compile the class when running lein repl in the lein project. in clj: Add in aot. Note core. clj depends on MoveDailyAction. class, so you need to put MoveDailyAction in the front to compile
:aot [kafka2hdfs.MoveDailyAction kafka2hdfs.core]
Here we can see that the use of aot is to compile clojure code into a java class in advance and then use it elsewhere.
Run the following code test in repl:
kafka2hdfs.core=> (def o (kafka2hdfs.MoveDailyAction. ab))#'kafka2hdfs.core/okafka2hdfs.core=> (.dest o)ab
Parameters accepted by the constructor can be read later.