Communicating between Rev and Java

Interested adding compiled externals from LiveCode and third parties to your LiveCode projects? This is the place to talk about them.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
pspringer57
Posts: 9
Joined: Mon Oct 20, 2008 5:51 pm

Communicating between Rev and Java

Post by pspringer57 » Mon Oct 20, 2008 6:14 pm

I have a large Java application (not JScript) and I would like to exchange data between a Runtime stack and that Java application (on a Windows PC). It looks like it might be an option to create a DLL from my Java source/.jar but that looks pretty complex/expensive.

Perhaps JScript would provide a way to do this.

Essentially, I want to use Revolution as a front end for this Java app, which will act as a server.

Does anyone have experience, knowledge, or examples that might help me out with this?

Thanks,
Paul

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Mon Oct 20, 2008 7:18 pm

Hi Paul,

At work I use a lot of Java (for Swing GUIs and multithreader daemon processes - not JSP/Servlets/Web applications) though I happily come back to Revolution whenever I get a chance. There are a few ways to make Rev and Java communicate:
1. If the Java app is a command-line application, you can execute it using a single 'shell' function call which will return the System.out console output.
2. Similarly, and necessary of you're exchanging a lot of data with the Java application, you can use the 'open process' / 'write to process' / 'read from process' / 'close process' commands to steer your Java application.
3. The next popular way is to use socket communication; in Revolution you'll use the 'open socket' / 'write to socket' / 'read from socket' / 'close socket' commands to send and receive data; in Java you'd setup a ServerSocket (I can recommend the book 'TCP/IP Sockets in Java' for help with that if you haven't done it before)
4. More outlandish solutions include wrapping your Java methods as web services (e.g. on top of Glassfish) or even use asynchronous messaging via JMS (e.g. using OpenESB).

HTH,

Jan Schenkel.[/i]
Quartam Reports & PDF Library for LiveCode
www.quartam.com

pspringer57
Posts: 9
Joined: Mon Oct 20, 2008 5:51 pm

Post by pspringer57 » Mon Oct 20, 2008 7:31 pm

Thanks Jan - I will investigate. A quick look at the User's Guide did not turn up any details of how the open/write to/read from/close process commands work, but I'll see what I can find.

There will be lots of data, particularly returning from the Java app - on the order of many thousands of lines, as for a .CSV file.

-Paul

pspringer57
Posts: 9
Joined: Mon Oct 20, 2008 5:51 pm

Post by pspringer57 » Mon Oct 20, 2008 7:50 pm

OK, I found the process command descriptions in the Dictionary. Now, can you tell me how the Java program sees data written to the "process"? Do you read as from the console?

Thanks,
-Paul

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Tue Oct 21, 2008 5:58 am

Here's a Java example class which reads from the System.in pipe and writes to the System.out pipe. I wrapped System.in with a BufferedReader so that I could easily read a single line at a time.

Code: Select all

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class ConsoleTest {
	
	public static void main(String[] args) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		InfiniteLoop:
		while (true) {
			System.out.println("Enter a number between 1 and 7 <Press enter to continue, enter 0 to end>");
			String line = reader.readLine();
			int number = 1;
			try {
				number = Integer.parseInt(line);
			} catch (NumberFormatException e) {
				//ignore and just ask again
			}
			if (number == 0) {
				break InfiniteLoop;
			} else if (number > 0 && number < 8) {
				System.out.println("You picked number " + number + "!");
			} else if (number < 0 || number > 7) {
				System.out.println("Tsk tsk - between 1 and 7!");
			}
		}
	}
}
When you're printing something from Java to System.out, you will 'read from process' in Revolution; and everything you 'write to process' in Revolution can be gotten via System.in

The main downside is that these calls are blocking, which means if one application goes haywire, the other can end up in a deadlock. That's where socket communication gives you a little more leeway.

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

pspringer57
Posts: 9
Joined: Mon Oct 20, 2008 5:51 pm

Post by pspringer57 » Tue Oct 21, 2008 1:00 pm

Thanks Jan - you've been very helpful. I am making progress with a socket solution and will probably go that way, but your example on read and write from/to "process" cleared that up for me too.

As an aside, I am having a lot of trouble using the forum - every time I try to submit something I get an "Invalid Session. Please resubmit . . ." error. Do you know if this is a frequent problem, or more likely something on my end? I eventually get through by re-trying, refreshing, etc. but I haven't yet found the "secret combination" that always works.

-Paul

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Tue Oct 21, 2008 1:11 pm

Glad to hear you're having success with sockets. I recently had to do a big client-server project with sockets in Java and multithreading communications, and was quite pleased with the end result.

As for your forum problems - perhaps it needs cookies and your browser is not accepting them?

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply

Return to “Using Externals”