# Optimizing Java OpenJDK

When optimizing Java applications based on OpenJDK, typically the goal is to tune the JVM from both the point of view of cost savings and quality of service.

Please refer to the [Java OpenJDK optimization pack](https://docs.akamas.io/akamas-docs/3.2.1-1/akamas-reference/optimization-packs/java-openjdk-pack/java-openjdk-11) for the list of component types, parameters, metrics, and constraints.

### Workflows <a href="#workflow-design" id="workflow-design"></a>

#### Applying parameters <a href="#applying-parameters" id="applying-parameters"></a>

Akamas offers many operators that you can use to apply the parameters for the tuned JVM. In particular, it is suggested to use the [FileConfigurator operator](https://docs.akamas.io/akamas-docs/3.2.1-1/akamas-reference/workflow-operators/fileconfigurator-operator) to create a configuration file or inject the arguments directly in the command string using a template.

The following is an example of templatized executions string:

```
#!/bin/bash
cd "$(dirname "$0")"
java ${jvm.jvm_gcType} ${jvm.jvm_minHeapSize} ${jvm.jvm_maxHeapSize} ${jvm_newSize} ${jvm_survivorRatio} -jar renaissance.jar -r 20 --csv renaissance.csv page-rank
```

#### A typical workflow <a href="#a-typical-workflow" id="a-typical-workflow"></a>

A typical workflow to optimize a Java application can be structured in two parts:

1. **Configure the Java arguments**
   1. Generate a configuration file or a command string containing the selected JVM parameters using a [FileConfigurator operator](https://docs.akamas.io/akamas-docs/3.2.1-1/akamas-reference/workflow-operators/fileconfigurator-operator).
2. **Run the Java application**
   1. Use available [operators](https://docs.akamas.io/akamas-docs/3.2.1-1/akamas-reference/workflow-operators) to execute a performance test against the application.

Here’s an example of a typical workflow where Akamas executes the script containing the command string generated by the file configurator:

{% code lineNumbers="true" %}

```yaml
name: optimize-java-app
tasks:
  - name: Configure Parameters
    operator: FileConfigurator
    arguments:
        source:
            hostname: app.akamas.io
            username: akamas
            path: /home/akamas/app/run.sh.templ
            key: rsa-key
        target:
          hostname: app.akamas.io
          username: akamas
          path: /home/akamas/app/run.sh
          key: rsa-key

  - name: Launch Test
    operator: Executor
    arguments:
      command: bash /home/akamas/app/run.sh
      host:
        hostname: app.akamas.io
        username: akamas
        key: rsa-key
```

{% endcode %}

### Telemetry Providers <a href="#telemetry-providers" id="telemetry-providers"></a>

Akamas can access JMX metrics using the [Prometheus provider](https://docs.akamas.io/akamas-docs/3.2.1-1/integrating-akamas/integrating-telemetry-providers/prometheus-provider). This provider comes out of the box with a set of default queries to interrogate a Prometheus instance configured to fetch data from a [JMX Exporter](https://github.com/prometheus/jmx_exporter).

Here’s a configuration example for a telemetry provider instance that uses Prometheus to extract all the JMX metrics defined in this optimization pack:

{% code lineNumbers="true" %}

```yaml
provider: Prometheus
config:
  address: monitoring.akamas.io
  port: 9090
```

{% endcode %}

where the configuration of the monitored component provides the additional references as in the following snippet:

{% code lineNumbers="true" %}

```yaml
name: jvm
description: target JVM
componentType: openjdk-11
properties:
    prometheus:
        instance: jvm
        job: jmx-exporter
```

{% endcode %}

### Examples

See this [page](https://docs.akamas.io/akamas-docs/3.2.1-1/knowledge-base/optimizing-a-sample-java-openjdk-application) for an example of a study leveraging the Java OpenJDK pack.\\
