Tales from the jar side: Detecting if a website is up, Null Pointers at JavaOne, Niemann strikes back, and funny tweets
A ghost walks into a juice bar, says "I want some BOOOOZE". Bartender says, "Sorry, we don't serve spirits here." <rimshot>
Welcome, fellow jarheads, to Tales from the jar side, the Kousen IT newsletter, for the week of October 16 - 23, 2022. This week I taught week 2 of my Spring and Spring Boot in 3 Weeks course on the O’Reilly Learning Platform, my Latest Features in Java course as an NFJS Virtual Workshop, and my Getting Started with Spring and Spring Boot course on the O’Reilly Learning Platform, all while attending the JavaOne conference in Las Vegas, NV.
Regular readers of this newsletter are affectionately known as jarheads, and are far more intelligent, sophisticated, and attractive than the average newsletter reader. If you wish to become a jarhead, please subscribe using this button:
As a reminder, when this message is truncated in email, click on the title at the top to open it in a browser tab formatted properly for both the web and mobile.
Is A Site Up, Programmatically
In both my Spring courses and my Latest Java course, I use an HTTP client to access a third-party RESTful web service. That’s always a bit risky, because third-party services, by definition, are not under my control. They can come and go, and can just be unavailable for a time.
One of the services I’ve used for years is called ICNDB, which stands for the Internet Chuck Norris Database. It provides Chuck Norris-type jokes, like:
Time waits for no man, unless that man is Chuck Norris.
That’s not a bad joke, and you can change the name of the hero, which I like to do. Mostly I use my friend Venkat Subramaniam and restrict my gags to nerdy ones, like:
Venkat Subramaniam can delete the Recycle Bin.
The site has been flaky lately, and, sure enough, when I went to use this week, it was down. I have a feeling it may not be coming back, either. That’s not a crisis or anything, since there are other services I can use instead. I often use the NASA services from Open Notify, like the one to determine how many astronauts are in space at any given moment. That’s what I did this week, when I had to replace an existing exercise on the fly.
When the ICNDB site went down, however, it caused some of my tests to fail on my GitHub repositories. This was a perfect opportunity to use a neat feature in JUnit 5, which is called Assumptions. The idea behind assumptions is that they are precondition checks. A precondition is a boolean expression that must be true before you execute your test, or there’s no guarantee the post-conditions will be true. In other words, if the preconditions aren’t true, the test is not expected to pass. If you use an assumption in Java 5 and it doesn’t succeed, the test is aborted, not failed.
A precondition on calling a RESTful web service is that the site you’re about to access is up and available. If the site’s not up, obviously there’s no point in trying to access it. The question is, how can you tell, programmatically, if a site is up?
One way I’ve tried in the past is to use the isReachable(int timeout)
method of the java.net.InetAddress
class in Java, which waits for a timeout period in milliseconds. That means my assumption would be:
Wait a maximum of 2 seconds for the site to respond, because for the services I tend to use, they either respond right away or not at all. This check frequently doesn’t work, however, in that the assumption passes but the actual test doesn’t, resulting in a failure rather than a skipped test.
The other approach I take, which tends to be more reliable, is to make an HTTP HEAD request before my regular GET request. A HEAD request is asking for the response headers, but no body.
That changes the test to:
The HEAD()
method in the HttpRequest.Builders
class was added in Java 18. Prior to that, in order to make that type of request, you used:
.method(“HEAD”, BodyPublishers.noBody())
which is what I have in my Java 17 code. If you look in the Javadocs for HttpRequest.Builders
in Java 18, it marks the new HEAD
method as a default method in the interface, meaning it has an implementation in the interface itself. Sure enough, when I dug into the source code (for Eclipse Temerin, Java 18), I saw:
That gave me an excuse to use Java 18 in my Latest Java course. I have very few other reasons to do so, other than demonstrate the trivial web server created by the jwebserver
command. The whole issue gives me a chance to discuss programming by contract in class, though, which is all that stuff about pre- and post-conditions, so that’s good.
I plan to update all my tests for web services to use that assumption from now on.
Null Pointers at JavaOne
A few years ago I got the opportunity to join the Null Pointers, the rock band of Java developers that performs a couple times a year at conferences like DevNexus and JavaOne. I’m the lead singer in the group, which features Frank Greco on lead guitar, Jim Weaver and Zoran Sevarac on rhythm guitars, Paul and Gail Anderson on bass, Freddie Guime on keyboards, Andy Schwaderer on drums, and, this time helping out with vocals, Brian Vermeer. Here we are at the developer event at the end of the day Tuesday:
The CT Java Users Group feed got a short clip:
You mostly hear Brian singing rather than me, but that’s probably a good thing this time. I’ve been being careful about getting COVID — I’m double vaxxed and triple boosted — but right after my trip to New Orleans I came down with a stupid cold that went right for my vocal chords. My flight to JavaOne was on Monday, and on the Sunday before I was seriously debating whether to check in for my flight at all, given that I could hardly speak above a whisper. As you can see, I eventually decided to push through and go, and somehow sang over my cold.
We had another performance the next night at a Java Leaders party:
(Hi Dave, btw, who kindly tweeted that. It turns out he’s also a jarhead, and it was great getting a chance to talk to him at the conference.)
Given that I also taught online classes four days this week, I’m honestly not sure how I got through it all, but as the saying goes, if it was easy, anybody could do it.
We really did have fun, and we had great crowds both nights. I’m glad I went. I just hope next time I’m healthy enough to enjoy it more.
The American Way
For the past few weeks I’ve been summarizing new developments in the Hans Niemann chess cheating saga. Hans is an American. He was born in San Francisco, CA, spent a few years in the Netherlands, but currently resides in Connecticut. This week, he proved beyond the shadow of a doubt how American he truly is by participating in that most American of activities — filing a lawsuit.
Hans sued World Champion Magnus Carlsen, the Play Magnus Group, Chess.com, Daniel Rensch (head of Chess.com) and even streamer GM Hikaru Nakamura for at least $100 million for defamation and unlawful collusion in their cheating allegations against him.
His own tweet said it all:
The actual lawsuit can be read in pdf form here, if you are so inclined. Lawyer Akiva Cohen went through it in a tweet thread for the rest of us:
Let’s just say it’s a longshot at best, but hey, this is America. Anybody can sue anybody over anything. If nothing else, this will keep the whole situation in the news for quite a while.
By far the best tweet response I saw was:
Normally I’d explain that for those who might have come in late, but not this time. Sorry.
Incidentally, Niemann played pretty well in the US Championships this week. He won three out of his last four and drew the other, and finished in a tie for 5th. Not bad for a 19-year-old kid who believes the entire world is conspiring against him.
Other Tweets
AirBnB: 10 Downing Street
There were a couple of great tweets about the chaos surrounding Britain’s Prime Minister elections.
This video shows the selection process going on:
I fully admit that as an American, our system is no better, but it’s either laugh or cry, right?
Sweet Caroline
This makes no sense, but I couldn’t stop watching and laughing:
Editing my book
Now that my conferences are done, I can get back to editing my book, Mockito Made Clear.
OMG so much this. Still, you can get Mockito Made Clear at a discount using the coupon code kkmockito35, and it’ll be updated with the final copy when it’s available.
I do believe in spooks
Finally, as the tweet says, if ghosts aren’t real, explain this:
Have a great Halloween, everybody. 👻
As a reminder, you can see all my upcoming training courses on the O’Reilly Learning Platform here and all the upcoming NFJS Virtual Workshops here.
Last week:
Week 2 of Spring and Spring Boot in 3 Weeks on the O’Reilly Learning Platform
Latest Features in Java, an NFJS Virtual Workshop
Getting Started with Spring and Spring Boot, on the O’Reilly Learning Platform
Two performances with the Null Pointers, the rock band of Java developers at the JavaOne conference.
This week:
Week 3 of Spring and Spring Boot in 3 Weeks on the O’Reilly Learning Platform
Deep Dive: Spring and Spring Boot, an NFJS Virtual Workshop
Modern Java: Functional Programming with Streams, Lambdas, and Method References, ditto