B4J Question SetThreadRequest / GetThreadRequest

Magma

Expert
Licensed User
Longtime User
Trying to figure which is the best way to work with Server's Requests... and to avoid writting hundreds of lines...

hopefully using this... when i want to get the right "request" of user
at any sub i want: Dim req As ServletRequest = GetThreadRequest
and get the right request...

otherwise i must put at any sub the "req As ServletRequest" that is adding to me extra work...

is it a right way ?


B4X:
Sub Process_Globals
    Private RequestThreadLocal As JavaObject
End Sub

Public Sub SetThreadRequest(req As ServletRequest)
    If RequestThreadLocal.IsInitialized = False Then
        RequestThreadLocal.InitializeNewInstance("java.lang.ThreadLocal", Null)
    End If
    RequestThreadLocal.RunMethod("set", Array(req))
End Sub

Public Sub GetThreadRequest As ServletRequest
    If RequestThreadLocal.IsInitialized = False Then Return Null
    Return RequestThreadLocal.RunMethod("get", Null)
End Sub

having SessionFilter:
B4X:
'SessionFilter Class
Sub Class_Globals
    
End Sub

Public Sub Initialize
    
End Sub

Public Sub Filter(req As ServletRequest, resp As ServletResponse) As Boolean
    themodulehasit.SetThreadRequest(req)
    Return True
End Sub
 

Daestrum

Expert
Licensed User
Longtime User
If you're running on Java 21+ you can look at:
t.InitializeStatic("java.lang.Thread").RunMethod("ofVirtual", Null)

It may help you. Similar principle, but not reliant on real thread pool.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
yes it should work fine for you.
 
Upvote 0
Solution
Top