App42 platform has various technical and business services using which app development can be done without writing a single line of backend code. However there might be a case where the app developer might need to have some server side custom code for their app. There could be multiple scenarios for example-
You want to add server side validation and or custom logic on the input data instead of on the client.
You want to make a custom service by aggregating two or more App42 services. For example aggregating Push and Storage service for custom requirement.
App logic that might get changed in future and hence you don’t want to bundle it in your app. Instead you would like to put it on the server where it can be accessed through App42 SDK/REST calls from your app.
Download sample code from GitHub.
Import the Sample Project in your Eclipse IDE.
Open Java source file MyCustomCode.java inside com.shephertz.app42.paas.customcode.sample folder and modify the execute method of it as shown below.
In the above example, if you want to rename or change the class name, you are free to do so however it should have parent package as com.shephertz.app42.paas.
Above example uses App42 Logging Service to do the logging inside your custom code. These logs would be available to you inside AppHQ console.
Custom Code deployed in our cloud runs inside a sand-boxed environment for each service. There are restrictions on using certain JAVA APIs like System, FileIO, Reflection, ByteCode injection etc. Also, your package name should start with com.shephertz.app42.paas.customcode to get it deployed on App42 cloud. If you have any requirement where you are looking to use these restricted APIs or any other third party Jar file, please write to us at email@example.com for further help.
Once you have written your custom code, you can deploy it either through ant command line or using our APIs.
To deploy your custom code through Ant script, run < ant deploy > from a command line inside your root folder of the sample project that you downloaded from above. Before running this command, modify build.properties to enter your APIKey and SecretKey for the same. Once you run the above command, you will be asked to enter the name for the custom code to be deployed on App42 Cloud. It’s default name is MyCustomCode. If you get BUILD SUCCESSFUL message after entering the name, your App is deployed on App42 Cloud. Otherwise you will get an appropriate exception message
You can also deploy your server side custom code from AppHQ console. To deploy it, your custom code class should be wrapped onside jar file. You can use any java utility to create jar file of your code. Once your jar file is ready, you can deploy it from dashboard using Custom Code:
Login to AppHQ Management Console
Click on Custom Code -> Deploy Jar as shown below by passing name and target jar file.
Once your code is deployed successfully, you should be able to see it in service listing option under Custom Code panel in dashboard as shown below.
Once your custom code service is deployed, it can be accessed through App42 SDKs available in different languages using Custom Code Service and calling runJavaCode on it. Example of Calling Custom code from Android/JAVA SDK is shown below
You can also schedule server side custom code if you require to use it as scheduled job on server side instead of calling from API. Here are the steps to schedule server side custom code from AppHQ console.
Select Custom Code -> Deploy Jar -> Select your App and Custom Code name from the list
Click on Manage Button adjacent to Custom Code name.
Fill all required details in pop up window as shown below:
JSON Body : JSON to be passed as input to your code (non-mandatory)
Date : Date for your custom code job execution
Time : Time for job execution
Time Zone : Time Zone in which you have passed the above two values i.e. date and time
Repeat Count : How many times your job should run after first execution. Pass 0 if you want to run it only one time otherwise pass increment from first execution.
Repeat Forever : Select if you want your job to be run always. This will be equivalent to infinite repeat count value.
Repeat Interval : Interval in seconds between consecutive job execution time.
Once you are done with above details, click on Schedule button.
Once your code is scheduled you should be able to see scheduled service details like next fire time, previous fire time, repeat count, and interval defined as shown below.
You can cancel your schedule job any time by clicking on Cancel Schedule button in top menu in center panel. Also, you can check scheduled job logs under Scheduling Logs option.
Watch complete video on YouTube