B4J Tutorial [server] Run a Server on Azure as an App Service

xulihang

Active Member
Licensed User
Since Azure supports Jetty, a B4J server app can also run on azure.

1. Search jetty in the marketplace and create a web app service.



2. Use git to clone the repository. You can find the link in "Properties".

3. Place your .jar at someplace in the repository and edit web.config.

change this:

B4X:
  arguments="-Djava.net.preferIPv4Stack=true
                             -Djetty.port=%HTTP_PLATFORM_PORT%
                             -Djetty.base="d:\home\site\wwwroot\bin\jetty-distribution-9.1.2.v20140210"
                             -jar "d:\home\site\wwwroot\bin\jetty-distribution-9.1.2.v20140210\start.jar""
to this:

B4X:
arguments="-Djava.net.preferIPv4Stack=true
                             -jar "d:\home\site\wwwroot\bin\start.jar"
                             %HTTP_PLATFORM_PORT%"
The server needs to set its port based on the argument. The b4j code:

B4X:
Sub AppStart (Args() As String)
    Dim port As Int
    port=Args(0)
    srvr.Initialize("srvr")
    srvr.AddHandler("/hello","hello",False)
    srvr.Port=port
    srvr.Start
    StartMessageLoop
End Sub
4. Then use git push to make deployment.

Here is an example: https://b4jserver.azurewebsites.net/hello

More detailed instruction by tchart: https://www.b4x.com/android/forum/threads/server-run-a-server-on-azure-as-an-app-service.98277/#post-638780
 

Attachments

Last edited:

tchart

Active Member
Licensed User
Hi xulihang, this looks awesome. Could you provide more details on how you do the git clone and jar replacement? I'm a bit stuck.
 

xulihang

Active Member
Licensed User
Site is not running
You should find your own link as shown in the picture.
无标题.png


Use the command below to clone it (install git first and replace the link with yours):

B4X:
git clone  "https://$basiccat-server:apikey@basiccat-server.scm.azurewebsites.net/"
Then do the following step 3 and step 4.
 

tchart

Active Member
Licensed User
I got mine running.

I haven't had time to write the steps up yet.

I did note that the latest version of Java they offer wasn't a JDK and app wouldn't start. I set it to the lowest 1.8.x and that worked.
 

antsnel

Member
Licensed User
I got mine running.

I haven't had time to write the steps up yet.

I did note that the latest version of Java they offer wasn't a JDK and app wouldn't start. I set it to the lowest 1.8.x and that worked.
Great! Thanks for the heads up on the JDK!
 

tchart

Active Member
Licensed User
Ok here goes.

1) I found the easiest way to start off was actually from the github site and to press the "Deploy to Azure" button at the bottom - URL is https://github.com/azure-appservice-samples/JettyTemplate

2019-01-14 19_50_30-Window.png
2) Complete the details of your app in the wizard

2019-01-14 19_51_10-Window.png

3) Wait for it to deploy. You will find the app wont actually start correctly

2019-01-14 20_00_19-Window.png

4) Under application settings adjust Java version - use 1.8.0_25
2019-01-14 20_01_13-Window.png

5) Jetty will now start correctly
2019-01-14 20_02_06-Window.png

6) Now set up your jServer project as per xulihangs instructions

2019-01-14 20_05_10-Window.png

7) Set up FTP to your site and upload the new JAR file (stop the app first). FTP credentials are under Deployment Center.

2019-01-14 20_13_36-Window.png

8) Now edit the web.config file under wwwroot

2019-01-22 19_19_51-_site_wwwroot_web.config.png


9) Now start up the app and it should be good to go

2019-01-22 19_21_15-2019-01-14 21_43_39-Window.png ‎- Photos.png
 

xulihang

Active Member
Licensed User
Very detailed step by step instruction! FTP may be a more appropriate way to deploy B4J apps than git.
 

tchart

Active Member
Licensed User
Very detailed step by step instruction! FTP may be a more appropriate way to deploy B4J apps than git.
Agree, I only use git for the template, the B4J app is deployed over FTP. I'm sure the steps could be streamlined but I haven't had another look yet .
 

tchart

Active Member
Licensed User
Just an FYI, if you deploy on the free tier for the app service that your service will be shut down when idle. This means if you have any background jobs that execute on a timer that they may not run.
 

tchart

Active Member
Licensed User
Also, I have created a template based on B4J Server which works out of the box. Workflow is the same as above but the website works straight away with a B4J server app which you can replace with your own. To update the JAR via FTP you first need to disconnect from the GitHub project in Deployment Centre - havent figured out how to do that via config yet.

https://github.com/ope-nz/b4j-server-azure-appservice-template
 

tchart

Active Member
Licensed User
I have updated the azuredeploy.json file to enable websockets by default - which work great.

I have also uploaded the source code for the azure_template project.
 
Top