Java – Spring unit test with custom provided configuration.

The following example will show how to define the custom Spring configuration for unit test with @ContextConfiguration annotation.
Full example can be found the below github.

https://github.com/nsclass/java-timezone/blob/master/src/test/java/com/ns/timezone/javatimezone/JavaTimezoneServiceApplicationTests.java

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = JavaTimezoneServiceApplicationTests.AppConfig.class)
public class JavaTimezoneServiceApplicationTests
{
	@Configuration
	static class AppConfig
	{
		@Bean
		TimezoneService timezoneService() {
			return new TimezoneService();
		}
	}

	@Autowired
	private TimezoneService timezoneService;

	@Test
	public void verifyGetAllTimezoneDisplay() {
		Flux<TimezoneService.TimezoneDisplayInfo> timezones = timezoneService.getAllTimezoneDisplay();
		int count = timezones.collectList().block().size();
		Assert.assertEquals(627, count);
	}

}
Advertisements
Posted in Java, Programming | 3 Comments

JVM – Profiling and GC analysis tool

The easiest GC log analysis web site.
https://gceasy.io/

Profiling
Adding a JVM Option +XX:+PreserveFramePointer

https://github.com/jvm-profiling-tools/perf-map-agent
https://github.com/brendangregg/FlameGraph

$ perf-java-flames [pid]

https://github.com/Netflix/flamescope

Posted in Java, Programming | Leave a comment

Simple way to generate ssh pub/private key

Very simple way to generate ssh pub/private key in Linux.


$ ssh-keygen -t rsa -P ""


Copy the content of .ssh/id_rsa.pub to .ssh/authorized_keys

Posted in Programming | Leave a comment

Spark – Cassandra connector example

Github
https://github.com/nsclass/spark-cassandra-example

object SparkCassandra {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .appName("SparkCassandra")
      .master("local")
      .config("spark.cassandra.connection.host", "127.0.0.1")
      .getOrCreate()

    // Read data as RDD
    val rdd = spark.sparkContext.cassandraTable(keyspace = "system", table = "local")
      .select("key", "cluster_name", "cql_version")

    println("Data read as RDD")
    rdd.collect()
      .foreach(row => {
        println(row.getString("key"))
        println(row.getString("cluster_name"))
        println(row.getString("cql_version"))
      })

    // Read data as DataSet (DataFrame)
    val dataset = spark.read
      .cassandraFormat(keyspace = "system", table = "local")
      .load()

    dataset.collect()
      .foreach(row => {
        println(row.getAs("key"))
        println(row.getAs("cluster_name"))
        println(row.getAs("cql_version"))
      })
  }
}
Posted in Programming, Spark | Leave a comment

Client side load balancing

Posted in Programming | Leave a comment

Kafka – Docker compose example for Kafka

Kafka docker compose example. Please set value of KAFKA_ADVERTISED_HOST_NAME for local host IP address.

version: '2.1'
  
services: 
  zookeeper:
    image: wurstmeister/zookeeper:3.4.6
    ports:
      - "2181:2181"

  kafka:
    image: wurstmeister/kafka:2.12-2.2.0
    ports:
      - '9092:9092'
    tmpfs:
      - /kafka/:rw
    depends_on:
      - zookeeper
    environment:
      KAFKA_ADVERTISED_HOST_NAME: [HOST_IPADDRESS]
      KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
Posted in kafka, Programming | Leave a comment

Time – days of clock vs Monotonic clocks

Getting days of clocks
– Linux: clock_gettime(CLOCK_REALTIME)
– Java: System.currentTimeMillis()

Getting monotonic clocks
– Linux: clock_gettime(CLOCK_MONOTONIC)
– Java: System.nanoTime()

It’s important to know difference between days of clocks and monotonic clocks because days of clocks is measured from a certain epock(Java is using 1970 Jan 1 in UTC) it can be backward when time is adjusted by NTP.
But monotonic clocks will never go back, it will guarantee to be always forward so if application want to measure the duration of time, monotonic clocks should be used.
However it’s meaningless on comparing monotonic clocks between two machines.

Posted in Programming | Leave a comment