DEV Community

InterSystems Developer for InterSystems

Posted on • Originally published at community.intersystems.com

1

Java External Language Gateway

If you like Java and have a thriving Java ecosystem at work into which you need to incorporate IRIS, it's not a problem. Java External Language Gateway will do it seamlessly, almost. This gateway serves as a bridge between Java and Object Script in IRIS. You can create objects of Java classes in IRIS and call their methods. You just need a jar file to do this.

Connection diagram: proxy object <-> Gateway object <-> TCP/IP <-> External server <-> target object

The first thing you need to do is set up the environment. To start using the Java Gateway, ensure you have the following:

  1. InterSystems IRIS: Installed and running.
  2. Java Development Kit (JDK): Installed and configured.

The second requirement may sound simple, since you're already using Java in your work, but it isn't. Thanks to this question, it turned out that you need to use JDK version 11 at the most. Which means that you need to change the version in your IDE which may case quite a bit of trouble.

Next step, we can check that everything works and try to instantiate the object of a Java system class. To do this, you have to start a connection, creat a proxy object, and call a method. Sounds like a lot of code, but in reality it's just one statement:

write $system.external.getJavaGateway().new("java.util.Date").toString()

This will print the current date and time on screen.

To go further, we can create our own Java class:


public class Dish {
    private String name;
    private String description;
    private String category;
    private Float price;
    private String currency;
    private Integer calories;
    
    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public void setCalories(Integer calories) {
        this.calories = calories;
    }
    
    public String describe() {
        return "The dish "+this.name+" costs "+this.price.toString()+
                this.currency+" and contains "+this.calories+" calories!";
    }
}

and call it's methods from Object Script:

 set javaGate = $SYSTEM.external.getJavaGateway()
 do javaGate.addToPath("D:\Temp\GatewayTest.jar")
 set dish = javaGate.new("Dish")
 do dish.setCalories(1000)
 do dish.setCategory("salad")
 do dish.setCurrency("GBP")
 do dish.setDescription("Very tasty greek salad")
 do dish.setName("Greek salad")
 do dish.setPrice(15.2)
 write dish.describe()

As a result we will get a string with the description of the created dish:

The dish Greek salad costs 15.2GBP and contains 1000 calories!

Quite neat, don't you think?

Anyway, learn more about using Gateways from the Documentation and good luck implementing this knowledge in your projects!

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay