Channels ▼
RSS

Open Source

NetRexx: The Original JVM Scripting Language Returns


NetRexx is an alternative language for the Java Virtual Machine (JVM). One of its most compelling benefits is its complete, syntax-less integration with JVM libraries. Whereas other JVM languages depend on new libraries to be written — with all the related problems like inventing new conventions — NetRexx integrates seamlessly with Java.

The NetRexx language processor can function as a pure scripting language and interpret source code, or it can compile to Java class files. In the compiled version, the right invocation is generated and there is no difference between using a NetRexx class or a Java class. NetRexx takes this integration to the extreme, by depending wholly on the JVM for I/O and other functions.

The benefit of such tight integration is that new developments in IT are available to NetRexx programmers as soon as they are available to the Java developers.

For example, when the Raspberry Pi first emerged — at least, when we could get our hands on one — a whole new world of turning on and off of LEDs and relays via GPIO pins was open to NetRexx. From the beginning, there were two class libraries that could perform the low-level I/O operations, such as with this:

import framboos.
  led = Outpin(0)
  led.setValue(1)
  Thread.currentThread().sleep(10000)			
  led.setValue(0)
  led.close()

Note that this is the complete program, using the scripting mode of NetRexx, in which there is no need to set up class definitions, class constructors, or a main() method. This program will turn on an LED, wait 10 seconds, and turn it off.

While coding, you can use an edit-and-interpret cycle for rapid development; when the code is debugged and working, it can be compiled to a class file and stored in a JAR along with the other application classes.

A slightly larger example (Listing One) shows the use of NetRexx with the Eclipse Paho client for MQTT, the machine-to-machine messaging protocol for the Internet of Things (which is a good combination with a Raspberry, by the way — the mosquitto message broker does an excellent job offering a complete message broker in a small memory footprint).

Listing One: NetRexx written as a class.

import java.sql.Timestamp
import org.eclipse.paho.client.mqttv3.

class Publish implements MqttCallback
  
  method Publish()
    conOpt    = MqttConnectOptions()
    conOpt.setCleanSession(0)
    tmpDir    = System.getProperty("java.io.tmpdir")
    dataStore = MqttDefaultFilePersistence(tmpDir)
    clientId  = MqttClient.generateClientId()
    topicName = "/world"
    payload   = "hello".toString().getBytes()
    qos	      = 2    

    do
      broker	= "localhost"
      port	= "1883"
      brokerUrl	= "tcp://"broker":"port
      client	= MqttClient(brokerUrl,clientId, dataStore)
      client.setCallback(this)
    catch e=mqttException
      say e.getMessage()
      e.printStackTrace()
    end -- do

    client.connect()
    log("Connected to "brokerUrl" with client ID "client.getClientId())

    -- Get an instance of the topic
    topic = client.getTopic(topicName)
    
    message = MqttMessage(payload)
    message.setQos(qos)
	
    -- Publish the message
    time = Timestamp(System.currentTimeMillis()).toString()
    log('Publishing at: 'time' to topic "'topicName'" with qos 'qos)
    token = topic.publish(message)
    
    -- Wait until the message has been delivered to the server
    token.waitForCompletion()
    
    -- Disconnect the client
    client.disconnect()
    log("Disconnected")
  
  method log(line)
    say line
    
  method messageArrived(t=MqttTopic,m=MqttMessage)
    
  method deliveryComplete(t=MqttDeliveryToken)
    log("Delivery Complete: " t)

  method connectionLost(t=Throwable)
    log("Connection Lost:" t.getMessage())

  method main(args=String[]) static
    Publish()

For the log() method in Listing One, we would choose an Apache log4j implementation in production code.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video