Coffee2Code
3rd February 2023, 17:31
Hi,

I tried the example in the programming manual to start a java application.
As the example isn't directly working, I tried to fix it. it seems to be working well....
unless the buckets I receive have neither a header nor a body. The return value of java.get.bucket is 0, and it changes to -4 if no bucket is available anymore.
I wonder what I do wrong.
the session code:

extern domain tcmcs.s999 f.in
extern domain tcmcs.s999 f.out
extern domain tcmcs.long f.ret
extern domain tcmcs.s999 f.class
extern domain tcmcs.s999 f.method

long queue.fromJava
long queue.toJava
long Handle, ret

|****************************** program section ********************************


|****************************** group section **********************************

group.1:
init.group:
get.screen.defaults()
f.in = "Enter a text to be echoed"
f.class = "test.MyTest"
f.method = "MyMethod"

|****************************** choice section ********************************

choice.cont.process:
on.choice:
read.main.table()

choice.bms:
on.choice:
receive.java()

|****************************** field section *********************************


|****************************** function section ******************************

functions:

function extern receive.java()
{
long length
domain tcmcs.s999 header
f.out = ""
f.ret = java.get.bucket(queue.fromJava, f.out, length, header)
f.out = "header: " & header & " Body: " & f.out
display("f.out")
display("f.ret")
}

function extern stop.java()
{
f.ret = java.unload.application(Handle)
display("f.ret")
java.uninstall.listener(queue.fromJava)
java.destroy.queue(queue.fromJava)
java.destroy.queue(queue.toJava)
}

function extern start.java()
{
queue.fromJava = java.new.queue()
queue.toJava = java.new.queue()


Handle = java.load.application("CM_TEST")
if (Handle <= 0) then
e = e
endif

f.ret = java.install.listener(queue.fromJava)

f.ret = java.execute.static.application.method(handle, f.out, "test.MyTest",
"installListener",
queue.fromJava, queue.toJava)
display("f.ret")
}

function extern send.java()
{
f.ret = java.put.bucket(queue.toJava, f.in, len(f.in), "header")
if f.ret = 0 then
e=e
else
e=e
endif
display("f.ret")
}

function extern read.main.table()
{


Handle = java.load.application("CM_TEST")
if (Handle <= 0) then
e = e
endif

f.ret = java.execute.static.application.method(handle, f.out, f.class, f.method, f.in)
if f.ret = 0 then
e=e
else
e=e
endif
display("f.ret")
display("f.out")
java.unload.application(Handle)
}



the java code:

package test;

import com.baan.baanvm.*; // import the interface definitions

public class MyTest implements IQueueListener {

public static String MyMethod(String b) {
return ("Java: " + b);
}

private int m_queueFromERP;
private int m_queueToERP;
private static IBaanVM s_iBaanVm = new BaanVMImpl();

/** constructor of an instance of the listener */
public MyTest(int p_idIn, int p_idOut) {
m_queueFromERP = p_idIn;
m_queueToERP = p_idOut;
}

/**
* Called by Infor Enterprise Server (java.execute.static.method.async). Its
* main function is to install an instance of the listener on the queue, and to
* initialize all variables
*/
public static void installListener(int queueToERP, int queueFromERP) {
try {
/* install an instance of the listener */
s_iBaanVm.installListener(queueFromERP, new MyTest(queueFromERP, queueToERP));
} catch (Exception e) {
/* failed to install listener, log error message */
s_iBaanVm.logMessage(e.toString(), 0);
}
}

/**
* This listener method will be called whenever a new bucket appears on the
* m_queueFromERP queue
*/
public void onReceive(IBucket p_bucket) {
int ret = 0;
IBucket tmp_bucket = null;
if (p_bucket != null) {
/*
* we have received data, do something (using the IBucket interface to set and
* retrieve the contents) !
*/

/* for this example, just bounce the bucket back */

/* place bucket on the queue m_queueToERP */
tmp_bucket = s_iBaanVm.createBucket("asd", "Received Bucket " + p_bucket.toString());
if ((ret = s_iBaanVm.putBucket(m_queueToERP, tmp_bucket)) < 0) {
/* failed to place bucket on queue to Infor Enterprise Server */
s_iBaanVm.logMessage("Failed to bounce bucket ret = " + ret, 0);
}
} else {
/* no bucket received, log a debug message */
tmp_bucket = s_iBaanVm.createBucket("NULL RECCEIVED");
s_iBaanVm.logMessage("QueuePinger(" + m_queueFromERP + ") received: NULL bucket!!!", 0);
}
}

}


I also wonder how to implement the receiving of buckets in a proper way. for now I click a button on session to fetch it manually.

Coffee2Code
6th February 2023, 10:11
function extern receive.java()
{
long length
domain tcmcs.s999 header
f.out = ""
f.ret = java.get.bucket(queue.fromJava, f.out, length, header)
f.out = "header: " & header & " Body: " & f.out
display("f.out")
display("f.ret")
}


Error was in java.get.bucket. the lentgh is the length I wanto to receive. For some reason I thought it would be a reference.

So this is working:

function extern receive.java()
{
long length, ret
domain tcmcs.s999 header, out
out = ""
length = 999
ret = java.get.bucket(queue.fromJava, out, length, header)
if ret > 0 then
f.out = out(1;ret)
out = out(1;ret)
endif
if ret < 0 then
return
endif
f.ret = ret
f.out = "header: " & header & " Body: " & f.out
display("f.out")
display("f.ret")
}

I still wonder how to implement the receiving of buckets in a proper way. I added a timer to now need to click it all the time. But I wonder if there is an event driven way to achive it, as the timer seems to block the GUI each time its running, which makes it weird to type.