JavaOne – Streams in JDK 8

“Streams in Java 8 – The good, the bad & the ugly”

Speaker: Simon Ritter & Stuart Marks
[Simon has the Twtter handle @speakjava; very cool]

For more blog posts from JavaOne, see the table of contents


Need to think differently. We are used to imperative programming with loops and variables.

Dealing wih exceptions
ugly code – three lines of code and hard to tell what it does

Problems:

  • looks like Perl
  • returns null (vs Optional or empty string)
  • split is called twice so wasted work
  • skipped URLDecoder.decode() because didn’t want to deal with a checked exception – but lost functionality. Problem caused by a missing API in Java so have to use decode.

Better approach:

  • use a method with a try/catch block; call that method from the stream
  • use Map.entry to simulate a tuple
  • Use single char (vs regex) in split. If only pass one character to split, far faster
  • split() is overloaded to take a numeric limit to how many are returned

Imperative streams
inside the for each is a print, and if statement and a LongAdder variable (good for frequent writes and infrequent reads)

then refactored to use mapToInt, a println and an if statement and a local variable. more complicated and still not functional

then switched to peek and no variabe but still an if statement (well a ternary)

finally switched to use a filter and count instead of sum

still not 100% functional because println is a side effect. ok for debugging

[good showing evolution to get functional]

Problems

  • Easy to misuse forEach() because feels familiar. But easily leads to side effects
  • Imperative thinking “for each of these I want to..”
  • Pause to consider if should use for each

Mixing internal and external iteration
for loop running 12 times and then getting data for each month with filter checking Month.of(x) – doesn’t work because x isn’t effectively final

“solve” effectively final by setting to different interim variable

IntStream.range(0,12).forEach – uses internal iteration but forEach. Marginaly better as don’t need interim variable

Instead return a nested map of Month to Map with nested grouping by so only need one iteration – the data stream

Problems

  • Going through data.stream() 12 times
  • forEach cheat
  • array not right data structure; it’s really a map of month to value

Hands on lab question
reduce (“”, (a,b) -> a+b) – works but inefficient because String concatenation

reduce(a,b) -> sb.append(b) – fails because ignores the first letter.

next attempt uses an if statement in reduce

then tried a custom collector. works but more complicated than necessary
Collector.of(StringBuilder::new, StringBuilder::append, StringBuilder::append, StringBuilder::toString

or just use Collectors.joining()

Problems

  • If not using a parameter, it is probably wrong
  • Side effects
  • if stateent version not associative so would fail when run in parallel

Misc

  • can’t use same stream multiple times
  • method references are slightly more efficient than lambdas because lambda gets added into a method in bytecode. Saves a level of indirection by using method reference. But only slightly
  • Calling .sorted() multiple times vs chaining comparing.thenComparing – the later is better [also works because preserves sort :)]
  • parallel streams do more work. might or might not complete faster. uses fork-join pool. number of threads defaults to number of CPUs. In Java9, this is # CPUs for container. On Jaa 8, it was for physical machine
  • Nested parallel streams is bad idea because using same threads so performance is worse. Can create ForkJoinPool if must. Buyer beware; this is an implementation specific behavior and tied to the profile of the machine you write it for.

My take: Fun start to he morning. I like that they covered common things in an entertaining way and not common things. Something to learn for everyone!

JavaOne – Maven BOF

“Maven 5 BOF”

Speaker: Brian Fox, Manfred Moser & Robert Scholte

For more blog posts from JavaOne, see the table of contents


[I was late because we talked more about JUnit 5 after the BOF]

Only 26.4% of Maven Central traffic is from Maven. Nothing else is more than 10% though; not even Ivy or Gradle

Some projects don’t have snapshots; instead every commit is a release

Talked about version ranges. Depends on proximity to your project rather than the latest version. Important to clean up pom dependencies before Java 9 so not in module path. Use Maven dependency plugin (analyze) to find unused ones. Make sure to use latest version of depedency plugin.

Maven won’t generate module descriptor. Different purpose. Not all modules are dependencies. More info in module descriptor. What to export is a decision that needs to be decided by developer. jdeps can generate a rough descriptor to get started based on binaries.

Can have .mvn file inside projects with preferences startig in Maven 3.3.5. For example, you can specify to provide more memory.

Shouldn’t be issues going from 3.3.5 to 3.5.9

Maven (dependency) resolver is now a standalone project

JavaOne – Docker, Kubernetes & Open Shift

“Project Jigsaw – Integration with Tools”

Speaker: Marek Jelen & Diogenes Rettori

Sponsored session by RedHat [I didn’t know they had sponsored sessions; I asked if they applied to JavaOne or Oracle Open World and they said neither; they paid. I was curious why they were JavaOne and yet had laptops provided by Oracle. That explains why three sessions of this class were offered.]

For more blog posts from JavaOne, see the table of contents


Can do self paced tutorials in cloud install at https://learn.openshift.com

[Nice assortment of shipping container pictures on the screen as he reviews what Docker/Kubernetes are for and why this matters. A bit of preaching to the choir here…]

“Very diverse community of contributors” The top six contributors are all from Google and RedHat. (except one who move to Microsoft) [sounds more like a strong partnership than a diverse community]

Open Shift and Kubernetes

  • OpenShift is an enterprise version of Kubernetes with extra stuff.
  • OpenShift 3.6 in lab
  • Can use kubectl with Open Shift cluster
  • OpenShift sometimes uses own version of Kubernetes objects and wrap others
  • RHEL (red hat enterprise linux) has master and nodes.
  • Nodes used to be called minions.
  • Works with cloud and non-cloud deployments
  • OpenShift adds Developer CLI,, Admin CLI, web console, REST APIs, creating an image from source, authentication and users/groups from LDAP, Also wrap namespace in a project and more

Workshop

bit.ly/javaoneworkshop

http://content-workshop.apps.javaone3.gce.pixy.io/index.html#/workshop/training/module/install

I’ve read about and played with Kubernetes. I didn’t realize OpenShift was Kubernetes with different commands.

Concepts from lab

  • pod – smallest deployable unit
  • service – group of pods
  • replication controllers – ensure correct number of pods exist
  • deployment configuration – how should be configured
  • horizonal pod auto scaler – monitors CPU and scales. See here
  • route – so external clients can access
  • service account – non-user account, get one by default

Commands in lab

  • oc login url – to login
  • oc get x – basic info
  • oc get x -o yaml – more detail in yaml
  • oc describe service x – even more detail
  • oc scale –replicas=x y – or can click up arrow in UI
  • oc delete pod x – to delete
  • oc expose service x – to create route
  • oc logs x – view logs
  • oc policy add-role-to-user view -z default – grant access to view for default user
  • oc policy add-role-to-user view user1 – grant access to view for user1

In UI

  • Add project > docker
  • Change number of replicas with up/down arrows once expand overview
  • View logs by clicking “…” on overview
  • Add project > catalog > python – use source to image so can go straight from github source code

Real world

  • 18% adults have anxiety problems
  • “not all anxiety is bad” depends on the task; issue on more complex/unfamiliar/difficult tasks
  • 20% of downtime is caused by tech failure or natural disasters. Rest caused by people
  • batch size – amount of features in a release

A/B Deployment demo

  • installed Node.JS app directly from github
  • then created a separate app from github. (so now have two apps in open shift)
  • edit route in open shift console to divide traffic between the two services. can choose percentages/weights
  • changed RAM; did rolling deployment so old one stays up until new one is ready

My take: We immediately had a problem because the laptop was setup differently than the lab; odd. not a big deal to type ./oc instead of oc. But that seems easy for them to have fixed in the VM. then the project we were supposed to switch to we were already on. then it says to login with your email vs the name we were given. I’ll stop commenting on the differences now, but this clearly wasn’t written for or tested for JavaOne. It even refers to “in the morning session” for how you find out what a pod is. I asked and they said this was written to be a full day of training. So glad that I’ve already read about Kubernetes and was able to follow it anyway. I like when they showed how to do the same task in both the UI and at the command line

[I did 9/12 of the labs. It wasn’t that they were bad or long. I was having trouble focusing. It’s 100% self paced and I wasn’t asking any questions. Or learning much. A small percentage of the lab is the UI and the rest I’ve done already with raw Kubernetes. So decided to use the remaining time to check twitter posts about other parts of the conference. I didn’t leave because the last 20 minutes are a demo of something I haven’t seen so wanted to see that.]