Full-stack Reactive Java with Project Reactor & Spring Boot 2 • Mark Heckler • GOTO 2018

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

FYI, here's the talk Abstract

Reactive programming offers Java developers a way to build message-driven, elastic, resilient, and responsive services...yet many Java developers don't know where to begin. The Reactive Streams initiative provides a baseline and Project Reactor provides a great way to become immediately productive, leveraging reactive capabilities from end to end. Whether you're coming from a Spring MVC environment or a functional perspective, Reactor empowers you to spin up fully reactive Spring Boot 2 applications quickly and efficiently.

In this talk, the presenter dives into the net-new Netty-based web runtime and shows you how to:

  • integrate easily with existing Spring-stack technologies
  • easily transition from blocking to reactive applications & systems
  • define your API in an imperative style and functionally, reaping all benefits both ways
  • leverage powerful new testing mechanisms to make code better and life easier

The presenter will code all examples live and in real time. This is not an abstract discussion, come to gain real, practical knowledge!

👍︎︎ 7 👤︎︎ u/goto-con 📅︎︎ Mar 21 2019 🗫︎ replies

More than 1/2 of this is him making bad jokes.

👍︎︎ 6 👤︎︎ u/DuncanIdahos7thClone 📅︎︎ Mar 22 2019 🗫︎ replies
Captions
[Music] hey welcome to this afternoon's session on full-stack reactive java with Spring Framework 5 spring b2 and project reactor I like really short titles so I just start with that my name is Marc heckler I'm a principal technologist in spring developer advocate with pivotal Software Inc pivotal are the makers of such fine software components as the Spring Framework spring boot Redis rabbitmq green plum gem fire huge contributor to apache tomcat huge contributor to the Cloud Foundry common code line you may have heard of us we're a tiny little start-up in Silicon Valley who just went public by the way we just IP out a few weeks ago which makes the financial folks extremely excited I'm still doing the same thing I was in here before so yeah it's all good I blog admittedly not as often as I would like but you know I'm trying to get better it's a new year still sort of you know we're in the first half I guess it still counts but I blog semi-regularly at the hecklers calm I tweet very regularly though @mk heck has anyone here on Twitter Twitter come on ok it's it's 2018 it's time Twitter actually turned a profit for the first time last year so I think it's here for the long haul it's safe jump in now it's where your favorite open-source committers hang I jokingly said I guess it was yeah last month that there are some months that I spend more time on Twitter than I do at home and I went back to my hotel room and I figured it up and actually in April and May I actually did spend more time on Twitter than at home so it's a little sad actually but but Twitter it's a place to be we only have 50 minutes here today which is a rather a shortish amount of time the good news is we have a lot of time afterwards if you have any questions comments or feedback Twitter is the best way to reach me I live on Twitter from send up to send down and it times in between I have been known to wake up in the middle of the night reach over to my nightstand and the hotel room pick up my phone and tweet again probably another sad statement of my life but there it is so please do reach out to me on Twitter I'm very responsive there but if your feedback doesn't fit in 280 characters or you hate Twitter or what-have-you I'm also a member of the slightly older and more established social network called email is anyone on email good ok it's got to be big one of these days I'm sure they keep telling me that I actually have liked you about a half a dozen different email addresses but these are my two favorites market the hecklers dot-com I actually have both the org and the comm site so I had to check which one I listed and in heckler pivitol die oh these are the two that I checked the least infrequently so I'm not on email every day multiple times a day throughout the night but I do check it from time to time so if you have any feedback please do reach out to me there it's fine just give me a couple days to respond so before I get into this actually I if I could I need to to ask if I could get a selfie because I like to prove to my boss that I'm actually doing something here instead of just sampling the coffee scene which by the way it's excellent but but if I could get a selfie here let's try this side of the room first everybody say spring and this side say reactor see that was even harder in that sounded louder that was I know you guys come on step it up anyway so so now I have proof so we're done any questions okay I am I guess a little background on me Who am I I am an author I have I've authored several blogs and blog posts I've co-authored a couple of books I have another project in in-process actually a video course that I'm going to be releasing on spring cloud stream here soon ish so if you'd like to hear more announcements on that please do follow me on Twitter that's right now it's everything first again I live on Twitter I am a frequent conference speaker and I go where I'm invited thank you so much for inviting me here goto is awesome any any first-timers I guess I should ask that yeah what do you think so far pretty awesome right so hopefully they'll invite us all back they'll let us come back hope hope so anyway I am an architect and developer and as you might guess from the next bullet point where most of my expertise has been one it's in the realm of Java I was voted into the Java champions a few years ago now three years ago maybe for ongoing contributions to the greater Java community so if you had anything to do with that if you voted for me appreciate it I appreciate the vote of confidence and I am a seeker of a better way as are you or you wouldn't be here our industry has shiny object syndrome right we are constantly fixating on the new hotness but and that's bad in general right but it is important to learn new things because by learning new things we sometimes find solutions to problems that were previously intractable we find better ways of doing things and if we don't venture out of our comfort zone we can never possibly learn those things I love an old I'm kind of a quote quote nut so I love an old quote by Washington Irving which is something along the lines of when traveling by stagecoach it's often good to shift and be bruised in a new place I know nothing about travelling by stagecoach but I think the lesson is here here's is valid it's good to shift it's good to get uncomfortable so we can broaden our toolset and our our expertise right so we can solve more problems better so today what are we talking about great question okay reactive programming so I love this quote by project reactor team member rossandjo and chef in which he says in a nutshell reactive programming is about non-blocking event-driven applications that scale with a small number of threads with back pressure as a key ingredient that aims to ensure that producers do not overwhelm consumers now that PACs in a lot of information in a single quote which is awesome right but let's unpack it a little bit first reactive programming is reactive programming is about non-blocking event-driven applications a little history is in order here and again I'm kind of a history net as well I will shorten this because the more I talk the less I get to code and I love code I hope you do too so but in the Java ecosystem typically we've had a bit of an artificial artificial construction right at the operating system level typically everything operates asynchronously and yet within Java the JVM which sits on top of an operating system we've had this construct which tends to have a model of scaling out by increasing the number of threads via multi-threading right so the more requests that come in for each request typically you have another thread that spawn and then we we have this massive number of threads at any given point in time most of which are what our idle right they're waiting on something else they're waiting on a response from a database from an operating system from a maybe an API call so it's it's very easy to reason about right i mean you typically make a request and you wait and then when it comes back you proceed so that's that's nice it's helpful but it's not terribly efficient right with a reactive model you don't have that now asynchronous programming has been around for a while right but it's been a little messy understatement right asynchronous programming has led us to lovely terms endearing terms like callback hell things like that that that if you've ever had to do this asynchronous programming in the typical model it's been a little rough around the edges right with reactive programming it tends to adopt a much cleaner look while giving you the advantages of asynchronicity it allows you to scale with a small number of threads this is kind of important right I think node showed us all in the Java community you want a small number of threads it's what a thread can do but you also have the problem of a thread right so we don't want to repeat necessarily that mistake but we do want to take some of those lessons learned from the node community back pressure is what I feel like is the kind of the number one thing that separates reactive programming from asynchronous programming because asynchronous programming doesn't in and of itself involve back pressure there's no way a slower-moving consumer to push back to say hey look I can only take two values at a time or five values at a time or whatever the magic number is so back pressure is very important it doesn't mean that you won't still have publishers or producers that are producing a massive number of values that a perhaps perhaps a slower subscriber can't deal with but it does shift the responsibility for what you're going to do with the internet and number of values to the publisher which is typically where we have a little bit more control right I I do want to kind of explain this as I go but this gives you a little bit of a taste of where we're going with the idea of reactive programming in general now as I mentioned I love history so I used to talk about all the history that built up to the now that again that takes a bit of time and then I realized something as well because at one point in time there were a group of very intelligent people who released a thing a glorious thing called the reactive manifesto now in our industry nothing truly ever happened before a manifesto is created right so we can ignore safely ignore all the history up into that point is anybody out there okay I should have I guess led with that you will hear the worst jokes in any language here today in my talk that you will you will hear anywhere so that was a joke I can tell because I got absolute silence but but that's okay but so we had a manifesto and then then history began the world began so so a lot of smart folks got together and said you know really we could concentrate on a lot of things but that where we really need to turn our focus is on the interactions the streams if you will because that's where most of the inefficient waiting takes place so some companies got together companies who have really organizations as well who had come up with ways to solve these issues but they were all done differently and there's a there's an old term coined by rain Horta former head of Novell does anyone remember Novell any other old-timers in the room besides me okay Novell was they still be around I don't know I think about that I think it is but rain or die headed this company for years way back and he turned he coined the term coopertition which i love the idea of coopertition is valuable because you can still compete with your competitors while you can still cooperate with them so that your customers don't get crop caught in the crossfire which is smart right and we're developers we don't necessarily care if company a doesn't like Company B or they don't get along and the developers don't talk we just want our stuff to work so several companies got together and created the reactive streams initiative based upon the outputs of the reactive manifesto so you had companies like pivotal like light men nay typesafe Cuisine Twitter Red Hat I think even Oracle finally came on board right thus we had the Java util Java util concurrent flow and Java nine but the reactive stream specification was born out of a desire to perhaps solve things differently but to be compatible to work together in the resolution of these things so there are four parts to the reactive streams initiative one is the specification itself the textual spec one is the API another is the examples that are used to kind of showcase the API and fourth is perhaps the most important which is the TCK the technology compatibility kit and that's what allows you to demonstrate your compliance or lack of compliance or things you need to fix or work with in terms of your implementation of their active streams initiative now what is what are reactive streams there are four interfaces defined and they're very simple right you have a publisher which kind of makes sense kind of self-explanatory it's the thing that publishes values you have a subscriber which also somewhat self-explanatory it's something that receives these values you have a subscription which is the contract that a subscriber enters into with the publisher and then you have a processor interface which implements both the subscriber and the publisher interface so it can take a value act on it do something with it manipulate it and then pass it on to its subscribers so it's very simple it's very granular it's it's really too granular to be useful in and of itself I mean for starters it's an these are interfaces so you do have to provide an implementation but it's again that very granular level so at some point in order to really use this you've got to put some code behind it right now you can code your own implementation there's a lot involved as you might as you might imagine there are a lot of moving parts probably not the best recommendation to do but there are implementations things like haka streams and rx Java and in our case project reactor so what is project reactor this is kind of this spring entrance into the field now it's not just us we have committers from a lot of different companies we do cross pollinate we do work closely with a lot of other folks on the other teams I like to point out that David Carnac who's the head of rxjava - - point X I should say now is a heavy committer into project reactors so we get along right we all try to work together and and better the the whole field but we all have perhaps a little bit different way of doing things project reactor I think does a lot of things right I'm biased you make your own decisions but we for starters we base everything on the Java 8 API right so if you're used to dealing with the Java 8 functional API completable streams lambdas things like that duration this feels like a very natural extension of that we also in terms of publishers in the Java world you typically return a value from a method right or a collection of values and the same thing kind of goes when you're talking about publishers right you can return in our case a mono which is you're expecting 0 or 1 values that's a specialization of publisher or you can return a flux a flux is a publisher of 0 to n values perhaps even over an undefined number of values over an indeterminate amount of time now some operations make sense for both some operations make sense only in terms of one or the other so it kind of makes sense to specialize in the spring road we call this whole library that provides this capability in Spring Framework five and spring the two project web flux I hate the name I mean I'll be honest I don't hate it that's a little strong I didn't like the name I expressed my doubts I was outvoted I'm not bitter to me it makes me think of a vacuum cleaner but anyway it is searchable there are a lot of other advantages to it it's very quick for us developers to get on and search stuff with the name Webb flocks it's unique and it does catch right it makes sense because it's based on the flux publisher so that's kind of where we throw it in now you don't have to use spring you use project reactor it's a dependency of spring not the other way around but of course who wouldn't want to use spring right really ok anyway and it's all based obviously it builds on HTTP uses server sent events WebSocket TCP UDP I mean you've got the whole hall whole ballgame in there so with that let's code does anyone recognize this guy by the way oh good ok I always worry a little when I go into a room and I ask that and everyone's looking at me like for those of you who don't first let me say I'm sorry I'm very sorry if you're a Doctor Who fan you probably got that reference too but any Doctor Who fans I should ask good ok well you'll you'll get some stuff later that the rest of you again I'm so sorry well not yet but this is from the IT Crowd it's a Brit come from a few years ago and it is awesome is it on Netflix available on Netflix here I'm not sure I was talk about it wherever I go and I I spoke in Mexico not that long ago and sadly it's not so I told them you have to find another way to watch this it's too awesome but it's actually a little dated from a few years ago now but it is it is so much our lives right I mean you'll watch this and you'll laugh you'll cry you know you'll you'll relate but it's an awesome awesome show this is one of the protagonists Maurice moths you have a weekend coming up just block it and binge watch ok do yourself a favor all right so with that let's start in two things I'm gonna take a quick break here ok so how are you doing on time we have until 9:00 well that's great ok we get plenty of time then ok so so this is our starting point right this is the spring initially does anyone recognize this site for those of you who don't for the two of you who don't that's fine this is your starting point for spring boot base micro-services on the net you don't have to go here to create a spring boot project but it helps right so you can do it all manually if you want or you can curl this or to use the spring command line interface or you can just go here I like to go to the web site because it's so pretty okay just like I have not a great sense of humor I also don't have a great eye but this doesn't work really well okay so we're going to start here to create our spring boot projects now this doesn't generate any code what it does is allow you to make certain logical selections on dependencies and then it dips it up you download it and you get started I guess it does actually create your main class and your main method that's it but it's just it's elite that's starting off point it just kind of gets you started fast so it's opinionated like all spring projects are where you can very easily get started if you fall within that 80 to 90 percent use case but if you don't if you want to go crazy and go manual and choose all the selections by hand you can that's great we just don't have that much time so I'm gonna shorten things a little bit and just go with the simple version I'm a simple guy that's cool so let's start off now you do still do have some options even at the very simple level so if you're a maven developer you can generate a maven project if you're a hipster you can generate a Gradle project it's okay it's okay I actually might my my hoodie is in my bag so I'm gonna stay with maven for now that I might be Gradle and at a time so just you know we'll see but it's alright you can go there so I'm gonna choose maven you can also choose to use Java Kotlin or groovy any cotton fans in here yay okay I will come back to you here later on because I do have a code repo that I linked and I code all this in Java and I code it all again in Kotlin so if you're if you're curious it's out there so we're gonna stay with Java today just to kind of stay down that main stream path we're going to use a current version of spring boot which is 2.0 at 3 and I'm going to just kick things off I'm going to change that from example to the hecklers so that way at least we have the proper domain here so so now our artifact this is where it gets tough my colleague Josh long and I came up with a what we thought was a an all new cool thing to do to demonstrate the power of project reactor and we brains from this for at least 15-20 minutes so this was a great idea right so I'm a big again I mentioned earlier I'm a big history buff movie buff I like movie quotes right so I'm hitting him with tons and tons of movie quotes as I am want to do and he says you know let's is all his idea okay let's create a service that will stream movies over to the internet because that's never been done before right so we did this we called it Netflix and and we just started fielding this and some little guy in a suit showed up on the front door and he handed us a cease and desist letter so that was a little disappointing so we changed the name same little guy in the suit comes back so to make a short story long I decided that probably we shouldn't venture into that whole streaming movies thing but I thought what do I love right because you should always do what you love that's what the the people tell you to do is follow your heart so I'm gonna create a coffee service today any other coffee fans in here if people hate coffee yeah good okay I love coffee so anyway this is this is my favorite part of the whole day all right after drinking coffee so I'm gonna start by defining a few dependencies I'm going to bring in the first the dependency for reactive web anybody here I guess I should have asked this first anybody here a spring MVC developer okay so you're used to you know dealing with tomcat for your for your servlet container right that's the default but as with any spring boot project you can swap that out for undertow or jetty or what have you same thing here but we default to Nettie for the reactive part for for web flux you can swap that out again for any servlet 3.1 compatible engines so things like Tomcat 8 or undertow or jetty all is fine but but we start off with Nettie because it just kind of makes sense right Nettie plenty pioneered this area and as really well at it and it also gets you dependency for Spring web flex as we'd mentioned I'm also going to bring in let's see I'm gonna bring in reactive MongoDB because we need a database and I love is it is anybody here who works for MongoDB you do okay good because I love is super fast it's it's so fast sometimes it's like it doesn't even save the data I'm kidding I'm kidding I do pick on my friends so my friend you're in trouble today I'm sorry I'm just gonna warn you now but anyway so I'm gonna use I love I am also going to use something that I find really cool actually it's embedded MongoDB now this is he heard of this there's there's a German guy who created this library called flapdoodle it's embedded MongoDB now first of all I know what you're thinking no German has a good enough sense of humor to name anything flapdoodle right but it this guy exists he really exists I connected with him on zing so I know he's real okay but anyway I just love it because with the name I flapdoodle I mean you can't even say that without smiling right flapdoodle what Wow okay tough crowd I can't say it without smiling all right so I'm also gonna use Lombok because I'm a lazy developer lazy good not lazy bad right Lombok helps me cut down on the boilerplate code and I'm still learning things about Lombok I couldn't believe I used it off and on for years and I just saw not that long ago did you actually have the capability to define ball valve and VARs with Lombok from like 0.8 version years ago so if you were thinking Java 10 for my bar or Kotlin you don't even have to go there not that I'm discouraged you from doing Colin Collins awesome but anyway so I'm gonna use Lombok here today because it kind of short-circuits things that's always nice I'm gonna save this down if it ever comes down did I lose my connection now there it is okay so we'll just save that to the desktop all right so I'm gonna unzip this open up my project and my favorite IDE NetBeans huh just kidding all kidding aside I like NetBeans too I will pick on them a little bit but but NetBeans and Eclipse have excellent support for spring boot projects there's a guy I think out of Italy Alex Philippa who doesn't just a yeoman's job on on you know the spring food annotations and support within that means so so I joke about it but again love that means too but I do IntelliJ actually kind of fits with my with my workflow and my usage patterns better so that's what I'll stick with the today do you need to blow this up let's see let's apply that can we see that in the back let's see let's open that is that readable that's why I'm big in this setting so that's that's huge right alright so we'll start there so let's see this is a coffee service so we should start with our domain which will probably be something like coffee right naming is the hardest problem in computer science so I'm going to first create a few little attributes of a coffee that we might keep track of like it's ID also maybe string I should also point out those of you in the front row front couple rows I sometimes typo it happens I type fast sometimes my brains faster than my fingers so if you see me typo yell it out before I hit build otherwise on your it's your fault especially you you brought an ice cream earlier and you didn't even bring me one so you know alright so okay so so we're going to create a coffee class I'm going to again store this in so I'm going to make this a document class I'm also going to since I'm using Lombok to make this a data class that gives me things like my getters and setters my equals my hash code my to string I'm also going to ask Lombok to provide a nargis instructor and an all Argos constructor so that's kind of nice right out of the box I'm also going to indicate that's my ID field and that gets us largely our our basic domain anyway right so we're gonna start with a Minimum Viable Product and roll out quickly now that's okay but we need some operational visibility into this right because if we're going to create the coffee service to end all coffee services we need to be able to track the orders for each individual coffee so if we have a thousand orders a second coming in for a particular kind of coffee we may want to carry coffees that are similar to that coffee right so we can boost sales it's all about it's all about the sales right but if we have a coffee that's only being ordered once a year we can probably drop that out of our catalog so we need to have some information about coffee orders so we'll create a coffee order class order or saved you at that time okay we will want to have some information like the coffee ID right and then we also want to know when a particular coffee order took place a date ordered there we go okay and once again we're going to annotate this is that data at norc's instructor and all Argos instructor now we will need a repository spring spring data has a concept of repository spring boot allows for really nice Auto config so we're gonna create a coffee repository interface and we'll extend our reactive credit repository which is an interface to find in spring data now sometimes people look at spring boot when they're coming in fresh and I did it to know though attribution there but they look at it and they say there's so much magic there oh I'm used to seeing all this code and now you have a few annotations in it all it all works but I don't know why here's here's a hint arthur c clarke said it best any sufficiently advanced technology is indistinguishable from magic it's all just technology okay so I'm gonna pull back the covers a little bit spring boot will make certain logical assumptions based on your code your annotations your class path right and what it does when it sees when you when you do they build when you run your program is spring boot season you've extended an interface to find in spring data and when it sees you've done that and you have a database driver in your class path it makes the crazy assumption that you want to talk to a database I mean this isn't hard right this is not a really tough tough leap but this is auto-configuration going to work for you you can tailor it you can tweak it you can completely overwrite it and do it manually if you want to but why why so we're going to extend our interface we're going to be wearing items of type coffee with IDs of type string and that gets us some basic repository functionality right out of the box now we want to do a few things I want to load some data being a very visual person we have a working application now hopefully but we don't have any way to see that so I'm going to create a component here I'm going to call this my data loader and I'm going to inject my coffee repository and constructor injection best best-case scenario here best practices I guess I kind of hate that term but we'll go with it so I'm going to create a post construct method private void again visibility's low and we're going to load some data now this is a coffee service and this is the this is the absolute best part of the whole talk so I peek earlier right but this is the audience-participation part remember garbage in garbage out say if you don't give me something good to work with it's gonna be just downhill from here so we're gonna create a flux which is a publisher right we're gonna create a flux of just these values does anyone have a favorite coffee a good coffee none of the cheap stuff okay good coffees what what is it you have to shout it out man americano okay we can deal with that in honor in honor of my new best friend here okay so let's see actually Abraham was telling me about an excellent coffee that I can't wait to try as soon as I can find it Kyle Esmeralda see excellent coffee so I'm told okay there's one from my hometown of st. Louis kind of in the center of the US it's called Khali's coffee called e is a coffee roaster there and it's it's quite good if you get a chance to try it someday please do anyone else have a favorite coffee cafe au lait hmm is that a good one okay we'll go with it Cafe and I love this part because I get to show off my my wicked humor sir oleh once again look at that yeah baby alright anyone else what what spell it for me please oh okay well that's what I thought you were saying but I thought no it can't be that simple it's like my name any time you exchange names like its mark it's just just mark I felt like it was too easy so yeah anyone else Jaffa I like that okay yeah you're in the right room excellent okay okay we'll stop there for now but if anyone has one that they just really they think of later and they love it yell it out we'll stop we'll go back it's that important okay so we have some some of our favorite coffees right here I'm gonna take each of those coffee names and I'm going to map each of those as soon as I can type map each of those that's a coffee name so we're gonna create a new coffee object right I'm gonna use UUID to create an ID and then pass that name so that we'll map each one of those names to a new coffee right and then I'm going to flat map that so we're gonna take each of those copy coffees get it right here and do our repo save I'm doing a method reference here just to shorten things up a bit and and if we were doing a nonreactive type of approach we just do that right but what happens if we don't have a terminal operation in reactive programming it's like a stream right it's lazy like I am it doesn't do anything until you till you subscribe to it and this is very important because this and I'm gonna tell you something that's technically wrong here but we're gonna go with it just for the purpose of the example and then we can clarify later publishers generally are cold publishers so no work takes place until they have a subscriber why is that important because if you have a ton of publishers spinning up and doing all this kind of crazy work in your application nobody's listening why it's a huge waste of resources so we're going to subscribe and that's when the party kicks off right we don't even have to do anything here we can just say subscribe and that's good enough but again being very visual people we like to do a system.out.print line so we can see what's going on and that's okay but we have a problem here not a bug because I make sure never to bug my code that way I don't have to debug saves a lot of time just a protip right but we have a small problem because I'm gonna be start stopping and restarting this several times and each time I do that it's going to try to save the same records over and over again and I know we're using so not all of them may say but you know I mean there it may happen a couple of them may take right we have to be careful so he's being a great sport about this I love this next time I'll pick on you know Couchbase or something it's all good lots of love anyway so I'm gonna just start off by doing a repo delete all and once again if we were doing an imperative application we could just do this right but but we can't because we're asynchronous here because we don't know that that will finish before the next statement starts so we need to somehow do something to make that happen now we can do this don't you feel dirty when you see that I mean this is non blocking code and if you start littering your non blocking code with block statements with block methods you should probably question your life choices right so I'm gonna remove that we need something though that kind of does a similar type thing we need to wait for the all clear from our upstream methods and then proceed there are several options I like vinne mini because it takes a publisher as the next parameter we just happen to have a publisher right here right the flux is a publisher and that that's nice that's pretty good right now though what happens is though that we're actually just printing out each one as we add it it would be really nice to validate that truly this is all we have in our data store so what I'm gonna do is just do another then many just chain them right why not and do a repo dot find all and now now we should be able to verify that this is truly all that we have in our data store now so let's kick off the party here hmm what did I forget so I have something wrong here no server chosen by really come on okay so let me go to my palm and see what's off here oh yes I forgot this was something they tightened up darn data team all right so I'm gonna restart this by default flapdoodle flapdoodle flapdoodle okay anyway by default flapdoodle assumes a a test scope we wanted to be running all the time for this application I mean I can run a local version of but I love flapdoodle so why not so we have here our coffees that have been saved so everything looks like it's working and that's great but at this point we just we're interacting with our data store reactively but that's it we've done nothing more so we have 12 minutes to build out our coffee service we can do this okay so easy peasy the first thing we want to do is create our internal service API so I'm gonna create a coffee service coffee service I'm going to inject my coffee repository bean and then let's define our Minimum Viable API right so I'm going to let's see we'll want to return a flux of all coffees right get all coffees that seems valuable useful return repo dot find all straight pass through so fast so easy we also want to return a mono of type coffee right so we can grab a particular coffee let's see get coffee by ID of course specifying specifying the ID they cannot be done already oh my we'll take their extra time okay so I'm gonna return repoed find by ID passing the ID that's great what else Oh our operational visibility right so will we return a flux of coffee orders so get orders orders orders by again string training coffee ID so now if we had millions of subscribers this would be very easy right we just kind of grabbed what's in our in memory database and we just spit out all the orders that are taking place as they come life we don't have a million subscribers yet in fact we don't have a subscriber yet so you know we're gonna have to kind of fake this till we make it I'm gonna just generate some values so look like we have subscribers on the hope that eventually we'll get seed funding and you know take over the world with this so we're going to do a return flux dot generate and let's generate some values so I'm going to create a sync and do a sync dot next create some new coffee orders using our coffee ID and let's see-oh also instant now so we can just generate these and and as it stands right now this will generate a ton of coffee orders just as fast as this little MacBook and run right but which is great for you know four systems but not so great for us seeing it so I'm gonna slow things down just a little bit to delay the elements so we can get maybe about one per second so we can see them as they flow through now IntelliJ is telling me it thinks I have a flux of objects here until I specify particularly coffee orders so this looks like it should work if only there were some way some way to test code before you put it in production if only there was some way to test code anyway I was talking to him earlier and he said you know there's this key combination I like to use and and he wasn't he was nice enough not to make fun of me but he did tell me I should try this so let's I'm gonna try this oh look it works you were absolutely right so apparently we can create tests we can test this code which is nice right I never lead with tests cuz nobody never come but we're gonna start off with a test we're gonna just test this one method and make sure it works we figure the pass-throughs work will assume that they will but we can write tests for those too I'm not gonna have time today but we can circle back that's in the repo right so I'm going to auto wire in my coffee service and we're going to get started with that so first thing to do is have reasonable descriptive names for your tests right so get orders and we're gonna take ten and just see how that works so we need a string we need a coffee ID right so we'll create a coffee ID we'll use our service to get all coffees block first and get the ID from that now unit tests are the one area where it's it's okay to block in a reactive application the reason being that imperative applications are relatively much easier to test I say relatively right because you typically you make a request you get a reason once you validate what you got versus what you expected and you move on now with a publisher and in reactive programming kind of works that way for the most part but what happens when you have a sporadic publisher if you expect values for who knows how long who knows how many you could get three values in three seconds and then another value in a minute and another value in a week that makes them some exceedingly long unit tests right so we need to have some way to compress that here's the good news between this and we're in this case all we're doing is just telling our datastore to give us back all the records but stop after the first one we don't even care which one just grab any old coffee it's fine the other thing that we have with reactor is the ability to time travel so who are my who've Ian's I feel so bad because I normally dress appropriately for this all I wore was was this shirt I normally wear a Doctor Who shirt when I'd give this talk but but we're gonna time travel it's so cool so this is the second best part of the whole talk after the coffee's right so we're gonna start off and use our friends step verify your step verifier dot with virtual time this is great because we can compress time so I'm going to create a supplier here and use my service to get coffees get all orders I should say for this particular coffee ID I'm going to take ten this is an example a very simple example by the way of back pressure I'm just having my subscribers specified in my publisher that it can only take ten values just give me ten no more or less just stop at ten and that brings us in on scope a little bit which is kind of nice I'm then going to await a certain amount of time now we wrote this code so we know we're going to get one value every second but we're time travelers right so we can we can be a little you know a little on the excessive side and be safe well we'll wait ten hours should be fine okay so we're gonna wait ten hours and then I'm going to just check in this case I'm going to just see that I got ten values I'm asking for ten I'm gonna verify I got ten and then let's just verify complete so let's run this and we should get a green bar right if everything is work we should get a green bar yeah we got a green bar our tests pass now that means one of two things either our code works or our test doesn't so we need to verify I always like to verify that my test is testing what I think it's testing so I'm gonna change this to expecting 11 values I'm still asking for 10 but I'm expecting 11 so we should get a red bar so let's let's hopefully get a red bar feels really weird to root for a red bar here but okay so it worked or didn't work as the case may be so I'm gonna change this back because I'm a little OCD and just make sure that we leave our tests in a consistent state and this should go green again yes okay so we're good there now there's there's a much greater testing story to tell what project reactor including web test client and things like that not anytime here today but again my repo has a little bit more along those lines so at some point please check that out but let's get back to our code so now we have our internal API defined we have our interactive our database interactions fully reactive let's define our external API now for who are my spring MVC developers in here so you used to seeing rest controllers right this is very familiar now this is the thing that I feel like we did really well on the spring team we created parallel packaging so we have spring in web MVC and spring web flux and we duplicated MVC is not going away there are a lot of mission-critical applications running around the world on spring MVC but we are also not telling you that you have to take all of your ten plus years of hard-won knowledge and throw it away and start fresh just to take advantage of a reactive stack so this will seem very familiar to you if you're used to creating rest controllers guess what you can still do this I'm gonna use request mapping to indicate that everything falls under my coffees URI to start off with right and I'm gonna create my coffee controller coffee controller and let's see so private final final coffee service I'm gonna inject my coffee service so I can use my internal API for my external API write code reuse is a good thing now I'm going to do a get mapping and let's let's create a method that returns a flux of coffees again we'll just call this all return service gets all coffee's again hats they're very simple we're leveraging code we've written and built and presumably at some point tested right so I'm gonna do another one to get map the the ID by a particular ID to get a particular coffee right so public mano of coffee by ID we're gonna use path variable to extract that ID and then we'll return service dot get coffee by d right and then finally let's see our other one was to return our coffee orders so get mapping /id slash orders now public flux of coffee orders in this case orders once again using a path variable to extract the ID return return service dot get orders for our ID and that's almost enough but rest controllers by default assume that they get all values they bundle them up and they ship them back in a single document we actually know that we're gonna be sending back one value per second ad infinitum so how do we do that well we you can use WebSocket you can use server sent events but in this case I mean it's a natural fit for server sent events we're producing one value per second just until heat death of the universe right so we're going to produce we're gonna indicate we're producing a media type of text event stream value and that indicates to our rest controller to produce a stream of server sent events so let's go ahead and run this and we'll see what we get wait how much time do we have two minutes we can do this this is fine okay so let's test this make sure it works let's see blow this up just a bit more and I use HTTP let's hit our coffees endpoint and that all works right we knew it would we don't make mistakes live coding everything always works so I'm going to check our particular coffee so we pull this down that works now let's see if we get our coffee orders and see if we stream those BAM okay so everything's coming through exactly as it should but we have a minute we can fix that right so I won't have time to show you the client but I would like to show you that if you are not coming from a spring and B C perspective if you're coming from a functional reactive type of approach you can change this out oh you know what we are right having five o'clock I will leave it to you as an exercise to check out the repo because I actually show both ways of doing things it's very nice you can define your own functional routing which is pretty slick so let's go back and we'll finish up here because sometimes I forget to show this and I want to make sure I get to that this is it right if you're interested in more information about the reactive streams initiative all the information all the goodies are up at the top link project reactor we have excellent documentation so that isn't a really good place to look every all the API information the the Javadoc the full textual explanation how to debug how to set up checkpoints how to do everything you'd possibly want to do tweak the scheduler it's all in there we did a really nice job on the docs I will say and then finally all the coding you saw here and much much more in both Java and Kotlin fork service and a clients are out in the bottom repo that that does have sub modules so be careful when you pull it down that you actually do the full thing to get all the sub modules so you get all the code but please do check it out and please do let me know what you think of it happy to have any get feedback so once we get the shot there okay so thanks for coming and go to [Applause] you
Info
Channel: GOTO Conferences
Views: 9,729
Rating: 4.4731708 out of 5
Keywords: GOTO, GOTOcon, GOTO Conference, GOTO (Software Conference), Videos for Developers, Computer Science, GOTOams, GOTO Amsterdam, Mark Heckler, Pivotal, Reactive Java, Spring Boot, Spring Boot 2, Reactive programming, Spring MVC, Reactive Streams, RxJava
Id: tSCyTUkVdoA
Channel Id: undefined
Length: 48min 18sec (2898 seconds)
Published: Fri Oct 26 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.