Skip to content

Money in Java

paulcleary edited this page Nov 10, 2015 · 2 revisions

Even though Money is built primarily with Scala, it can be used with Java applications.

Money is currently compiled to Java 1.6, so it will run in Java 1.6, 1.7, and 1.8. We will shortly be supporting Java 1.8 standard.

For Java 1.6, you must use the 2.10 variation of Money. For Java 1.7 and Java 1.8, you should use the 2.11 variant.

Getting Started

For Java 1.6, add a dependency as follows for maven:

    <dependency>
        <groupId>com.comcast.money</groupId>
        <artifactId>money-core_2.10</artifactId>
        <version>${money.version}</version>
    </dependency>

For Java 1.7 or Java 1.8, add a dependency as follows for maven:

    <dependency>
        <groupId>com.comcast.money</groupId>
        <artifactId>money-core_2.11</artifactId>
        <version>${money.version}</version>
    </dependency>

TODO: add repo info

In order to resolve this host name, you will likely need to be on the corporate network.

Also, note that the release version of the artifact will change - review the repo to get the latest version.

Core Features in Java

Starting and Stopping a new Span

It is very important to always stop your spans. Putting them in a finally block is one way to ensure they will be closed.

Spans will eventually timeout and get cleaned up; however, under heavy load, lots of spans waiting to timeout could add significant memory pressure to your application causing the dreaded OutOfMemory error.

  //JAVA EXAMPLE
  import static com.comcast.money.japi.JMoney.*;

  public void traceThisThingie() {
      try {
          startSpan("chumpie");
          ...
      finally {
          stopSpan("chumpie");
      }
  }

In Java 7+, you can use the try with resources construct as well

  //JAVA 7 EXAMPLE
  import static com.comcast.money.japi.JMoney.*;

  public void traceThisChumpie() {
      try (TraceSpan span = newSpan("chumpie")) {
          ...
      }
  }

Recording Notes

You can record Notes of the following types: String, Boolean, Long, Double. Simply use the record methods in the Java API.

  //JAVA 7 EXAMPLE
  import static com.comcast.money.japi.JMoney.*;

  public void doSomething(Long indexSize, String customerId) {
      try (TraceSpan span = newSpan("chumpie")) {
          record("index-size", indexSize);
          record("customer-id", customerId);
      }
  }

Using Timers

Timers allow you to start and stop timers in order to record the duration of some process. Timers are recorded to microsecond resolution.

The advantage of using timers is that you can start a timer in one place in your code, and stop that timer in a completely different area of your code, or on a different Thread all together!

  //JAVA 7 EXAMPLE
  import static com.comcast.money.japi.JMoney.*;

  public void doSomething(List<String> index) {
      try (TraceSpan span = newSpan("chumpie")) {
          startTimer("fun-with-money");
      }
  }

  public void finishIt() {
    stopTimer("fun-with-money");
  }

Core Features in Java

Starting and Stopping a new Span

It is very important to always stop your spans. Putting ththe followay to ensure they will be closed.

Spans will eventually timeout and get cleaned up; however, under heavy load, lots of spans waiting to timeout could add significant memory pressure to your application causing the dreaded OutOfMemory error.

  //JAVA EXAMPLE
  import static com.comcast.money.japi.JMoney.*;

  public void traceThisThingie() {
      try {
          startSpan("chumpie");
          ...
      finally {
          stopSpan("chumpie");
      }
  }

In Java 7+, you can use the try with resources construct as well

  //JAVA 7 EXAMPLE
  import static com.comcast.money.japi.JMoney.*;

  public void traceThisChumpie() {
      try (TraceSpan span = newSpan("chumpie")) {
          ...
      }
  }

Recording Notes

You can record Notes of the following types: String, Boolean, Long, Double. Simply use the record methods in the Java API.

  //JAVA 7 EXAMPLE
  import static com.comcast.money.japi.JMoney.*;

  public void doSomething(Long indexSize, String customerId) {
      try (TraceSpan span = newSpan("chumpie")) {
          record("index-size", indexSize);
          record("customer-id", customerId);
      }
  }

Using Timers

Timers allow you to start and stop timers in order to record the duration of some process. Timers are recorded to microsecond resolution.

The advantage of using timers is that you can start a timer in one place in your code, and stop that timer in a completely different area of your code, or on a different Thread all together!

  //JAVA 7 EXAMPLE
  import static com.comcast.money.japi.JMoney.*;

  public void doSomething(List<String> index) {
      try (TraceSpan span = newSpan("chumpie")) {
          startTimer("fun-with-money");
      }
  }

  public void finishIt() {
    stopTimer("fun-with-money");
  }