1.2 Setup using Gradle

Installing Gradle

Download and install Gradle by following the installation guide for your particular operating system.

Please ensure the gradle command is added in the system path by checking with the command itself.

Example - Using gradle command on a Linux OS to check if Gradle is installed properly.

$ gradle -v
 
------------------------------------------------------------
Gradle 4.6
------------------------------------------------------------
 
Build time:   2018-02-28 13:36:36 UTC
Revision:     8fa6ce7945b640e6168488e4417f9bb96e4ab46c
 
Groovy:       2.4.12
Ant:          Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM:          1.8.0_161 (Oracle Corporation 25.161-b12)
OS:           Linux 4.4.0-121-generic amd64

Creating a Java project

With the Gradle tool installed, the next step will be to create a Java project for the Java agent. This is done using the jcreate mzsh command. For more information regarding the jcreate command, see 2.2.10 jcreate.

Container deployments

If you are deploying on containers, please read the instructions here for AWS deployments /wiki/spaces/MD83/pages/5967616 and for private container deployments /wiki/spaces/MD83/pages/5967000

Creating a project on the platform server

  1. Using the jcreate command, create a new Java project.

    Example - Using jcreate to create a new Java project

    $ mzsh mzadmin/dr jcreate /home/mzadmin/agent1 helloWorld

    The jcreate command will create a java project named helloWorld in the agent1 directory.

If the development is to be done on a separate server from the platform, the following additional steps are required to be performed.

  1. To move the directory to another server, the first step will be to compress the helloWorld directory into a compressed archive.

    Example - Compressing the Java project directory using Zip.

    $ cd /home/mzadmin/agent1
    $ zip -r helloWorld.zip helloWorld/
  2. After compressing the directory, copy and extract the zip file to a directory in server or machine that the development will be performed.

    Example - Decompressing the Java project at the destination server.

    $ mkdir agent1
    $ cd agent1
    $ mv /tmp/helloWorld.zip .
    $ unzip helloWorld.zip

Building a Java project

The following are steps to build and deploy the Java project successfully for the Java agent.

  1. Go to the Java project directory and edit the gradle.settings file.
    Modify the file to include valid entries for host, port, username and password for the Platform.

    Example - gradle.settings file.

    gradle.settings
    ext {
    //----- Platform host settings -----
    host="localhost"
    port="9000"
    username="mzadmin"
    password="dr"
    
    //----- Runtime settings -----
    syncHttpAddress="http://${host}:${port}/javaagent/sync"
    postHttpAddress="http://${host}:${port}/javaagent/post"
    }
  2. The next step will be to update the project's libraries with the latest UDRs available with the following commands:

    Example - Command to refresh the UDRs in the project library.

    $ gradle pull
    

    This command will download all the necessary java jars to the project directory. This includes any changes done to it before hand. 


  3. After running gradle pull, the following command is performed to compile and create the library files.

    Example - Command to build the library

    $ gradle clean assemble

Implementing a Java project

Once the Java project is set up and all libraries present, the implementation of the business logic can begin. Below will be an example of an implementation, where the java file HelloworldAgent.java will have env.debug("Hello World"); under the consume function block.

Example - Debugging a "Hello World" statement

Example - Debugging a "Hello World" statement

HelloworldAgent.java
package com.mediationzone.java_agent;
  
import com.digitalroute.mz.java.agents.RealtimeAgent;
import com.digitalroute.mz.java.agents.api.AgentEnv;
import com.digitalroute.mz.java.agents.api.ConfigDefinition;
import com.digitalroute.mz.java.agents.UDRInterface;
import java.util.Map;
  
public class HelloworldAgent implements RealtimeAgent {
  
    @Override
    public void configure(ConfigDefinition cfg) {
        cfg.defineOutput(byte[].class, UDRInterface.class);
        //cfg.defineOutput(Your_UDR.class);
        //cfg.defineOutput("routename", Your_UDR.class);
    }
  
    @Override
    public void deinitialize(AgentEnv env) throws Exception {
  
    }
  
    @Override
    public void init(AgentEnv env, Map<String,String> userdata) throws Exception {
  
    }
  
    @Override
    public void consume(UDRInterface input, AgentEnv env) throws Exception {
        //  try {
        //    env.debug(input);
        //
        //    Your_UDR newUdr = env.createUDR(Your_UDR.class);
        //
        //    Your_UDR udr = (Your_UDR) input;
        //  } catch (Exception e) {
        //    throw new RuntimeException(e);
        //  }
        //
        env.debug("Hello World");
        env.route(input);
    }
  
    @Override
    public void consume(byte[] input, AgentEnv env) throws Exception {
        env.debug(input);
        env.route(input);
    }
  
    @Override
    public Object getMIM(String mimName) {
        return null;
    }
}

The code will generate a debug statement with the string "Hello World" and output it to the Workflow Monitor with the Debug option enabled.

For the example workflow and java code, see 9.47.5 Java Agent Example.

For more information about the environment functions utilized by the Java agent, see 3. Java General Functions.

External third party Java libraries could also be added and used by the Java configuration code. For the detailed steps, see 1.3 Adding External Third Party Java Libraries.

Example of the Hello World debug statement for Workflow with Java agent.

Deploy a Java project

Once all changes to the codes are performed in the project directory, the changes are committed with the following command:

Example - Command to commit the changes to the java codes.

$ gradle commit