Sidebar

Can someone provide me an example of JAVA being used in the QIE interpreter for message processing as apposed to the JS?

+1 vote
223 views
asked Jul 13, 2022 by william-g-6852 (130 points)
As the question states, I was told it was possible to code in JAVA on the QIE platform, some example scripts would be interesting to see.

1 Answer

+2 votes

QIE uses the Mozilla RHINO javascript engine to execute javascript from inside of the Java Virtual Machine (JVM).  Because it is running inside of the JVM, we have the ability to interact with Java libraries and classes.  While you can't just write java code, you can new up java objects, and then call methods on those objects.

A simple example of this, would be to use a java StringBuilder.class object to create a large string.  The javadoc for StringBuilder can be found here.  An example implementation of using this java class is:

var sb = new java.lang.StringBuilder();
for (var i=0; i < 10; i++) {
   sb.append("SomeData").append(i);
}
qie.debug(sb.toString());

In the above example, we are still using the javascript syntax, but we start out with a variable that creates a new java.lang.StringBuilder() object, this is the java object itself that is stored in the javascript variable sb.  We then call the available methods on the StringBuilder, in the above example, .append(), and .toString().  You can call any available method found in the java documents.

A more complex example would be to use a 3rd party library.  In this example, I will use the Apache Kafka libraries to send a message to a kafka queue.  You would need to download the libraries, and load them into QIE using the instructions found here.  Once you have loaded the libraries and restarted the service, you can call a Kafka queue as follows:

var properties = new java.util.Properties();
properties.put("bootstrap.servers", "10.97.10.56:9092");
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

var kafkaProducer = new org.apache.kafka.clients.producer.KafkaProducer(properties);
try {
   kafkaProducer.send(new org.apache.kafka.clients.producer.ProducerRecord("dev-test", i + "", "Test Message - " + qie.getUUID(true)));
} finally {
   kafkaProducer.close();
}

In the above example, we new up a java.util.Properties object, and then set the parameters as per the Kafka documentation.  We then create a new KafkaProducer object passing in the properties, finally, we send a message to the "dev-test" queue using the KafkaProducer.send() method.  This method requires a ProducerRecord as it's parameter so we new one of those up and set the queue and the message data.

If you added the following line to the top of the script:

importPackage(Packages.org.apache.kafka.clients.producer);

You could then change both of the 'new' commands to just the object like this:

var kafkaProducer = new KafkaProducer(properties);
.....
kafkaProducer.send(new ProducerRecord("dev-test", i + "", "Test Message - " + qie.getUUID(true)));

So, as you can see, the scripting is still javascript, but using the RHINO engine we are able to interact with the java libraries and leverage them to complete any task that needs to be completed.

answered Jul 13, 2022 by ben-s-7515 (12,320 points)
edited Jul 13, 2022 by nathan-c-4426
...