0

I have a perl program that dynamically generate images based on given width. I need to create web service that takes image dimensions from client, and pass it to the perl program to create the image then send it back to the client.

Hope this image helps to understand

Now on Jelastic cloud, I created 3 nodes:

  • Node 1: for tomcat (contains the java code).
  • Node 2: for MySql (contains the database).
  • Node 3: for Centos VPS (contains the perl code).

My questions:

  1. I'm I doing the right thing?? if not what is the best way to do my program?

  2. How can I call the perl code (in node 3) from the java service (in node 1), and return the generated image back to client.

Borodin
  • 126,100
  • 9
  • 70
  • 144
Ahmedy
  • 71
  • 4

1 Answers1

3

It sounds a reasonable design. You would write something like this.

import java.lang.Runtime;

int width = 99;

try {
    Runtime runt = Runtime.getRuntime()
    Process proc = runt.exec('/usr/bin/perl', '/path/to/myperl.pl', Integer.toString(width));
    proc.waitFor();
}
catch (Exception ioe) {
    ioe.printStackTrace();
}

Of course you'll have to adjust /usr/bin/perl to where your own perl exeutable really is, or you could invoke the shell to get it to search the path by using

runt.exec( '/bin/bash', '-c', 'perl', '/path/to/myperl.pl', Integer.toString(width) );

As for how to get the image back to the client, you don't say much about how your Perl program works, but either you tell it where to write the file or it decides for itself and tells you where it's put it afterwards

If it's the former, then you presumably pass the path on the command line, so you just have to extend the call to runt.exec above to pass another parameter

If it's the latter, then presumably the program prints to STDOUT where it has put the new file, and you need to read that stream from your Java code to collect the information. That would look like this in place of the proc.waitFor() call

import java.io.*;

BufferedReader inp = new BufferedReader(
    new InputStreamReader(proc.getInputStream())
);

while ( ( line = inp.readLine() ) != null ) {
    // Process output of Perl code to get file location
}
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • your solution seems good only if I have all environments are installed on the same node (tomcat, perl). – Ahmedy May 25 '16 at 08:24
  • In jelastic the tomcat node is separate from the centos node. They act like two different PCs "if I'm right". So I cannot use your method to call the perl program unless they are in the same node. – Ahmedy May 25 '16 at 08:34
  • @Ahmedy, you can execute perl script on the same tomcat node. Please clarify the reason why you put it into a separate VPS node, maybe some libs are missing? – Ruslan Mar 14 '19 at 11:53