java ยท reference

Java Cool Tricks And Tips

This is a blog post where I’ll be collecting some cool Java tricks I keep forgetting about.

Logging

Enable http client / web services dumping

-Dlogging.level.org.apache.http=DEBUG
-Dlogging.level.org.springframework.ws.client.MessageTracing=TRACE

Maven

Download artifact manually

mvn -s ../Projects/rh/workspace/settings.xml org.apache.maven.plugins:maven-dependency-plugin:2.1:get  -DrepoUrl=http://repo1.maven.org/maven2/  -Dartifact=com.google.protobuf:protobuf-java:2.4.1    

Full debug

mvn -X

Sort pom

alias sortpom='mvn com.google.code.sortpom:maven-sortpom-plugin:sort -Dsort.nrOfIndentSpace=4 -Dsort.sortPlugins=groupId,artifactId -Dsort.sortDependencies=scope,artifactId -Dsort.sortProperties=true

Maven clean up after failed download

delete m2e-lastUpdated.properties files

JVM

Crashdump disassembly

Copy paste hex bytes of assembly code into this script:

echo $'.text\n' >1.s; echo "48 8b 17 74 ..." | tr ' ' "\n" | while read f; do echo ".byte 0x"$f; done >> 1.s

then run:

as 1.s
objdump --adjust-vma=0x000000006da0307a -d a.out

Concurrency

Concurrent map with lazy initialised values

Map<String, Long> lazyMap = new ConcurrentHashMap<>();

// get/init function
Long getCachedValue(String key) {
    lazyMap.computeIfAbsent(key, Util::someCreateMethodForKeyReturningLong);
}

Concurrent map with counters

Map<String, Long> counterMap = new ConcurrentHashMap<>();

// update function
void update(String key) {
    counterMap.merge(key, 1L, Long::sum);
}
Published:
comments powered by Disqus