Makers Blog

Streamlining Database Connectivity with Java and GraalVM Native Image

Bruno Caballero 12 March 2024 min. read
305 views 0 comments

Today's landscape of applications in the manufacturing industries are divided into a rigid hierarchy. Starting from Management using ERP systems to planning systems using MES to supervision systems using SCADA down to control systems realized with PLCs and field systems at the very bottom.

While most systems for management, planning and supervision are implemented in high-level object-oriented programming languages, control systems are implemented completely with low-level languages with C being the dominating programming language.

This abrupt change from a high-level to a low-level hierarchy level creates several problems. Completely different tools and ecosystems are necessary. For the implementation, completely different developers with a different way of thinking must be brought in to implement a task. Interfaces must be defined between the two worlds or systems. Only because there is no possibility to continue with a high-level programming language known from the supervision level on the control level. This has now changed. Using container technology on the control level in a PLC for example, we are able to use programming languages like Java on this hierarchy level and break the rigid hierarchies.

hierarchies-1.png

Java is a widely used, high-level, object-oriented programming language. It has a vibrant community and ecosystem of libraries and frameworks that provide additional functionality, allowing you to leverage existing tools and solutions to build your application more quickly and efficiently.

We will show how Java is available on a Phoenix Contact PLCnext to simplify development of an application that is running on PLCnext and directly communicates with a database to retrieve and store application relevant information and present them on a web-interface. One of the most popular frameworks in Java is Hibernate. It simplifies the process of mapping Java objects to relational database tables, reducing the amount of boilerplate code needed for database operations. Hibernate provides a high-level of abstraction, allowing developers to work with objects rather than SQL queries directly. This can lead to faster development cycles and reduced development effort. Hibernate abstracts away the differences between different database vendors, making it easier to switch between databases without significant code changes. This article describes the integration of Hibernate for connecting software running on PLCs to databases.

Modern Java application on PLCnext

In order to start implementing a modern Java application, we recommend using application frameworks like Quarkus or Spring Boot. These frameworks simplify the configuration of the project, the dependency management.

For this example we will start our application using the Quarkus' Using Hibernate ORM and Jakarta Persistence Guide. An example of the source code can be found on the github project hibernate-orm-quickstart.

We will start modifying the source code of the application. We can change the Java class Fruit.java into Event.java and add a new field date.

Example of code Event.java:

...

@Entity
@Table(name = "Events")
public class Event {
	@Id
	@GeneratedValue
	private Long id;
	
	private String title;
	
	@Column(name = "eventDate")
	private LocalDateTime date;
    
...

We can configure the database URL and the credentials modifying the file application.properties

Example of src/main/resources/application.properties

quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=myusername
quarkus.datasource.password=mypassword
quarkus.datasource.jdbc.url=jdbc:postgresql://myremotehost/mydatabase
quarkus.datasource.jdbc.max-size=8
quarkus.datasource.jdbc.min-size=2
quarkus.hibernate-orm.database.generation=create
...

When the application is started, a table in the database will be created and initialized if needed. After startup, we can interact with the database with a web interface running on port 8080 on the PLCnext:

screenshot-graalvm-ui.png

Keep in mind, this all happens on the control level of our hierarchy without any additional system in between.

GraalVM Native Image

Many developers still believe that Java requires a so-called Java Runtime Environment to execute Java Byte-Code and does not provide the necessary performance. This is no longer the case for embedded applications. In this example we use the GraalVM Native Image technology to compile Java code into a native application. GraalVM's native image compilation offers a compelling solution for addressing the challenges of startup time and memory consumption in Java applications, particularly in resource-constrained environments such as embedded systems. By compiling Java applications ahead-of-time into native executables, GraalVM significantly reduces startup time and memory overhead compared to traditional Just-In-Time (JIT) compilation.

MicroDoc's GraalVM has the unique capability to generate binaries for embedded devices. At the moment of writing this tutorial, GraalVM supports 32-bits and 64-bits ARM and x86 processors.

In order to compile our Java application ahead-of-time targeting the PLC AXC F 1152, we can use MicroDoc's GraalVM and configure the some compiler options in the file application.properties.

...
quarkus.native.additional-build-args=--target=linux-arm,-H:CompilerBackend=llvm,-H:+PreserveFramePointer,-Dllvm.bin.dir=/llvm/bin,--native-compiler-path=/arm-none-linux-gnueabihf-g++,-H:CAPCacheDir=/cap-cache/armv7a-linux-gnueabihf

You will find more information about the process of cross compiling and debugging in the following article: Building and debugging Java native images of MicroDoc GraalVM Embedded

In order to build the application, you can just use the following command:

$ mvn -Pnative package

Next, we start the PLCnext simulator. The simulator is provided as a qemu image. The simulator will greet us with the following line:

PLCnext Linux 2023.0 sim-axcf1152 ttyAMA0

sim-axcf1152 login:

The generated binaries can be copied to the PLC with the command scp which opens a secure shell to the PLC and transfers the binary to the target system. You can start the application using the following command:

$ ./application

Using the PLCnext cockpit client we can also take a look at the current memory and storage usage:

cockpit.png

However, we are also interested in the startup and memory consumption of our single application compared to running the application in a Java Runtime Environment like OpenJDK. Thus, we measured both values as shown in the following table:

Startup time (less is better), Memory usage (less is better)

GraalVM Native Image: 0.413 s, 87 MB

OpenJDK: 13 s, 128 MB

GraalVM Native Image offers significantly improved performance in terms of both startup time and memory usage compared to OpenJDK, demonstrating its effectiveness in optimizing resource consumption for efficient application deployment.

Summary

Using Java on the control level like a PLC has multiple benefits. First of all, there is no cut in technology between the supervision level and control level. The same workforce can implement applications and benefit from the Java ecosystem and the available tools. Second, the GraalVM is able to compile Java code as native applications on embedded devices like 32-bits and 64-bits ARM and x86 processors. Thus, your developers do not need to take care of the underlying architecture and can implement your application in a high-level object-oriented language. Third, thanks to the container support of Phoenix Contact's PLCnext, we have access to the interfaces of the PLC directly in the same hardware component and can break the rigid hierarchies in current manufacturing industries' application.

Note:

The Makers Blog shows applications and user stories of community members that are not tested or reviewed by Phoenix Contact. Use them at your own risk.

Discussion

Please login/register to comment

Login/Register

Leave a Reply

Newsletter
Never miss a new article
Sign up for the newsletter
Never miss news about PLCnext Technology
Get interesting content via newsletter four times a year
Receive exclusive information before all other users