Hi,
sorry maybe it is a silly question but... can I use an HTTPJOB in a multithread handler? im using this code in a handler:
and everithing works as expected when I use it as a singlethread handler, but when I want to use it as a multithread handler it fails, I get this error:
I made some tests with wireshark and it seems like the error is because B4J is closing the connection too early, so maybe I'm not using OkHttpUtils2 properly? httpjobs should be used only in singlethread handlers? or I'm missing something here about Handlers? ,
If someone can help me clarify these doubts, I would appreciate it very much.
sorry maybe it is a silly question but... can I use an HTTPJOB in a multithread handler? im using this code in a handler:
B4X:
'Handler class
Sub Class_Globals
Public AWS_URI_Is_Path_Style As Boolean
Public AWS_Access_Key_ID As String
Public AWS_Secret_Access_Key As String
Public AWS_Region As String
Public AWS_End_Point As String
Public AWS_S3_Bucket_Name As String
Public AWS_S3_File_Name As String
Public AWS_S3_HttpMethod As String
Public AWS_S3_Query_map As Map
Public AWS_S3_Payload() As Byte
Public AWS_S3_GMT_DateTime As Long
Public AWS_S3_OtherHeader_map As Map
Type KeyValueType(key As String, value As String)
Dim ws As WebSocket
Private aws_job As HttpJob
End Sub
Public Sub Initialize
End Sub
Sub Handle(req As ServletRequest, resp As ServletResponse)
'get the callback module from the session (multiple modules can use this handler)
Log("Helper2")
Dim callback As Object = req.GetSession.GetAttribute("file_upload_sender")
Try
Dim data As Map = req.GetMultipartData(Main.TempDir,10000000)'10MB
Dim filePart As Part = data.Get("file1")
'S3_Upload(filePart)
wait for (S3_Upload(filePart)) complete (result1 As Boolean)
Catch
Log("Catch Error")
CallSubDelayed2(callback, "FileError", LastException.Message)
resp.SendError(500, LastException.Message)
End Try
End Sub
Sub S3_Upload (filepart As Part)
AWS_URI_Is_Path_Style = True
AWS_Access_Key_ID = "0021b7c45cae0a20000000004"
AWS_Secret_Access_Key = "K0025WFMCY2jv9GJ9o3Og7FQruzJn0M"
AWS_Region = "s3.us-west-002"
AWS_End_Point = "s3.us-west-002.backblazeb2.com"
AWS_S3_Bucket_Name = "B4Xtest"
AWS_S3_File_Name = "FILE"&DateTime.Now&filepart.SubmittedFilename
AWS_S3_HttpMethod = "PUT"
AWS_S3_Query_map.Initialize
AWS_S3_Query_map.Clear
Private wrk_in As InputStream
wrk_in = File.OpenInput("",filepart.TempFile)
Private wrk_out As OutputStream
wrk_out.InitializeToBytesArray(1000)
File.Copy2(wrk_in, wrk_out)
Log("aqui?")
wrk_in.Close
wrk_out.Close
File.Delete("",filepart.TempFile)
AWS_S3_Payload = wrk_out.ToBytesArray
AWS_S3_GMT_DateTime = DateTime.Now
AWS_S3_OtherHeader_map.Initialize
AWS_S3_OtherHeader_map.Clear
AWS_S3_OtherHeader_map.Put("Content-Type", "application/x-www-form-urlencoded")
AWS_S3_OtherHeader_map.Put("Content-Length", NumberFormat2(AWS_S3_Payload.Length, 1, 0, 0, False))
AWS_S3_OtherHeader_map.Put("x-amz-meta-author", "B4X")
AWS_S3_OtherHeader_map.Put("Expect", "100-continue")
Log(URI)
'Set up httpjob
aws_job.Initialize("AWS_job2", Me)
aws_job.PutBytes(URI, AWS_S3_Payload)
'Retrieve full header map
Private wk_hdr_map As Map
wk_hdr_map.Initialize
wk_hdr_map = FullHeaderMap
Private wrk_ptr As Int
'For each key, value pair of full header map...
For wrk_ptr = 0 To wk_hdr_map.Size - 1
'If not host header...
If wk_hdr_map.GetKeyAt(wrk_ptr) <> "host" Then
'Add it to httpjob
aws_job.GetRequest.SetHeader(wk_hdr_map.GetKeyAt(wrk_ptr), wk_hdr_map.GetValueAt(wrk_ptr))
Log(wk_hdr_map.GetKeyAt(wrk_ptr) & ", " & wk_hdr_map.GetValueAt(wrk_ptr))
End If
Next
Log(Authorization)
'Add Authorization header to httpjob
'AWS_job.GetRequest.SetContentType("application/x-www-form-urlencoded")
aws_job.GetRequest.SetHeader("Authorization", Authorization)
Log("start loop")
StartMessageLoop
End Sub
Sub JobDone(Job As HttpJob)
Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
If Job.Success = True Then
Select Job.JobName
Case "AWS_job"
'Set up map to hold XML response
Private XML_response_map As Map
XML_response_map.Initialize
'Suck XML response into XML_response_map
Private xm As Xml2Map
xm.Initialize
XML_response_map = xm.Parse(Job.GetString)
Log("XML: "&XML_response_map)
Log("Job: "&Job.GetString)
'Convert XML response to JSON
Private JSON_response As JSONGenerator
JSON_response.Initialize(XML_response_map)
'Log JSON resonse
Log(JSON_response.ToPrettyString(4))
Case "AWS_job2"
Log(">>>>>>>>>>>>>>>>>>>Uploaded<<<<<<<<<<<<<<<<<<<<")
'Result.SetText("UPLOADED!")
End Select
Else
Log("Error: " & Job.ErrorMessage)
'Result.SetText("Error: " & Job.ErrorMessage)
End If
Job.Release
'ws.Flush
StopMessageLoop
End Sub
and everithing works as expected when I use it as a singlethread handler, but when I want to use it as a multithread handler it fails, I get this error:
B4X:
javax.net.ssl.SSLProtocolException: Connection reset by peer: socket write error
I made some tests with wireshark and it seems like the error is because B4J is closing the connection too early, so maybe I'm not using OkHttpUtils2 properly? httpjobs should be used only in singlethread handlers? or I'm missing something here about Handlers? ,
If someone can help me clarify these doubts, I would appreciate it very much.