Well doesn't matter , reality is reality and Oracle has kind of "clearly" announced the conditions and statements. There should not be any surprises.
Let's do a short review of the one specific feature before we move to the Java 12 GA. I know there has been done already many blog posts around but we are fully migrating Robo4J to Java 11 and above. I got a feeling it does make a sense to show one on Robo4J project. Let's start.
Launch Single-File Source-Code Programs
As java has been for long time criticized for kind of obscurity that is necessary to run even a simple program. The JEP-330 serves a solution to this problem.
Instead of typing and running following lines inside the command line:
$ javac -classpath libs/*:. SingleFileRobo4j.java
$ java -classpath libs/*:. SingleFileRobo4j.java
$ java -classpath libs/*:. SingleFileRobo4j.java
You do things just simple a it should be
$ java -classpath libs/*:. SingleFileRobo4j.java
When such command gets executed the following output is printed out:
RoboSystem state Started
================================================
httpServer Started
...Press Key to stop
================================================
httpServer Started
...Press Key to stop
The ability of running a java single file is simply amazing! It allows you to prototype a robot by using only installed OpenJDK and a command line.
The Robo4J framework provides the ability to define and configure all units by using the descriptor file. See following example:
<robo4j>
<roboUnit id="httpServer">
<class>com.robo4j.socket.http.units.HttpServerUnit</class>
<config name="com.robo4j.root">
<value name="port" type="int">9022</value>
<value name="packages" type="String">com.robo4j.socket.http.codec</value>
</config>
</roboUnit>
</robo4j>
<roboUnit id="httpServer">
<class>com.robo4j.socket.http.units.HttpServerUnit</class>
<config name="com.robo4j.root">
<value name="port" type="int">9022</value>
<value name="packages" type="String">com.robo4j.socket.http.codec</value>
</config>
</roboUnit>
</robo4j>
The descriptor file does contain currently one unit. Having a descriptor XML file defined we need to write shore code to let it execute it by the Robo4J. Let's create a file SingleFileRobo4j.java with the following content:
import com.robo4j.RoboBuilder;
import com.robo4j.RoboContext;
import com.robo4j.util.SystemUtil;
import java.io.InputStream;
public class SingleFileRobo4j {
public static void main(String[] args) throws Exception {
InputStream contextIS = SingleFileRobo4j.class.getClassLoader().getResourceAsStream("robo4j.xml");
RoboContext system = new RoboBuilder().add(contextIS).build();
system.start();
System.out.println(SystemUtil.printStateReport(system));
System.out.println("...Press Key to stop");
System.in.read();
system.stop();
}
}
import com.robo4j.RoboContext;
import com.robo4j.util.SystemUtil;
import java.io.InputStream;
public class SingleFileRobo4j {
public static void main(String[] args) throws Exception {
InputStream contextIS = SingleFileRobo4j.class.getClassLoader().getResourceAsStream("robo4j.xml");
RoboContext system = new RoboBuilder().add(contextIS).build();
system.start();
System.out.println(SystemUtil.printStateReport(system));
System.out.println("...Press Key to stop");
System.in.read();
system.stop();
}
}
As you can see there is not much code. The Robo4J system gets the descriptor file, can be initiated and started. When the code gets executed the system provides a simple API. Such API includes the default access path shown by the example below :
GET request command: $ curl localhost:9022
result: [{"id":"ef514a70-3326-45a6-a1d2-469092563fc4","state":"STARTED"},{"id":"httpServer","state":"STARTED"}]
result: [{"id":"ef514a70-3326-45a6-a1d2-469092563fc4","state":"STARTED"},{"id":"httpServer","state":"STARTED"}]
Conclusion:
The ability of single-file source code programs execution makes possible to prototype the IoT system (robots) just using a command line. The reader may have noticed that the code is pretty straight forward. The important part is moved to the proper Robo4J XML Descriptor file definition. The Robo4J XML descriptor provides the links and configuration of all Robo4J Units that should be included.
The following example can be also used for running more complex systems just by using available Robo4J Unit for the Lego or RaspberryPi platforms.
Enjoy Coding !
No comments:
Post a Comment