B4J Question [ABMaterial] Best place to put a Timer to run every 1 hour [Solved]

Anser

Well-Known Member
Licensed User
Longtime User
Hi,

I have a requirement of Calling a MySQL stored procedure every 1 hour. The Stored Procedure basically updates few tables.

I was thinking of writing a new B4J app to get this done. Then the idea got into the mind that as I already have an ABMaterial application running on the same server, then why not utilize the same app to get this done, so that I can avoid running multiple B4J applications on the same server.

Can I get some advice on this ? Where should I put this timer and routine ?. This has nothing to do with the pages available on the ABMaterial application. It uses the same database that the ABMaterial Application uses.

Whenever I start the ABMaterial application the timer should call a sub every one hour. This should be irrespective of the users accessing the application. Even if no one is using the ABM application, the timer should call the routine every one hour. This is in no way related with the users accessing the webpages in the ABM application.

For Eg in the Main() before the
B4X:
' start the server
myApp.StartServer(srvr, "srvr", port)
StartMessageLoop

OR after the above code ?
Or inside the ABMApplcation Module ? If yes, then at which point ?

Any advice will be appreciated.

Thanks
 

moster67

Expert
Licensed User
Longtime User
Just create a new Class-module and do something like this:

B4X:
'in Main add you Class-module as a Backgroundworker
Sub AppStart (Args() As String)
    srvr.Initialize("srvr")
    srvr.Port = 54021
    srvr.AddBackgroundWorker(NAME OF YOUR CLASS MODULE)
    srvr.Start
    StartMessageLoop
End Sub


'Class module
Sub Class_Globals
    Private timer1 As Timer
           
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    timer1.Initialize("timer1", 5 * DateTime.TicksPerMinute)
    timer1.Enabled = True
    StartMessageLoop '<- don't forget!
End Sub

Sub Timer1_Tick
    'do the work required
       
End Sub
 
Upvote 0

Anser

Well-Known Member
Licensed User
Longtime User
I already have a srvr in the app. Do I have to use another srvr for this srvr.AddBackgroundWorker OR can I use the same srvr object that is used for ABM
Here is the existing code of Main

B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True    
    #AdditionalJar: mysql-connector-java-5.1.44-bin  
#End Region

Sub Process_Globals
    Public srvr As Server 'Should there be another Server variable to run the background task ?
    Dim ABM As ABMaterial 'ignore
    Dim port As Int = 5100
End Sub

Sub AppStart (Args() As String)
   
    DBM.InitializeMySQL("jdbc:mysql://MyIP/MyDatabase?characterEncoding=utf8","MyUserName","MyPassword",100)

    ' Build the Theme
    ABMShared.BuildTheme("mytheme")  
   
    ' create the app
    Dim myApp As ABMApplication
    myApp.Initialize
       
    ' create the required pages
    Dim myPage As DashboardReport
    myPage.Initialize      
   
    ' add the pages to the app
    myApp.AddPage(myPage.page)

    ' Can I use the above srvr itself OR Do I need another Server Object ?
    ' If yes, then should I make the call srvr.AddBackgroundWorker before  myApp.StartServer(srvr, "srvr", port)  ?
    ' srvr.AddBackgroundWorker(NAME OF YOUR CLASS MODULE)
    ' Is here the right point to call srvr.AddBackgroundWorker ?
   
    ' start the server
    myApp.StartServer(srvr, "srvr", port)  
    StartMessageLoop

End Sub

Thanks
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
Did you try as I suggested?
I don't see why ABMaterial should change anything...

Use the same server and make sure to add the backgroundworker before starting the server in the AppStart() sub as I showed you in my previous post

B4X:
'other code...
srvr.AddBackgroundWorker(NAME OF YOUR CLASS MODULE)
'other code
 
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
A possible alternative solution is to create an Event in MySQL direct with something like this:

B4X:
CREATE
    DEFINER = 'anyserveruser_6'@'%'
EVENT ListingSystem2.event1
    ON SCHEDULE EVERY '1' HOUR
    STARTS CURRENT_TIMESTAMP
    ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
    DO
BEGIN
Select something from my table and update a flag or whatever I desire or run a procedure
END;

ALTER EVENT ListingSystem2.event1
    ENABLE

More information here: http://www.mysqltutorial.org/mysql-triggers/working-mysql-scheduled-event/

My apologies for being disruptive.
 
Upvote 0

Anser

Well-Known Member
Licensed User
Longtime User
A possible alternative solution is to create an Event in MySQL direct with something like this:
Thank you very much. Exactly what I was looking for. I never knew this was possible from MySQL itself, hence I though of using B4J for that.
 
Upvote 0
Top