B4J Question Creating custom libraries in Java

wl

Well-Known Member
Licensed User
Longtime User
Hi,

Am sure there was somewhere some guidance in how to create B4J libraries in Java, but I can't seem to find it ?

Any hints, references ?

Thanks!
 

wl

Well-Known Member
Licensed User
Longtime User
Thanks, looking into it. As well as jSLC.

Do you happen to have a simple demo on how to call an event from Java code in B4J code. I found anywheresoftware.b4a.keywords.Common.CallSubNew...

But what are those parameters: where to get the ba object etc ..

Thanks !
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
To answer my own question and as a help for all those wondering the same ... a small demo Java class ...

B4X:
package demo;

import anywheresoftware.b4a.BA.*;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.keywords.Common;

@ShortName ("Demo")
@Events(values={"OnSum (val as int)"})
public class Demo {
    private BA m_ba;
    private String m_eventPrefix;
     
    public void Initialize (BA p_ba, String p_eventPrefix) {
        m_ba = p_ba;
        m_eventPrefix = p_eventPrefix.toLowerCase(BA.cul);
    }
   
    public int add (int x, int y) {
        int sum = x + y;
        if (m_ba.subExists(m_eventPrefix + "_onsum")) {
            m_ba.raiseEvent(this, m_eventPrefix + "_onsum", x + y);
        }
        return sum;
    }
}


and the example calling code in B4J:

B4X:
Sub test
    Dim dm As Demo
    dm.Initialize("demo")
    Log ("Through method: " & dm.add(7, 4))
End Sub

Sub demo_OnSum (val As Int)
    Log ("through event: " & val)
End Sub
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
I was planning to create a Java library that would allow me to run code in a separate thread.
In fact as I'm writing a http reverse proxy I need low level control on (http) data passing through a websocket and passing it to another one and visa versa.

In its current implementation asyncstreams call events on the main thread and this does not work when multiple simultaneous request are being launched at the same time.

But I read threading is/will(?) problems in B4J ? @Erel: would it be possible to elaborate what kind of problems to expect ? As webserver applications are multithread most B4J code would be able to run in a separate thread, no ?

Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
In its current implementation asyncstreams call events on the main thread and this does not work when multiple simultaneous request are being launched at the same time.
It is true that all the events will run on the main thread. However if you use AsyncStreams correctly then you can immediately write the data to the stream and the writing will happen on a background thread. So it will work properly with multiple connections.

You can write a library that receives a stream and writes it to a different stream. It will probably won't work better than with AsyncStreams but it should work.

You shouldn't run B4X code on a background thread except of a very specific cases such as in server solutions.
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
Meanwhile I wrote a (lowlevel) HTTPS reverse proxy using a some class wrappers (SSL socket, thread, countdownlatches, ...) I wrote in Java.
I works flawlessly, no memory leaks, no threading issues etc ... I did some extensive stress tests on it...

So B4J can be programmed to work multithreaded without using the Server class ...
 
Upvote 0
Top