Skip to content

bernardbaker/java.time.zone.converter.microservice

Repository files navigation

Project: Time Zone Converter Microservice

Overview: A gRPC service where a client sends a request with:

  1. A timestamp (in UTC or any other timezone)

  2. A target timezone

The service responds with the equivalent timestamp in the target timezone.


Features:

  1. Proto Definition: Define a .proto file with:

    • A ConvertTimeRequest message containing the timestamp and target timezone.

    • A ConvertTimeResponse message containing the converted timestamp.

    • A TimeZoneConverter service with one method, ConvertTime.

  2. Server Implementation: Implement the TimeZoneConverter service in Golang:

    • Use the time and github.com/rickar/cal libraries for timezone conversions.

    • Handle basic errors (e.g., invalid timezones).

  3. Client Implementation: Write a simple Golang client that:

    • Accepts input for timestamp and timezone via command line or hardcoded values.

    • Calls the gRPC server and prints the converted timestamp.


Explanation

  1. Parsing the Time:

    • The input time (2024-12-04T15:00:00Z) is in UTC, so it’s parsed using time.Parse with the time.RFC3339 layout.

    • The result is a time.Time object in the UTC time zone.

  2. Loading the Target Time Zone:

    • Use time.LoadLocation with the IANA time zone name (e.g., America/New_York).
  3. Converting Time:

    • The In(location) method converts the time to the desired time zone.
  4. Output:

    • Print both the original UTC time and the converted time for verification.

Testing

Run Java service/tests (plain gRPC + Maven):

cd java.time.zone.converter.microservice
./mvnw test
cd java.time.zone.converter.microservice
./mvnw exec:java

Monitor with Java Mission Control (JMC):

The service port (8080) is for gRPC traffic. JDK Mission Control connects through JMX, so expose a separate JMX port (for example, 9010).

cd java.time.zone.converter.microservice
./mvnw exec:java \
  -Dexec.jvmArgs="-Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9010 -Dcom.sun.management.jmxremote.rmi.port=9010 -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=127.0.0.1"

Then in Java Mission Control:

  1. Open JMC and look for the running local JVM in the JVM Browser.
  2. If it does not appear automatically, create a JMX connection to 127.0.0.1:9010.
  3. Use the Overview, Memory, Threads, and Flight Recorder views to monitor behavior.

If you run the service in Docker, publish both ports, e.g. -p 8080:8080 -p 9010:9010, and ensure java.rmi.server.hostname is reachable from JMC.

Simulate heavy traffic for JMC:

Use the included script to generate concurrent gRPC requests and create visible load in Java Mission Control.

cd java.time.zone.converter.microservice
./simulate_traffic.sh

Optional tuning (defaults shown):

  • DURATION_SECONDS=300: longer run for sustained high traffic.
  • CONCURRENCY=128: requests fired in parallel per cycle.
  • TZ_MODE=multi: randomizes timezones to simulate globally distributed traffic (single uses one timezone).
  • TZ_POOL=...: comma-separated IANA timezone list used when TZ_MODE=multi.
  • GRPCURL_MAX_TIME=2: per-request timeout (seconds) to prevent hung clients under extreme load.
  • TARGET_TZ=America/New_York: timezone value sent in requests.
  • HOST=localhost:8080: service endpoint.

Examples:

cd java.time.zone.converter.microservice
DURATION_SECONDS=600 CONCURRENCY=192 TZ_MODE=multi ./simulate_traffic.sh
cd java.time.zone.converter.microservice
# Spike test: very high parallelism for a short window
DURATION_SECONDS=120 CONCURRENCY=512 GRPCURL_MAX_TIME=1 TZ_MODE=multi ./simulate_traffic.sh
cd java.time.zone.converter.microservice
# Soak test: lower but sustained load
DURATION_SECONDS=3600 CONCURRENCY=96 GRPCURL_MAX_TIME=3 TZ_MODE=multi ./simulate_traffic.sh

Output a simulated traffic graph over time:

Use this helper script to print an ASCII graph of simulated requests-per-second (RPS) patterns.

cd java.time.zone.converter.microservice
./simulate_traffic_graph.sh

Profiles:

  • PROFILE=steady: mostly flat with light oscillation.
  • PROFILE=burst: periodic short high-traffic bursts.
  • PROFILE=spike: a few sharp spikes over baseline.
  • PROFILE=soak: gradual ramp and long plateau.

Examples:

PROFILE=spike DURATION_SECONDS=300 STEP_SECONDS=5 BASE_RPS=1000 PEAK_RPS=12000 ./simulate_traffic_graph.sh
PROFILE=soak DURATION_SECONDS=1800 STEP_SECONDS=10 BASE_RPS=800 PEAK_RPS=8000 ./simulate_traffic_graph.sh

Example grpcurl Command with .proto File:

grpcurl -plaintext \
  -import-path ./java.time.zone.converter.microservice/proto \
  -proto api.proto \
  -d '{"timestamp": "2024-12-04T15:00:00Z", "target_timezone": "America/New_York"}' \
  localhost:8080 \
  timezone.TimeZoneConverter/ConvertTime

Explanation:

-import-path ./java.time.zone.converter.microservice/proto: Specifies the proto directory for the Java service copy.

-proto api.proto: Specifies the .proto file that contains the service definition.

-d: Specifies the JSON payload, as in the previous example.

localhost:8080: Replace with your actual server address.

timezone.TimeZoneConverter/ConvertTime: Specifies the full service and method name, as per your .proto file.

About

A Java based micro service which converts timestamps/timezones.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors