B4J Question MQQTBroker and Application_Error

udg

Expert
Licensed User
Hi all,

today I installed B4J 5.00 and recompiled an "old" test of mine about using MQQTBroker as a testing broker.
I'd like the idea of placing an Application_Error log somewhere just to be alerted about release mode errors, but couldn't find the way to integrate it in my test code.
Please find below the whole "broker" code.
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
   #CommandLineArgs:
   #MergeLibraries: True
#End Region

Sub Process_Globals
   Dim broker As MqttBroker
End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   'If srvr.CurrentThreadIndex = 0 Then 'main thread
     LogError(StackTrace)
     Return False
   'Else 'handlers threads
   '   Return True
   'End If
End Sub

Sub AppStart (Args() As String)
  'record broker PID assigned on server
   Dim jo As JavaObject
   jo.InitializeStatic("java.lang.management.ManagementFactory")
   Dim pid As String = jo.RunMethodJO("getRuntimeMXBean",Null).RunMethod("getName",Null)
   File.WriteString(File.DirApp,"brokerpid.txt","EggBroker: "& pid.SubString2(0,pid.IndexOf("@")))
   'set and start the broker
   broker.Initialize("", 51212) 'first parameter is the event name. It is currently not used.
   broker.SetUserAndPassword("tstuser","tstpwd*")
   broker.Start
   StartMessageLoop 'Non-UI app
End Sub
Should I add a Server object just to be able to use Application_Error?

A different question is: where the error log is written to? Yes, I read about StdError and in Linux I will redirect StdErr to a text file, but what am I expected to do on a Windows Server where the jar is launched by double-clicking on its name? Should I write a batch for launching it by command line?

TIA

udg
 

jmon

Well-Known Member
Licensed User
but what am I expected to do on a Windows Server where the jar is launched by double-clicking on its name
you can redirect the stdin & out to a file:
https://www.b4x.com/android/forum/threads/redirect-the-output-to-a-file.65165/#content

you can also do that with AWTRobot library.

just to be alerted about release mode errors
I think you have to return "True" in your Application_Error sub, to handle the error:
https://www.b4x.com/android/forum/threads/unhandled-exceptions.77506/

this way you could save the error to a file, or save it to you database ... etc ...
 
Upvote 0

udg

Expert
Licensed User
I think you have to return "True" in your Application_Error
That was my understanding too, but I copied the code from Erel's post and he used False. BTW, have I to assume that since I don't use a JServer component than I am necessary in Main thread so "CurrentThreadIndex" is unnecessary?

Thanks for the links.

udg
 
Upvote 0
Top