Real World Java 9 by Trisha Gee

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
very good you play do it make sure good morning everyone welcome you're the most organized audience I've ever seen you like shuffled out made space for everyone using up all the seats it's fantastic I've never seen that before and I've been to a bunch of conferences well done and yes hi I'm Tricia I mean want to serve you when I know about Java and I intend home this isn't alarming at all and I'm a technical advocate for JetBrains I work mostly with IntelliJ IDEA anyone using IntelliJ IDEA all of you I love you all your fantastic an eclipse users and so sorry for you and I also work with up source the code review tool but most of my job is kind of doing and research into what's coming in Java or what's kind of currently interesting in Java and then say going out how to apply it to to kind of your day job basically because I was a Java program for about 20 years and that's quite frightening and and you know people always tell you about new and exciting stuff but that's not so relevant to what you're doing in the day job unless someone makes it more applicable to you so that's kind of how I see my job if you like so today we're going to talk about real world Java 9 and which is great because Java 9 isn't out yet what I really want to cover is a little bit of jigsaw in action and who went to one or both of Marc's sessions at the conference that's great because I don't want to repeat a lot of the stuff that he said I just want to give us a high-level look at using jigsaw I'm going to cover a bit about reactive programming in Java 9 of I'll hide mark is in my audience please don't shout at me if I get it wrong and and I also what I really really want to do and hopefully I'll have time is at the end a good chunk of time we'll be going over all the other features in Java 9 well not all of them but the ones that I think are going to be relevant to you in your day job so they're kind of interesting stuff to others as developers firstly let's step back and take a look at why we care about Java 9 other than it's new and exciting and obviously we want new and exciting from my understanding and of course you have to bear in mind that no one I hope no one is using in production yet the idea of Java 9 is you'll get a bunch of stuff three you'll get better performance better use of memory it uses new CPU instructions things like that so you should get things like performance improvements and generally a better experience from things like the compiler and without having to use any of the new features so this is a good thing let's like let's look at the elephant in the room straight away and most people when they talk about Java 9 they kind of that I mentioned jigsaw and particularly if your name is mark Reinhold and and so this is kind of a big feature for Java 9 and well I don't know I was going to say it's perhaps not super exciting to us as developers maybe maybe we'll get onto that so you hear about Java 9 you might hear about the repple who's heard of the repple Jay shell fantastic audience you are and so maybe more than half of you so the idea of the reporter who's used a rattle in another language that isn't Java I don't know if that's the same set of people but it's a good chunk of you so most of us who've been doing Java and only Java for a long time the repple is kind of an interesting and new idea the idea of being able to execute small snippets of code from the command line and try stuff out this is quite good for introducing yourself to the language but it's also quite good for like trying out bits and pieces to see how things work and I think this is this is an interesting idea Java because in the past you know we're accused of having quite a lot of boilerplate I don't know why they're saying that when a simple hello world application is only 24 lines of code but the record will help us and try stuff out without having to have all of that boilerplate but we're kind of left with this question of oK we've got the rattle and jigsaw what else is there is there anything that's kind of interesting for me as a developer but it turns out like in Java 9 there's one or two things going into the into the language some of these aren't necessarily going to be visible to you as developers there's a lot of stuff that's gone onto the covers obviously the things like modularization of the JDK there's things like update is logging inside the JDK things like I think I wrote a bunch of performance tests and stuff like that so that's not necessarily visible to you but you should benefit from it in terms of it being a better written better design language but there are some other bits and pieces in here that I want to take a look at at the end as well the end may be all that a third of the way through the through the presentations which things in there might be most applicable to us as usual I like to set my expectations for this talk appropriately low since there are about 500 of you in this room and of course you should not be using Java 9 in production please don't do that it's not ready yet and it does change from week to week so it's um you know every time you download the latest version there are some changes in the way things work so don't use it in production but do have a play with it definitely it's ready for for trying out speaking of which everything is early access so again setting expectations appropriately low I'm using an early access version of Java 9 I'm actually using build 1 6 6 I think the most recent one is build 1 6 8 but we'll build 1 6 8 doesn't play nicely with most recent EA build of IntelliJ IDEA so you have to get sort of the right combinations of the early access things in order to try and try and get things to work properly so I'm using only access Java 9 and then the early access builders IntelliJ IDEA and 2017 point to which the early access only came out like last week so it's in very early days I won't use the word unstable but also it's always interesting and of course with Java 9 we really don't know what best practice looks like yet and because we're not using it we're not using in production we're only just starting to get an idea of what best practice might look like in terms of Java 8 with lambdas and streams and things so I can't really tell you like this is the way to do stuff jigsaw wise because we won't really know that until it's embedded in and people start using it a lot more in anger if you like let me set the background for the case study we're going to look at and at devoxx UK a couple of years ago I did a demo Java 8 in anger I wrote this application to demonstrate Java 8 features like lambdas and streams and the idea is it's a Java effects UI with which responds to real-time tweet data so and it also happens to be architected with lots of tiny services that might be micro sized but they're not micro services because they're not reliable they fall over all the time there's no monitoring or any de stuff is actually appropriate to micro services but the idea of this application is to was to use eight features to architect something which is kind of running as lots of independent services and react to real-time data and it turns out that this is quite a good example to use with Java nine particularly when we're looking at things like the rector streams API which is something I want to go into in a little bit of detail if you're interested in the original application and I've got resources for that specifically so I'm not going to go into all the ins and outs of what it does and why it does it because I already have a presentation on that so first and foremost by the way I've only got like 50 minutes to talk about all of Java 9 so I'm going to talk quite fast I do apologize but I'm in London so I feel comfortable doing that here and next up yes so I think probably if you read anything about Java 9 if you read stuff in the news you might get a little bit alarmed by some of the scare stories of everything is going to break them Java 9 so the first thing I really wanted to do is take an application like my application it's small ish as in small enough to get your brain around it but complicated enough to do something interesting and I want to compile it with Java 9 and see what breaks and that way we can get an idea of what potential problems might exist so let's try and get this run in oh yeah the first thing I want to do is I want to see it running at the beginning just to make sure that everything's working to begin with because if it doesn't if it doesn't work before I start refactoring it's Java 9 there's zero hope to the rest user of the rest of the demo so as I said this runs as a series of independent services I've got one service which kind of acts as a Twitter service it's reading tweets from a file to make life a bit easier I've got one service which is extracting the username from those tweets I have a mood service which is not starting let's try that again I have a mood service which looks at the tweets and extracts whether the tweet is generally happy or generally sad and I have a dashboard which is showing me those results in real time yeah look at that an exception straightaway I think it's because I started the dashboard before I started the mood service hence my comment about lack of reliability it's kind of working [Music] all right there we go so yes so it works before a refactor it's that's a really good start and right now I'm going to see what happened the first time I compiled this application against Java 9 it was obviously a Java 8 application so let's take it back to a version which before I started doing the Java 9 migration so if I build this there we go so there's a few things I did insert a couple of things to fail on purpose but most of the stuff that failed was already there firstly you can't have underscore as a valid field name now people might think I didn't know you could do that that looks exciting I can use underscore as a valid field name and up until Java eight you could use underscores the valid field or parameter or variable name but in Java nine in Java eight you'll get a warning in Java nine and it's not going to work anymore so don't do that there's a fairly easy fix and something sensible and away you go and the reason being they're going to use underscore or something interesting in later versions of Java so that's kind of cool but in the meantime obviously we have to get rid of it from our code bases one of the other things is to take it out of presentation mode so of course as we always here with jigsaw that one of the ideas was to encapsulate the internal API is you shouldn't be accessing anything which is like some misc and I wrote this application knowing I shouldn't access some doc miss but being too lazy to figure out what the right way to do it was so I was using the encoder and now under Java 9 of course it doesn't compile and this fix times actually fairly easy because there's a there's a simple replacement for this so here instead of using my basic C for encoder I use a base64 very 64 Java util get encoder and code two strings so I can just replace that and then everything compiles if I build everything now my whole application compiled successfully so for me it was fairly painless to convert this Java eight application to use Java 9 without using any of the new features there were some problems but nothing nothing to showstopping so that's not as bad as I thought it would be what do I do with my remote who is and so when I created this presentation I didn't actually want to talk about jigsaw at all because for start there going to be lots of people doing talks about jigsaw it's going to be the focus of sort of a lot of the Java Nayan presentations and it's quite a big topic you can't just kind of mention oh and by the way jigsaw but it turned out that I ended up playing with jigsaw anyway to figure out like how it's going to look for me in a fairly small project but also mainly because Gradle doesn't work with Java 9 at the moment and I heard this week on Monday I did get it to work with Java 9 with a bunch of like compiler flags and I have to run it without the daemon and had to run it in the command line I couldn't run it from inside IntelliJ and there are some things you can do to get Gradle to work but I could not get my project to work inside IntelliJ with Gradle in a multi module project and the whole point was this is a multi module project so you know I kind of wanted Gradle to work but it also means that I can perhaps start playing with modules so what I did was so the directory structure we've got here is a fairly typical Gradle type directory structure where I have I've got a laser on this that's really silly so I've got a couple of different modules and the modules have got source main Java source tests Java inside it's a fairly typical groovy and Gradle structure what I ended up doing when I started playing with with jigsaw I changed the structure of my project to follow more closely what it's suggested in the tutorials and here that meant I had source and then all of my individual modules inside my source territory and test and all my individual modules aside my test directory which is kind of inside out from what I had before and this mirrors what's inside the open JDK so I kind of wanted to follow that this was a non-trivial thing to do to rearrange your whole thing just so that you can put some module info files inside your modules I have got it to work with the Gradle structure as well so it's not a mandatory part but if you're following their tutorials you'll see a directory structure which is different to what you're used to it's different to the sort of groovy way of doing those Gradle way of doing stuff so in my production code I've added a module inside of Java magic incantation file into each of my individual modules you'll notice I don't have them in my test directory and this was a pragmatic choice someone call it hack a pragmatic choice to get everything working inside IntelliJ IDEA and because you can't obviously you can't have modules for the same package names in two different modules so I can't have a test module and a production module and so I know there is a way around that using jigsaw but I didn't have the time to get that working so I chose this as a pragmatic approach for the demo there are of course and trade-offs of this in that for example I can only run the tests from inside the IDE also because I'm not using Gradle anymore I also manage my dependencies from inside the IDE so this is not really a production rating system as it stands right now but what it did find when I was doing this whole process of modularizing my code is my original module structure was kind of naive shall we say so I had them I've got two back-end services and the code for the back-end services depended upon the code for my Twitter service now that doesn't make any sense I can listen to the Twitter service but I shouldn't be dependent upon the code in the Twitter service so by looking at my dependency structure by looking at modules as a sort of first-class citizen I realized I kind of modeled my model modification incorrectly I also noticed that the client code was dependent on the web sockets API but nothing else was which is kind of weird because they're all using WebSockets and my reactor streams API was being used by two of my modules rather than either all of them or just one which manages the API side of stuff so I kind of had a bit of noise in there which just it just felt a bit wrong the way that the code had been organized so when I was thinking about what my modules really were and how to encapsulate the code what my dependencies should really look like I rearranged a lot of this so now all my services are kind of at the same level they don't depend upon each other code wise so that feels much more correct and I have a module which is this reusable service code which is dependent upon by all the services that make sense - and this is the only one that cares about WebSockets because this is a thing which hides away the implementation which happens to be WebSockets from the actual services themselves I also have all the back-end services dependent upon my helper module for the reactive streams API so that's cool because they all use reactive streams and then I have two services which use rxjava by choice and one service which uses reactor because I wanted to show that you can use different implementations of the of the reactor streams API which we'll look at later on so I was much happier with this module structure and by thinking about modularity and thinking about my dependencies I came up with a much cleaner design for my application so let's take a look at what was involved with creating a module info file and what that sort of means and I know mark covered this in his talk this morning but I'm going to do it from inside the IDE because that's what we really do in the real world all RIT was really impressed with with Mark's command line foo just I just don't work that way cool all right so let's switch to the right branch okay so you can see here I've got my supply modules let's put this in because he's a bit better and what I've got is in most of my modules I have the module info file I have a module which I haven't converted into being a jigsaw module yet so what I need to do is I need to create a new module info IntelliJ makes a reasonable guess as to what the name of that module should be and I'd like to keep it like that you can't really see that very well and so by doing that by adding my module info Java for this particular module and I start to magically use things like module paths and I'm doing things the modular way rather than the old way which is the way that we're doing it right now so now when I compile it I should get some interesting errors and what we see is inside this module one of the classes now which was compiling fine before now it says the module sensitive flow does not have the module Java logging in requirements obviously because I'm using IntelliJ IDEA I use alt enter to magically fix my problem and what we see is module info now we have requires Java dot logging so by default it's going to is going to be able to see everything from Java dot base but not all of the other modules inside the JDK so you you have to go in and declare which of the modules you need to use you can do that either by Auto fixing stuff or obviously you get some nice happy code completion for all of your Java modules them so now that that's not erroring what's our next error so we have a similar problem here only this isn't from inside the JDK this is an external and external dependency on an XML jar file I need to add that library to the cluster idea and then I need to say add requires reactive dot streams to module info so do the same sort of thing we have now we have two different requires one from inside the JDK and one which is using automatic modules so it's going to use the external jar file there and then those errors go away what's our next set of errors now from outside from a different module I get error is sensitive flow does not export the package since the flow to the module sensor user so now this is complaining about the fact that some external module is using something from inside this module so this module needs to declare what some what other modules can use so I'm just going to export my package there and then everything should just work so that's kind of fine and that's that's okay for a small little module like this there's not much going on in there there's a requires and exports and that could fairly easily be fixed up by addressing each error by error I'll show you some of my other module infos which cost me slightly more to do so this is the UI here you can see that I require a bunch of the Java effects modules and Java logging and I also JavaFX uses reflection so I had to open up my pretty much all of my code to Java effects for reflection now i explicitly decided to just open it just to java effects and I want to open it to everything to be able to access me and I also have to export this is an interesting a lot of these kind of came up as errors while I was compiling and while I was running stuff so I was just kind of like hacking away to try and get it to work and it turns out that when I showed this to someone from inside Oracle I shouldn't have to do that and there's actually a bug arose within the JDK as to how they're going to address this so this is the way that Java if X happens to do its reflection over your code but that's the magic incantation I needed and that all works another one and let's look at service this one uses a whole bunch of stuff from external jar file so a lot of these are automatic modules and I found this by trial and error brute force because I'm stubborn but I would suggest you use that you read the documentation for the Jade X 2 land use hatch because it takes way less time than restarting your application all the time and figuring out what these errors mean so yeah do read the manual it's very useful so that's what that's what I have to do to kind of get my application to be more modular it is a small education it's not like this massive enterprise applications it took me about a day a day and a half to get that all running and working the the pain I found was mostly the fact that I am greater doesn't work I spent another day this week trying to get Gradle to work so great doesn't work with Java 9 right now I'm not sure about the state of maven that's I think that works under certain circumstances I think Gradle works if you don't have a multi module project but kind of a lot of us are working with multi module projects and if you decide to reorganize the file structure that's a non-trivial task I would not recommend doing it unless you absolutely have to and of course the way that I've got this to work is there's no command-line build for this there's no way of running the test inside a CI environment or something like that one of the other pains is that the IDE support there is either a support for things like jigsaw as and so I've got some auto-completion i've got some quick fixes I have some help there from the IDE but I don't have like a magic generate me my module info type button I don't have some of the other things which might make my life a little bit easier I don't get quick fixes for everything so we are working on that but what we what we need from the JetBrains IntelliJ IDEA side is that if you find pain working with Java 9 in IntelliJ IDEA we need you to report that to us so that we can fix it basically and do do your homework if you're going to do this particularly jigsaw do do a lot of reading figure out how the tools work figure out how the stuff works from the command line before diving into the IDE because otherwise you don't really understand what's going on underneath the covers and it can be quite painful right I want to move on and talk about the reactive API because this whole talk was supposed to be about rah reactive is good and and here's OD reactive in Java 9 there exists a reactive streams API currently in rector streams 1.0 this has everything you need to define the sort of API between two different reactive programming libraries so obviously you have a publisher and subscriber I don't want to talk too much about what reacted streams really is or asynchronous programming or anything like that I just kind of want to skim over the surface of it and show you how it works in anger really and so with the reactor streams API we have a publisher we have a subscriber we have a processor which combines the publisher and subscriber and a subscription which kind of helps you to manage the relationship between the publisher and subscriber implementations of reactive streams include things like echo streams reactor rx Java vertex so a lot of these popular libraries for doing reactive programming in Java and other jvm languages in fact it's not just the jvm languages they implement this API so that's quite nice they can talk to each other without really caring about their own implementation that they're talking over the same API so for this reason in Java 9 we have got three active streams API built into the library and I think this is a really positive thing I think this is great I think reactive streams and reactive programming is a good model to follow it's quite a nice way of doing asynchronous programming it's not as complex as other models and the fact that this comes as part of Java 9 I think means that we are kind of embracing it as a Java community as a way one of the ways of working so this is great the only downside is that of course the reactor streams API is actually different to the Java 9 API the API itself is not the interfaces are called the same thing the methods are called the same thing they have the same signatures but they're in a different package so at the moment they're not really reusable so if I'm using rx Java and I'm getting a publisher I'm getting an org reactive streams publisher I'm not getting a Java util concurrent flow publisher so that I'm for this demonstration I actually wrote an adapter just to wrap those things and convert them from one to another but that's one of the things which I think is is interesting to understand because the libraries at the moment are going to be using the reactor streams API not the Java 9 API obviously because they're not compiling against Java 9 yet so when you want to use something like a reactive API in my application I designed it to be sort of event-driven so it seems like quite a good fit for or directive API I I have really stupid services they use WebSockets they take a string off a WebSocket and put a string onto the WebSocket and then I have obviously some business logic this basically is the architecture diagram for everyone's application ever and but I had some really simple business logic because for a number of reasons one it needs to be an approachable understandable demo 2 I need to be demonstrating Java 8 features so my business logic is generally speaking a lambda expression which Maps the strings from one value to another so in the case of the user service it takes a whole JSON representation of a tweet and returns a username for example and so this looks like you pass in the signature is I'm going to take a function which is my business logic and an I to take advantage of that I pass in a lambda expression or a method reference which represents the business logic of my of my service so given this my services as you said are intentionally quite stupid but I can make them a little bit smarter by exposing the reactor streams API instead so what I sort of figured is that actually from a business logic point of view I could be listening or subscribing to a publisher which is going to give me a stream of messages and I can be as sort of sending those messages to something which subscribes to me to a subscriber so I'm going to change the shape of my overall service so that I have a publisher which publishes strings in my case and a subscriber which listens to the result of the of the business logic so oh yeah so let's take a quick look at what that looks like so in the case let's go back to the diagram the publisher in my case is a class called client endpoints because what that does is it listens to the WebSockets and then publishes some string value and so that's the that's client endpoints so what I need to do is I just make client endpoint implement float publisher and then what it needs to do once you implement float publisher you need to implement subscribe and the SUBSCRIBE method takes a subscriber and then you're going to do something so as a publisher what I care about is who is subscribed to me and then I will do something with those subscribers at a later date when I need to so I'm just going to manage those subscriptions by them by keeping them in a list so Java 9 provides the interfaces for the reactive streams API you don't need to implement them with your own implementations the idea is that you're going to use an implementation like rx Java or reactor or something like that but for the case of my services because they're very very stupid I actually decided to implement my own publisher so that's that's the choice that I've made here but not necessarily definitely not something I would recommend and because there's plenty of frameworks that do all this stuff but I'm going to manage my own subscriptions from this point of view on the other end the subscriber in my case is a class called broadcasting server endpoint and what that does is it listens to stuff in the business logic and then uses WebSockets to send that to whoever cares and so again what I've got is I'm implementing subscriber and subscriber has unsubscribe on next on next is the interesting thing that's like what do I do when I'm given an event on error and on complete so those are the things that I have to implement in my class so on subscribe' all I'm going to do is I'm going to say give me everything that you get for the end of time what you can do if you want to as you say I only want to see the first three messages but in my case I'm just going to always listen to what the business logic sends me on next and what I need to do is I need to listen to all the sessions for all the sessions which are listening to me and to send them that message on error may I suggest that you do not do this error handling in asynchronous systems is a very interesting situation and I did a lot of simple error handling in terms of forgetting the demo working I did a lot of like print stacktrace and things like that and do not swallow errors in an asynchronous system because things will just not work and you will have no idea what to do about it error handling is a first-class citizen of asynchronous systems and it's really important to understand what to do with them and so don't do that and then do something on complete so I need to close my WebSockets when when there's no more messages so it was fairly straightforward for me to use the reactive streams API to provide a simple API for anything that wants to and basically create a new one of my services so what does that mean like how am I going to use this API what's the point of having a reactive API and so the first thing I'm going to look at is I'm going to look at my tweet service how can I now make use of a reactive programming framework in my tweet service how does that change things so what I had before and I've mentioned this a few times is that this was the aim was to demo Landers and streams what I had before is I had this really nice feature of Java 8 why I use Files lines to get a stream of strings for for every line in a file I will get a string back in a stream and then I can do something with it so what I'm doing is I'm reading I'm reading a file of tweets and then for every line I'm just kind of omitting that to anyone who cares I have to filter out some noise in this file and then the other thing I do for the purposes of the demo is that I insert a 100 millisecond delay so that you can see something interesting on the UI again do not do this in production but from my point of view as a demo application this is actually part of my business requirement you need to be able to see the tweets ticking over at a regular interval so let's rewrite this using reactor streams because we can so I'm going to use rx Java for this solo album so that's adding rx Java I need to add requires rx Java to my module info because we're Java 9 now and then I'm going to say from iterable I don't get to make the most as the nice files lot lines features in in Java 8 but I do get a chance to do something fairly similar and then I can basically just lift the same functionality from here and add it onto my reactive streams the only difference is there's no such a mess with it as peek so I use a method called Duan next which does basically exactly the same thing so I can use reactive streams to do the same thing that I was doing with Java 8 streams we will discuss the wisdom of this later so as you can see it looks more or less the same I can just delete all of that we start my service and everything works the same way at used to which is not a massive surprise because the code looks almost exactly the same so you might be thinking to yourself well what's the point of that now I've basically added another external library an external dependency to do exactly the same thing that I did before and I have slightly a slightly ugly a way of creating my streams well react to streams gives you a bunch of features you don't get with Java eight streams so for example this thing always made me quite uncomfortable this idea of like shoving in 100 millisecond pause especially using the peek method because peek is just generally a sort of debug method to allow you to do something on any many thing which comes through the stream so this feels like a little bit of a hack I'm with reactive streams you can do things like you can create a flowable for an interval so I can create a flowable which is an interval of 100 milliseconds what this is going to do is let's call this tick this is going to emit a long value every 100 milliseconds this is going to take over every 100 milliseconds so that's quite nice and then what I can do is I can merge that with my stream or with my reactive stream for reading the file so I can I can zip these two things together so that I can force this stream of reading things from a file to admit things every 100 milliseconds and this is fairly easy I just have to say zip wiz give it the tick and then I have to tell it given that I now have two values I have a long value from the tick stream and I have a string value from my file reading stream but I don't care about the long value because it's just a placeholder there to sort of take over so I'm going to just omit the string value and then this way I'll restart this I kind of I've used this feature from reactor streams which is the ability to actually work on two streams and merge them together to give me something which does the same thing as before but it's quite I think it's quite a nice elegant solution to prove this is meant to happen it's meant to take every hundred milliseconds rather than using a slightly hacky approach so that's the feature of reactors dreams you don't get with Java eight streams for example okay what else can we do I wonder the user service because it's not very interesting the mood service let's look at the mood service 13 minutes left okay let's quickly look at the mood service so here the what I did with the services as you saw I am I exposed a publisher and subscriber to anything which is going to use these services and then I just have to tell it what to do with those things so let's say I want to get moods with a publisher subscriber I'm going to use reactor to kind of show that you can use different implementations of vector streams this interviews in flowable I have flux flux from publisher now I'm going to get an error here because from publisher is expecting a reactive streams publisher and I'm passing it a flow table sure and so I have an adapter to do that so that's although I'm just going to wrap it inside my adapter and then what I can do is I basically do the same thing I was doing with Java 8 streams I can do it with wreckfish streams flat maps of course is different because I can't take a stream I have to have a flux from array and then everything works exactly the same way it did before which again sort of begs the question what's the point well the point is actually when oh yeah I need to I can't collect because I'm surprised that compiles I can't collect I need to subscribe I'm going to put the subscriber on to the end of that to listen to that which also needs to be wrapped inside the adapter [Music] so again this looks exactly the same as the Java eight streams what's the point the point is that actually if I run this it doesn't work nothing happens he stops and and this was this is a mystery especially as I'd overridden all of my error handling with I'll do something at this point and it turns out that that reactor streams are an entirely different model they work in entirely different way they're designed to work on infinite streams they're designed to be combined the device design it's supposed to be split and they have the idea of back pressure as a really important first class citizen so with with back pressure if your publisher outpaces your consumer you have to explicitly tell the system what to do about that are you going to drop messages on the floor are you going to apply back pressure and force the producer the publisher to slow down so you have to understand how the flow of information works through your system and and if you don't things just fall over in this particular case it turns out that flat map spawns off a whole bunch of different threads to deal with flat map now this doesn't happen in Java eight strings because if you're not going parallel it just says everything in the single operation as a blocking single threaded operation and that's a completely different model to the reactive streams thing so I had to put in a magic incantation to say only run that on one thread and then everything works I would love to be able to give you some detailed information as to why that is but to be honest I don't know but it works now and that's the important thing the well the important message here is react Christians are not the same as Java eight streams Java eight streams are a very nice way of querying collections in a way which is scalable if you use the magic parallel incantation and your collection is big enough and your operation is expensive enough you can spawn that over multiple CPUs and that's good that's not the same model as reactive streams it's not for doing the same thing even though the API looks exactly the same it's very confusing all right so I've got nine minutes to go over everything else really I'm not going to go into the details of this because it's not that exciting and yes so reactors dreams look exactly the same as job eight streams they are not the same every time you look at reactor streams you get this really intuitive marble diagrams I don't really understand that so you events that go on one stream and events that go on another stream streams don't cross don't cross the streams and you have no idea how they're supposed to interact with each other and which direction time goes in but other than that it's a really clear way of explaining how reactive streams look right so reactor students work with multiple streams you can have multiple consumers back pressure is really important and they have a very different concurrency model so if you are thinking about going reactive and you have not already done so you need to understand these things otherwise things are going to go very confusing very quickly right other Java 9 features good I've got a whole nine minutes to work on this convenience factory methods for collections has anyone heard of this a few people my personal opinion is that this is going to be the most used feature of Java 9 and it's a really silly one as well silly it's a really useful one but it's a it's not as exciting as lambdas and strings let's look at in the olden days ie now and you create let's make this a list okay it makes a list of strings and you'll create a list of strings by doing something like arrays dot as list so that's how we create little strings that's fine in Java 9 you'll be able to say list of you just saved yourself something like four characters yay life is good and that's not super exciting it's kind of nice it looks a bit better but what is kind of more interesting from my point of view is you can do the same thing with set but you could not do before so you can do this for creating in mutation unmodifiable lists and unmodifiable sets of data so if you're going to set something up just once and then use it throughout your application so obviously I can see there's being super useful for tests but also this is production code here it was sort of test data but you quite often might want to set up just a small collection that's going to be on modifiable and you want to do that as easily as possible so this is kind of fine here where it's kind of more interesting from my point of view is where we get look of maps so again this isn't a false example I put in here just to show Java 9 stuff that I needed to do this in order to to set up a static map with a set of values that I'm going to use to compare stuff and so I have to have this horrible static thing in here in order to set up the map and it's horribly ugly but it's not uncommon we've have to do this a lot now we don't need to do this we can say map of entries and so now we can do it with with maps of entries so that's kind of nice who saves ourselves some lines of code we don't have to use an ASCII static block and I can again I can see for some test data this could be really useful it seems more useful if you have fewer than ten entries because you don't need hardly any of this you just say map of get rid of all of that and just two key value key value key value for up to ten so given that I used to work for MongoDB and most of my job was writing maps inside Java this is super useful and just gets rid of an awful lot of boilerplate around creating maps and other collection types so I think this is going to be extremely valuable it's not quite as pretty as Groovy's way of doing it for example but it is a good compromise compared to to what we were doing before right five minutes left there will be no time for questions I do apologize and right private methods on the interfaces now this might seem like a really silly thing because interfaces are supposed to define your public API why would you need a private method on an interface well of course the answer is that in Java 8 we got default methods and static methods on interfaces so for example again this is some genuine code that was in this application and I have static methods but I've got copy and paste coding in here because there was no other elegant way of doing this I mean I could extract this out into a method get value for fields oops it's got value wrong and but this in Java 8 this still has to be public and I might not have wanted to expose this on my API of course now in Java 9 I just make that private and then I can reuse that code so that's going to be really useful if you started using static or default methods on your interfaces I'm sorry the spelling mistake is really killing me here feel better now and new methods on the streams API the Java eight streams API does allow you to work with infinite streams and that quite often doesn't let you stop your infinite stream which seems like a tiny miscalculation but now you have new methods on the streams API to allow you to do some operation until something until something is true or to ignore a series of values until some operate until some value is true and it's called take while and drop while so for example and what I was using filter in this case because it kind of works I was trying to do something for a certain set of values but now I can just say take while so while some criteria is true then I want you to do something and in this case my stream is only 17 things long so it's not a big deal but obviously for things like infinite streams you're going to want to do something until some value and then and then stop and this I did get bitten by this before Java 9 came in because when I was reading my my file of tweets I had no way of terminating that service just it's just going to go until the end of the file there's no way to say stop now so this is gonna be really useful so take while is do something until some criteria and drop while is ignore everything until some criteria and I believe you can combine the two so you could say like in all all of these do something with these and then stop at this point it was particularly well on ordered streams but it doesn't have to be just ordered streams I'm the stat walking API had a play with that this week because I thought I should probably put some error handling into my application and the stack walking API let's have a look I'll show you I haven't really played with it properly so do it bear with me a bit though you can stack Walker and then you can you couldn't walk your sacks you give it some function and it gives you a stream and then you can do something so a note that I'm not having to get the stack trace from an error or anything I can pick up my stack trace at any point in the application and what this lets me do is I can do things like I can use the power of streams to find for example let's say the first place in the stack which has a particular package name or how deep is my stack how deep is my step and how it start singing and or you know you can do you can use the full power of the streams API to have a look at your stack and of course the point here as well as it's done lazily so because of course everyone knows stack traces are really expensive if you if the stream operation is not applicable you're not you're not going to incur that cost so this is quite nice I haven't had a chance to play with it like properly but I think this is going to be really useful under certain circumstances in particular I wanted to see where is the first place in the stack where like my my package name might be available so or how many how deep is a bit where when my packages are in there all my times up right nearly finished multi release jar files this is really exciting for me as someone who works on library development stuff in the future from Java 9 onwards as a library developer I will be able to release a jar file which makes use of Java main functionality if you're using Java 9 otherwise makes use of pre Java 9 functionality and and it will work for 9 and 10 and onwards or pre-job and I'll as far as I'm aware so for example I don't know if this is going to happen rx Java could for example release a single jar file where it uses the Java 9 reactive streams API if you're using Java 9 otherwise it uses the reactor streams API if you're not using Java 9 and I think this will be really useful especially for migrating to Java 9 stuff process API updates you can now get the pit of your process and kill it if you want to this is a good thing there's a bunch of other stuff in there as well so if you are like managing your own services if you're creating your own micro services you can do things like actually use command line tools in a really useful way updated application you'll see this when you read the documentation you'll be able to see whether a deprecated method is scheduled for deletion or whether it's just like I you probably shouldn't use this and you'll also see when something was deprecated like Java 1.8 or 1.2 or whatever html5 Java dog if you generate Java dot which most of us don't you can generate it in html5 yeah Java Docs search look look a little search bar in Java dog skin brilliant the reptile thank God I don't have time to look at the reptile but there's a there's a tutorial here I've talked about it in some of my other talks as well and if you are used to using a reptile and you have ideas of how a rifle should work inside an IDE please tell us because you really want to understand what users want from the repple right I'm sorry I'm enormously over time I'm so sorry and the pain of Java 9 your code might break with Java 9 it's true your code might break especially using Gradle and they are working on it by the way they are going to get it fixed and but at the moment doesn't work but the best thing to do is just try it out as soon as possible find out what the problems are especially if some of the problems actually aren't your fault and then someone else's fault you need to be able to report it to those libraries or to the jdk team or so whatever so please use it as soon as possible and find out what the problems are and there's a bunch of stuff which is going away that's one of the reasons why stuff is going to break and there's a bunch of stuff which is going to change for example g1 is now the default garbage collector I don't think that's going to have a massive impact because I think if you care about what garbage collector you've already tuned it to within an inch of its life and if you don't care about it it's probably not going to make any difference things like all the new version strings version string scheme it's now Java 9 notch over 1.9 so if you're pausing the version string to figure out whether you're running on one point eight one point six one point whatever and you're going to fall over when you pass Java 9 okay so be aware of that and other stuff Java 9 is still evolving including IntelliJ IDEA so and these features are evolving and things are getting better but it needs feedback some real developers to really help that happen you don't have to modularize your whole codebase in order to use Java 9 in fact if I I would recommend probably not doing that unless you absolutely have to and it might not it might not be for you it's not it's a non-trivial thing to modular eyes your codebase but you don't have to so don't panic about it is fine and reactors dreams are not the same as Java 8 streams so you have to be aware of what the different use cases are for those things I found Java and I encourage encourages good design with modularity encapsulation so on and so forth so it just got me thinking a lot more about architecture and design I think this is a good thing encourages new ways of working with a rip-away the repple with reactive streams there are some different models to use and there's lots of things in there that just make code nicer to write so things like the collections framework things like the stack walking API things like the process API and all these little bits and pieces just make it a little bit nicer to write code all the resources for this talk are available at this link including slides links to more information links to all the stuff I use to try and put this thing together and when the video is available I will put it up on there as well too thank you very much [Applause]
Info
Channel: Devoxx
Views: 36,602
Rating: 4.6701031 out of 5
Keywords: DVUK17
Id: GkP83hvdeMk
Channel Id: undefined
Length: 54min 56sec (3296 seconds)
Published: Wed May 17 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.