B4J Question Problem with jOKHttpUtils2 3.0

Pablo Torres

Active Member
Licensed User
Longtime User
Hello everyone, I'm sorry to disturb but I have an Issue that I'm not able to fix by myself, so I ask for anyone who has the knowledge to point me in the right address

I'm currently working with AWS Lambda functions, I have sucked on connecting to a Aurora Serverless BD with these functions and they work great!

But now, In one of the lambdas of my API I have to make a post to a URL, this is normal to me because I've been doing the same post to the same URL with a VPS since, at least, 1 year ago and everything is OK

I'm currently using Java 11 for the lambda, like I said everything works fine except the HTTP post, I'm publishing here part of the code I use (the important) and the logs I get from testing the lambda:

lambda code:
'Non-UI application (console / server application)
#Region Project Attributes
    #MergeLibraries: True
    #AdditionalJar: mysql-connector-java-8.0.29.jar
#End Region

Sub Process_Globals
    Private urlRespuesta As String
End Sub

Public Sub responsehandler(mapIn As Object) As String
    Try   
        Dim strData As String=mapIn
        Dim MyJPData As JSONParser
        MyJPData.Initialize(strData)
        Dim MapDatos As Map=MyJPData.NextObject
        Dim strEmail As String = MapDatos.Get("email")
        Dim jSQL As SQL
        jSQL.Initialize2("com.mysql.cj.jdbc.Driver","jdbc:mysql://xxxxxxxxxxxxxxx.us-east-1.rds.amazonaws.com:3306/xxx?", "XXXXXX","XXXXXX")
        ...
        Do some stuff
        ...
        Plexo(Message)
        
        StartMessageLoop
        
        jSQL.Close
        
        Return urlRespuesta
        
    Catch
        jSQL.Close
        Return "https://aru.sportsticket.uy/error.html"
    End Try
End Sub

Sub Plexo (strM As String)
    Dim j As HttpJob
Log("A")
    j.Initialize("",Me)
Log("B")
    j.PostString("https://xxxxx/plexo",strM)
Log("C")
    Wait For (j) JobDone(j As HttpJob)
Log("D")
    If j.Success Then
Log("E")
        Dim JP As JSONParser
Log("F")
        JP.Initialize(j.GetString)
Log("G")
        Dim mapRespuesta As Map=JP.NextObject
Log("H")
        urlRespuesta=mapResuesta.Get("Uri")
Log("P")
    Else
Log("Q")
        urlRespuesta="https://aru.sportsticket.uy/error.html"
Log("R")
    End If
Log("V")
    j.Release
Log("W")
    StopMessageLoop
End Sub

And the Logs I get from testing the lambda are:
A
B
C
httputils2service._submitjob (java line: 148)
java.lang.NullPointerException
at com.cronos.lambda.arucompraweb.httputils2service._submitjob(httputils2service.java:148)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:509)
at anywheresoftware.b4a.keywords.Common.access$0(Common.java:489)
at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:563)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:175)
at com.cronos.lambda.arucompraweb.main._responsehandler(main.java:749)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at lambdainternal.EventHandlerLoader$PojoMethodRequestHandler.handleRequest(EventHandlerLoader.java:282)
at lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:199)
at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:899)
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:262)
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:199)
at lambdainternal.AWSLambda.main(AWSLambda.java:193)


I don't know what am I doing wrong, but any help will be highly appreciated

Many Thanks
 

Pablo Torres

Active Member
Licensed User
Longtime User
Which line the IDE highlighted?
Hi aeric, the IDE has no highlight, I have no chance to debug with IDE because this is AWS Lambda function, I only can release it as a jar, upload it, test it over aws and watch the logs.

Moved to the questions forum.

Try to reproduce it in a small project and upload the project.
Great, uploading the small project. I did not take off the libraries I use in case something has to do with them.
The code of the project is very simple, I upload it to aws and to test it just post {"email":"[email protected]"} to the url of the lambda.
If someone want to run a test on AWS and has no account just upload the project here and I do the test or email me with the project, I can help with any test

Many Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are doing something special and this is obviously not a bug in jOkHttpUtils2. B4J apps are meant to run in a specific way.

As you are running it inside the AWS container, it breaks all kinds of things. One of those things is that it doesn't call the various Process_Globals.

You can try to call them yourself. Maybe it will work. Maybe it won't.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I assume the code you posted is a "server" app, even though aws lambda should be a serverless service.
My question is how do you call or trigger this service?
or in other words, how do I call responsehandler ?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
There is problem with https://sportsticket.uy/recibirplexo

LambdaTest:
'Non-UI application (console / server application)
#Region Project Attributes 
    #CommandLineArgs:
    #MergeLibraries: True 
#End Region

Sub Process_Globals
    'Private urlRespuesta As String = "https://sportsticket.uy/recibirplexo" ' <-- Take note HTTPS is not working !
    Private urlRespuesta As String = "http://sportsticket.uy/recibirplexo"
    Private urlError As String = "https://aru.sportsticket.uy/error.html"
    Private Timer1 As Timer
End Sub

Sub AppStart (Args() As String)
    Log("App started!!!")
    Timer1.Initialize("Timer1", 3000)
    Timer1.Enabled = True
    StartMessageLoop
End Sub

Sub Timer1_Tick
    Log ( "Timer started!!!" )
    Timer1.Enabled = False
    Wait For ( ResponseHandler($"{"email":"[email protected]"}"$) ) Complete (Value As String)
    Log( Value )
    StopMessageLoop
End Sub

Public Sub ResponseHandler (data As String) As ResumableSub 
    Try
        Dim strResult As String
        Dim MapDatos As Map = data.As(JSON).ToMap
        Dim strEmail As String = MapDatos.Get("email")
        Log( "Email: " & strEmail )
        'Dim strGenerado As String=File.ReadString(File.DirAssets,"CheckoutWeb.txt")
        'Plexo(strGenerado)    
        Wait For ( Plexo(data) ) Complete (Result As String)
        strResult = Result
    Catch
        Log( LastException )
        strResult = urlError
    End Try
    Return strResult
End Sub

Sub Plexo (strM As String) As ResumableSub
    Dim strResult As String
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString(urlRespuesta, strM)
    Wait For (j) JobDone (j As HttpJob)
    If j.Success Then
        Log( "Result: " & j.GetString )
        strResult = urlRespuesta
    Else
        Log( "Error: " & j.ErrorMessage )
        strResult = urlError
    End If
    j.Release
    Return strResult
End Sub
 

Attachments

  • LambdaTest.zip
    1 KB · Views: 114
Upvote 0

Pablo Torres

Active Member
Licensed User
Longtime User
You are doing something special and this is obviously not a bug in jOkHttpUtils2. B4J apps are meant to run in a specific way.

As you are running it inside the AWS container, it breaks all kinds of things. One of those things is that it doesn't call the various Process_Globals.

You can try to call them yourself. Maybe it will work. Maybe it won't
I don't think Process_Globals is the problem in this case because if that is the problem it should log D, E, F, G and H before the error.
But, maybe, it's a permission problems (internet permission for example). I will find out about that.
I assume the code you posted is a "server" app, even though aws lambda should be a serverless service.
My question is how do you call or trigger this service?
or in other words, how do I call responsehandler ?
It is NOT a server app, however it acts as one
Is a non-ui "normal" jar, it is uploaded as a AWS Lambda, it means that is is stored in a server (don't know where) and they give you different ways to trigger that.
One of that ways (the one I'm using right now) Is to make a post to the url of the lambda, that is where I post this:{"email":"[email protected]"}, just a regular json with the email.
There is problem with https://sportsticket.uy/recibirplexo

LambdaTest:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    'Private urlRespuesta As String = "https://sportsticket.uy/recibirplexo" ' <-- Take note HTTPS is not working !
    Private urlRespuesta As String = "http://sportsticket.uy/recibirplexo"
    Private urlError As String = "https://aru.sportsticket.uy/error.html"
    Private Timer1 As Timer
End Sub

Sub AppStart (Args() As String)
    Log("App started!!!")
    Timer1.Initialize("Timer1", 3000)
    Timer1.Enabled = True
    StartMessageLoop
End Sub

Sub Timer1_Tick
    Log ( "Timer started!!!" )
    Timer1.Enabled = False
    Wait For ( ResponseHandler($"{"email":"[email protected]"}"$) ) Complete (Value As String)
    Log( Value )
    StopMessageLoop
End Sub

Public Sub ResponseHandler (data As String) As ResumableSub
    Try
        Dim strResult As String
        Dim MapDatos As Map = data.As(JSON).ToMap
        Dim strEmail As String = MapDatos.Get("email")
        Log( "Email: " & strEmail )
        'Dim strGenerado As String=File.ReadString(File.DirAssets,"CheckoutWeb.txt")
        'Plexo(strGenerado)   
        Wait For ( Plexo(data) ) Complete (Result As String)
        strResult = Result
    Catch
        Log( LastException )
        strResult = urlError
    End Try
    Return strResult
End Sub

Sub Plexo (strM As String) As ResumableSub
    Dim strResult As String
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString(urlRespuesta, strM)
    Wait For (j) JobDone (j As HttpJob)
    If j.Success Then
        Log( "Result: " & j.GetString )
        strResult = urlRespuesta
    Else
        Log( "Error: " & j.ErrorMessage )
        strResult = urlError
    End If
    j.Release
    Return strResult
End Sub
Yo were right, it was Http not https, but I corrected that and still receiving the same logs and everything like before
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
It seems to create a lambda function with Java requires AWS SDK and Maven dependencies.

 
Upvote 0

Pablo Torres

Active Member
Licensed User
Longtime User
It seems to create a lambda function with Java requires AWS SDK and Maven dependencies.

First of all, many thanks @aeric for your answers and efforts so far
I have created several lambda functions (and they are working fine) without this. None of them crashed until now that I'ma adding the feature of making a post to a external url.
The jar dies in that point, never happened to me before, I have been using this library in B4A, B4i and B4x for years and everything worked great... but it seems that AWS don't like that library.

can you point me on how to add AWS SDK and maven to my project? I have no idea on how to do that.

I wrote the post with the hope that (above all) many of the guys involved in the creation of that library or regular users may have some idea with the logs on which may be the problem, I understand that I'm doing "something special" here, but not that special at all, the problem seems to

I really need a hand over here, it's not something I'm able to workout for myself

Many thanks to anyone that has read so far and if you have something to share, any help is welcome!
 
Upvote 0

Pablo Torres

Active Member
Licensed User
Longtime User
Would it work if you upload B4JBridge and debug from the CLI ?
Unfortunately, for Java 11 there is no debug environment and no chance to run in cli mode.
Just upload the jar, run it and watch the logs are the possibilities.
 
Upvote 0

Pablo Torres

Active Member
Licensed User
Longtime User
Can you share a B4J project that works previously?
Do you upload jcore as Layer? Do you use openJDK 11 or Amazon Java Correto 11?
I have created an account but not able to get a simple function to run.
Erel hit the target with his answer in msg14, but here it goes the answers to your questions:
1) Attached is a project that works smoothly
2) No, i upload the jar in the functions sections, I choose Java 11 and then upload the jar, just that
3) I use openJDK 11 in my machine to develop and select Java 11 Correto in the language options of aws lambda

Tip: In the code section, be sure to put the name of the control right (see the image), there it goes the package name of your project followed by ".main::_responsehandler", I crashed my head for 3 days with that, nothing works until I did that change
captura.png
 

Attachments

  • CAPEventos.zip
    1.3 KB · Views: 118
Upvote 0

aeric

Expert
Licensed User
Longtime User
Where do you put this line?
B4X:
Me.As(JavaObject).RunMethod("initializeProcessGlobals", Null)

Edit: I got it. Inside the Public handle sub.

lambda.b4j:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    
End Sub

'Sub AppStart (Args() As String)
'    Log("Hello world!!!")
'End Sub

Public Sub handle (data As Object) As String
    Me.As(JavaObject).RunMethod("initializeProcessGlobals", Null)
    Log ( data )
    Return "Success"
End Sub
 

Attachments

  • lambda.zip
    522 bytes · Views: 110
Last edited:
Upvote 0
Top