B4J Question How to define a global (non-session) variable in jRDC2 server

Diceman

Active Member
Licensed User
I have a jRDC2 app where the server is of course written using B4J. Everything is working fine, but every 10 minutes the server needs to delete some rows from a PostgreSQL table.

1) Is there a way to make
Req.GetSession.SetAttribute("LastCheckExpiredSessions", DateTime.Now)​
global so it can be accessed by any session instead of just the current session? If so, then I can check this attribute whenever Handle finishes handling a request to see if 10 minutes has expired from the last time the expired session rows were deleted. A user may be connected for only 30 seconds at a time then leaves, so I can't depend on a user ever being connected for as long as 10 minutes.

2) Is there an event that gets triggered if a session gets expired?

3) I thought of using a Timer that executes every 10 minutes on the server but of course this may interfere with the server handling incoming requests.

Can someone point me in the right direction?
TIA
 

DonManfred

Expert
Licensed User
Longtime User
What aboutusing and changing a global value instead of using the requests session?
 
Upvote 0

Diceman

Active Member
Licensed User
What aboutusing and changing a global value instead of using the requests session?

I had thought about that. It is certainly the easiest solution but I don't think the jRDC2 server can have a global variable (defined in Process_Globals) that can be modified by a thread. In other programming languages that use threads, the thread has to perform a semaphore lock on the main thread so it can modify a global variable that is stored on the main thread. Otherwise several threads could theoretically attempt to modify the global variable on the main thread at the same time.

From my understanding of how jRDC2 works is (and correct me if I'm wrong), AppStart starts the monitoring of the connections by calling StartMessageLoop and StartMessageLoop will call Handle to handle the request when a request comes in. The Handle sub defined in RDCHandler, is running on its own thread so it can't directly update a global variable on the main thread (without locking the main thread). From my understanding these global variables in Process_Globals are read-only. And I don't know how to lock the main thread to update the global variable or even if it is advisable to do so.

We may have to wait for Erel to give us the definitive answer.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
We may have to wait for Erel to give us the definitive answer.
Please don't add such lines to your questions. They will be ignored.

There is no problem with accessing a primitive variable from different threads. However I don't think that you need it here. Add a background worker that uses a timer to run the task every 10 minutes.

https://www.b4x.com/search?query=background+worker
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I had thought about that. It is certainly the easiest solution but I don't think the jRDC2 server can have a global variable (defined in Process_Globals) that can be modified by a thread. In other programming languages that use threads, the thread has to perform a semaphore lock on the main thread so it can modify a global variable that is stored on the main thread.
FYI, you can use CallSubDelayed to queue access to variables/sub routines of the Main thread (or any thread). This would resolve the issue(s) of simultaneous access of variables/subroutines.
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
Not the best way to approach this IMO. It's far better to run such jobs on the server using something like PGAgent or PG_Cron which runs as a background worker in the PostgeSQL server process.
 
Upvote 0

Diceman

Active Member
Licensed User
Not the best way to approach this IMO. It's far better to run such jobs on the server using something like PGAgent or PG_Cron which runs as a background worker in the PostgeSQL server process.

Good advice keirS.:rolleyes:

I've heard of pg_chron to create a job in PostgreSQL but it is overkill in my case.
Now if my job that runs every 10 minutes took a long time, say 5 to 10 seconds, then I would definitely run it on the server using something like pg_chron. I would also have to create a daemon to make sure the chron job is executing as scheduled.

But my job may take 5ms at most to execute on the server, every 10 minutes. So it would not be a burden to the jRDC2 server.
But others reading this may want to look into offloading their chron jobs to the db server if it takes a long time to run so it frees up the webserver to handle more connections.
 
Upvote 0
Top