so, you wanna migrate to java 9?

28
So, you wanna migrate to Java9? by Tomek Adamczewski 13/11/2017

Upload: tomek-adamczewki

Post on 21-Jan-2018

58 views

Category:

Software


0 download

TRANSCRIPT

Page 1: So, you wanna migrate to Java 9?

So, you wannamigrate to Java9?

by Tomek Adamczewski

13/11/2017

Page 2: So, you wanna migrate to Java 9?

Where are we?

2

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 3: So, you wanna migrate to Java 9?

Cool features of Java 9

• Project Jigsaw• JShell• Enhanced Collections framework• Enhanced @Deprecated• Project Coin improvements• Support for cgroup limits (think: docker)• G1GC improvements• Changes to JDK Release Model

3

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 4: So, you wanna migrate to Java 9?

“Think trains, not limousines”

4

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 5: So, you wanna migrate to Java 9?

5

Con

fiden

tial /

Res

trict

ed /

Publ

ic

Pres

enta

tion

or p

art t

itle

13/1

1/20

17Definition of Done

• Code compiles• New features merged to the master branch• All tests pass• The task is documented and closed

Page 6: So, you wanna migrate to Java 9?

16

CodeCompiles

jig·saw [jig-saw] n.a complicated problem consisting of many different parts

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 7: So, you wanna migrate to Java 9?

There are no silver bullets

A lot of libraries or plugins are not compatible with Java 9 yet

7

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 8: So, you wanna migrate to Java 9?

Useful tools

$ jdeps --jdk-internals -R --class-path ‘lib/*’ guava-HEAD-jre-SNAPSHOT.jarsplit package: javax.annotation [jrt:/java.xml.ws.annotation, lib/jsr305-1.3.9.jar]

guava-HEAD-jre-SNAPSHOT.jar -> jdk.unsupportedcom.google.common.cache.Striped64 -> sun.misc.Unsafe

JDK internal API (jdk.unsupported)com.google.common.cache.Striped64$1 -> sun.misc.Unsafe

JDK internal API (jdk.unsupported)

(…)

JDK Internal API Suggested Replacement---------------- ---------------------sun.misc.Unsafe See http://openjdk.java.net/jeps/260

8

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 9: So, you wanna migrate to Java 9?

…and hacks

<artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version> <configuration>

- <source>1.8</source> - <target>1.8</target> + <source>9</source> + <target>9</target> + <compilerArgs> + <arg>--add-exports</arg> + <arg>java.base/sun.security.jca=ALL-UNNAMED</arg>+ </compilerArgs>

</configuration>

9

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 10: So, you wanna migrate to Java 9?

…and hacks

java① --add-opens java.base/java.lang=ALL-UNNAMED② --add-modules java.xml.ws.annotation③ --patch-module java.xml.ws.annotation=jsr305-3.0.2.jar

--class-path $dependencies-jar $appjar

10

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 11: So, you wanna migrate to Java 9?

211

Code Compiles

New keyword

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 12: So, you wanna migrate to Java 9?

JEP 302: Lambda Leftovers

Treatment of underscores

In many languages, it is common to use an underscore (_) to denote an unnamed lambda parameter (and similarly for method and exception parameters):

(…)Phase 2 came in Java 9, when this warning became an error.

12

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

BiFunction<Integer, String, String> biss = (i, _) -> String.valueOf(i);

Page 13: So, you wanna migrate to Java 9?

Sonar to the rescue!™

13

Con

fiden

tial /

Res

trict

ed /

Publ

ic

Pres

enta

tion

or p

art t

itle

13/1

1/20

17

Page 14: So, you wanna migrate to Java 9?

Yet another tool to cope with?

14

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 15: So, you wanna migrate to Java 9?

…not really!

15

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 16: So, you wanna migrate to Java 9?

316

New feature merged to the master branch

Things might fail or they might not fail

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 17: So, you wanna migrate to Java 9?

Enhanced Collections framework

static <K,V> Map<K,V> of()static <K,V> Map<K,V> of(K k1, V v1)static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2)static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)

static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)

static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)

17

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Map<String, Integer> fooBar = Map.ofEntries(entry("foo", 1), entry("bar", 2),

// (...) entry("baz", 42));

Page 18: So, you wanna migrate to Java 9?

We’ve all done it before, didn’t we?

@SafeVarargspublic static <K, V> Map<K, V> map(Entry<K, ? extends V>... entries) {

return stream(entries).filter(Objects::nonNull).collect(toMap(Entry::getKey, Entry::getValue));

}

18

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 19: So, you wanna migrate to Java 9?

private static final Map<String, ErrorReason> ERROR_REASON_MAPPER =map(

entry("1001", Format_Incorrect), entry("1002", Format_Expiry_Date_Incorrect), // 95 more entries entry(”6027", Entity_Already_Active));

19

Case studyC

onfid

entia

l / R

estri

cted

/ Pu

blic

Pres

enta

tion

or p

art t

itle

13/1

1/20

17

----------------------------------BUILD SUCCESS----------------------------------Total time: 01:08 min (Wall Clock)

Page 20: So, you wanna migrate to Java 9?

20

Case studyC

onfid

entia

l / R

estri

cted

/ Pu

blic

Pres

enta

tion

or p

art t

itle

13/1

1/20

17

private static final Map<String, ErrorReason> ERROR_REASON_MAPPER =MapUtils.<String, ErrorReason>map( //explicit generics improve compile time by ~50s entry("1001", Format_Incorrect), entry("1002", Format_Expiry_Date_Incorrect), // 95 more entries entry(”6027", Entity_Already_Active));

----------------------------------BUILD SUCCESS----------------------------------Total time: 10.556 s (Wall Clock)

Page 21: So, you wanna migrate to Java 9?

Jenkins build-timeout plugin

21

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Possible strategies

• Absolute• Deadline• Elastic• Likely stuck• No Activity

Possible actions

• Abort• Fail• Abort and restart• Write build description

Page 22: So, you wanna migrate to Java 9?

422

All tests pass

Things might fail…but they also might not fail

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 23: So, you wanna migrate to Java 9?

OptUtils

/** @return ofNullable(t).map(provider).orElseGet(supplier) */public static <T, R> R optGet(

T t,Function<T, R> provider,Supplier<R> supplier) {

return ofNullable(t).map(provider).orElseGet(supplier);}

23

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 24: So, you wanna migrate to Java 9?

You can “unit” test your performance

@Testpublic void testPerf() throws Exception {

Result result = benchmark(4, 1000,(thread, pass) -> singlePass(thread, pass));

LOG.info(Table.print(result));

assertExecTimes(result);}

24

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 25: So, you wanna migrate to Java 9?

Ugly? But it’s up to 10x faster…

/** @return ofNullable(t).map(provider).orElseGet(supplier) */public static <T, R> R optGet(

T t,Function<T, R> provider,Supplier<R> supplier) {

return t == null ?supplier.get() :optGet(provider.apply(t), supplier);

}

/** @return ofNullable(t).orElseGet(supplier) */public static <T> T optGet(T t, Supplier<T> supplier) {

return t == null ? supplier.get() : t;}

25

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 26: So, you wanna migrate to Java 9?

526

The task is documented and closed

In case you just woke up…

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 27: So, you wanna migrate to Java 9?

Brace yourself

Java WILL be updated every 6 months. Make sure you’re armed and ready

27

Con

fiden

tial /

Res

trict

ed /

Publ

icPr

esen

tatio

n or

par

t titl

e13

/11/

2017

Page 28: So, you wanna migrate to Java 9?

CONTACT

[email protected]