B4J Question B4J+Jetty clarifications

zak

Member
Licensed User
Longtime User
Hello,
I have some knowledge in php but would like to try b4j+jetty on vps. But I would like to know how to implement few features:
1) in CPanel I could specify 'cron jobs' to trigger particular php files at specific times. How to do it when dealing with a single jar file generated by b4j?
2) how to send emails from b4j using directly the server provided emails?
3) from b4a i could send a string to a specific php file using HttpJob's PostString. Again how to do it when dealing with a single jar?
Thanks
 

OliverA

Expert
Licensed User
Longtime User
using directly the server provided emails?
What does this mean?
How to do it when dealing with a single jar file generated by b4j?
You do not necessarily have to have one jar that does it all. You could create different B4J projects per task and therefore have one .jar file per task/sub-group of tasks.
 
  • Like
Reactions: zak
Upvote 0

OliverA

Expert
Licensed User
Longtime User
How are you currently checking your email in PHP?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
It may be as simple as
B4X:
   Dim mySMTP as SMTP
   'Assumption: mail server on same machine as .jar file
   mySMTP.Initialize("127.0.0.1", 25,"","","SMTP")
   mySMTP.To.Add("someone@gmail.com")
   mySMTP.Subject = "This is a test e-mail"
   mySMTP.Body = "This is just a filler. This is the body of the e-mail"
   Wait For (mySMTP.Send) SMTP_MessageSent(Success As Boolean)
   Log(Success)
Note: You have to check (include) the jNet library under the Library Manager. SSL/TLS and authentication will add some more code to this.
 
Upvote 0

Don Oso

Active Member
Licensed User
Longtime User
The difference between PHP and B4j , in b4j all the "scripts" are inside the app all your site or app is encapsulated in one .jar , you can use Cron to launch b4j apps, but its a weird solution, because you can "schedule" some task inside the app , B4j Apps are process running in your server or desktop computer, no one time execution process like PHP
 
  • Like
Reactions: zak
Upvote 0

zak

Member
Licensed User
Longtime User
I will need to test OliverA email script and see for myself.
I got ur point Don Oso, it looks plausible.
 
Upvote 0

mrred128

Active Member
Licensed User
Longtime User
A web server (Service object) is just one chunk of code in your program. There are no real limits on the other things that can be don in the same program. As mentioned, there are timer objects that can be used to trigger all sorts of events.

I currently have two servers running in tandem behind a load balancer. I use "watch files" to kill the process, update the jar, etc. No limits to the imagination to implement external control that way.

My server is launched by a looping script

B4X:
#!/bin/bash

cd /usr/local/WebServer

rm -f STOPPED

while [ 1 -eq 1 ]; do
        # check to end
        if test -f off; then
                echo Finished
                touch STOPPED
                rm -f off
                exit 0
        fi
        # check for restart
        if test -f drop; then
                echo Restart
                rm -f drop
                sleep 10
        fi
        # check for update
        if test -f BaseAppServer.jar; then
                echo Update applied
        fi
        # check for update
        if test -f BaseAppServer.jar; then
                echo Update applied
                rm -f server.bak
                mv server.jar server.bak
                mv BaseAppServer.jar server.jar
        fi
        # rotate console log
        cd logs
        rm -f console.log.7
        mv console.log.6 console.log.7
        mv console.log.5 console.log.6
        mv console.log.4 console.log.5
        mv console.log.3 console.log.4
        mv console.log.2 console.log.3
        mv console.log.1 console.log.2
        mv console.log console.log.1
        cd ..
        #launch server
        java `cat javaopts` -jar server.jar 1> logs/console.log 2>&1
done

And the java timer code is....

B4X:
'
' No pipes so we use watch files for external control
'
Sub WFT_Tick
    wft.Enabled = False
    ' look for watch files

    If (File.Exists(var.Get("home"),"BaseAppServer.jar")) Then Fin

    If (File.Exists(var.Get("home"),"off")) Then Fin
    If (File.Exists(var.Get("home"),"drop")) Then Fin
    If (File.Exists(var.Get("home"),"tick")) Then
        Try
            File.Delete(var.Get("home"),"tick")
            Dim F As OutputStream = File.OpenOutput(var.Get("home"),"tock",False)
            F.Close
        Catch
            Log("Tick/Tock file error")
        End Try
    End If
    If DoCycle = 0 Then Fin ' Keep the code fresh and recycle app
    DoCycle = DoCycle - 1
    wft.Enabled = True
End Sub

public Sub Fin
    var.Close
    Log("")
    ExitApplication
End Sub

So the functions I implement externally....

BaseAppServer.jar - I am upgrading the server and by dropping the new jar in the base directory, the shell script runs this one instead. It backs up the old file too.
off - trun the server off.
drop - just reload the server jar cold.
tick - creates a file called tock. It's used to test the server on being alive.

Like I said, you can use external files (this as a model) to do anything your imagination desires.
 
Last edited:
  • Like
Reactions: zak
Upvote 0
Top