B4J Question "Normal" Libraries and jServer together possible?

DonManfred

Expert
Licensed User
Longtime User
I am developing a DiscordBot for a friend of mine.

I started using a jserver app.
I also used the JDA Library in my Main Module. The Code here is expected to run all the time. It is listening to Events.

As i did encounter problems while developing; i got some Exceptions and i´m not sure if the Code in Main is always running to be able to react to Events.

I decided to use a UI app for now. Here all is working fine.

So my question:
Is it allowed to use a jServer app like this? Or do i need to only do the work in the ServerHandlers?

My goal was/is to use the JDA always running (and do the work when an Event raises and additionally i want to use the ServerHandlers to Control the Code running in Main.

I can remember i´ve something read regarding this. But i am not sure. Something that i do need to add special java parameters or something like that.

Any helpful info is highly appreciated ;-)

EDIT: Dammit. Wrong Forum.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is not recommended to use jServer in a UI app. jServer uses a special threading model and is designed to run in non-ui apps.

As i did encounter problems while developing; i got some Exceptions and i´m not sure if the Code in Main is always running to be able to react to Events.
More information please...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It is not recommended to use jServer in a UI app
Yes, i was firstly using jServer TOO. In the UI App i am not using jserver at all. Here i am only using the JDA Library and i do react in Events. I get an Message Arrived Event for any Message the Bot got.

More information please...

I am not able to give any congrete Error as of now as i did changed to not use jServer for now. Until i know it should work.

I try to explain:

When using jServer i do initialize jServer in Main Module. Then, for the jServer, additional work is done in any of the defined ServerHandlers.
I do need to run a JDA Instance in the Server-App TOO.

As JDA does not need much but a Databaseconnection i am using ConnectionPool to use DB-Access inside the JDA-Events.
JDA is initialized in Main too. JDA is internally using a WebSocketClient (i dont need to take care about; all is handled by JDA internally.
JDA raises two Events.
B4X:
Sub JDA_onReady(event As Object)
End Sub
' and
Sub JDA_onMessageReceived(event As Object)
End Sub

I do all the "work" inside (or initiated by the Event) JDA_onMessageReceived.

I also do use a Timer (in Main) to check for unhandled Data to store. It raises every 5 Seconds a Tick and here i do save Data to the Database which are in a "Queue" (a Processglobal List),

While developing i did errors for sure which then raises an Exception. From the Error i see in the Log it looks all as it happens in

B4X:
Sub AppStart (Args() As String)
End Sub

but the Error is inside some code which runs inside the Message Arrived Event or in the timer Tick Event.

I was thinking that i can not use the JDA in the Server App how i am using it. So i decided to ask here to be sure it should work like this at all.
Coding error are expected and does crash some times, sure.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
While messing with some jServer and background workers, I did notice that I can "crash" the Main thread and the jServer and background worker will merrily keep on running. With that I've decided to do as little as possible in the Main thread as I can get away with. If your JDA library (my search fu is weak this morning, could not find it) does not realy on jFX, you could put all your code into a background worker, only communicating with the Main thread as is necessary. You'll have to finagle some communication means between Main and your background worker, but it's not insurmountable.

Links:
Info on background workers: https://www.b4x.com/android/forum/threads/server-background-workers.73269/#content
Hint on how to communicate with background worker: https://www.b4x.com/android/forum/threads/hot-get-backgroundworker-from-main-bas.86013/#post-544780
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I should move all the JDA Initialization into a Backgroundworker. Did i understand that correctly?
It is an option, as long as we are talking about non-JavaFX code. If JDA crashes, it would not take down the main thread. Problem now is that you cannot start a new background worker. My workaround is to use a pool of background workers. If one crashes, spin up another (callsub the background workers init routine, that contains the JDA initialization stuff). Of course if you crash all the background workers in the pool, then you are back at square one (restart application). In the end, this may not be a solution for the issue you are trying to solve.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
In the end, this may not be a solution for the issue you are trying to solve.
So better to use the main thread (Main Module) as long as i do not do anything long task (all subs i use are called using wait for, are resumeable subs.

I CAN use it in Main Module? <- not blocking any long time when running the event.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I CAN use it in Main Module? <- not blocking any long time when running the event.
Why not? The jServer portion will run merrily in the background. The only issue with a long blocking event would be for any other code on the Main thread. jServer will do it's own thing (unless the code in the Main thread starts hogging all CPU/RAM capacities).
 
Upvote 0
Top