Node.js Learning Path: Unit 5 – The Event Loop

Hey everyone! If you want to learn Node.js, then check out my Node.js Learning Path, which you can find only at IBM Developer.

The Event Loop is the key to Node’s scalability.

Unit 5 of the LP is about the Event Loop, and in it you’ll learn stuff like:

  • The phases of the Event Loop
  • How to write code that runs in the various phases (so you know when your code will run)
  • The Node Events API
  • Node Streams

There’s a quiz to help you deepen your understanding of the material, and a video too:

Ready to dive into the Node Event Loop? Then click here to jump right to Unit 5.

Want to take the entire course? Then click here to start at the beginning, with Unit 1.

Either way, have fun, good luck, and thanks for reading!

–jsp

Advertisement

Process Substitution and SHA256 Checksum

Hey everyone!

When you verify the SHA256 checksum of a downloaded file you can copy the checksum from the website, paste it into an editor, run the shasum -a 256 command, paste the checksum into that same editor and compare them by eye.

Or, use the diff command and process substitution to do it, and never have to trust your eyeballs again (you should still keep them open while driving though).

I put together this video to show how easy it is to do this. Enjoy!

Thanks for reading!

–jsp

 

Kitematic: Use This Tool

Hey everybody!

If you use Docker and don’t use Kitematic, you’re missing out!

TL;DR

Kitematic is an awesome addition to your Docker toolbox. Download it here.

What is Kitematic?

It’s a graphical tool that lets you see what is running in your local Docker environment.

You’re probably used to docker ps, and docker images, and so on, to see what Docker is doing. But Kitematic can show you that information in a GUI.

So?

Okay, so I’m working on Part 3 of a tutorial series I’m doing for IBM developerWorks, and I have Hyperledger Fabric running in Docker.

Contrast this:

Ix:~ sperry$ docker ps
 CONTAINER ID        IMAGE                                                                                                                        COMMAND                  CREATED             STATUS              PORTS                                            NAMES
 c8e71d740f38        dev-peer0.org1.example.com-digitalproperty-network-0.14.3-b524d4b38bf773adade74e18741aacd93212883d30ef9b63def86797a270f038   "chaincode -peer.a..."   44 minutes ago      Up 44 minutes                                                        dev-peer0.org1.example.com-digitalproperty-network-0.14.3
 2931d5073686        dev-peer0.org1.example.com-perishable-network-0.14.3-02848cbd757516cb261270e628f79e44a5d478431848d48302bac059fa1b3fd5        "chaincode -peer.a..."   21 hours ago        Up 21 hours                                                          dev-peer0.org1.example.com-perishable-network-0.14.3
 9e4edf0acb21        hyperledger/fabric-peer:x86_64-1.0.3                                                                                         "peer node start -..."   21 hours ago        Up 21 hours         0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp   peer0.org1.example.com
 7392660e2da1        hyperledger/fabric-ca:x86_64-1.0.3                                                                                           "sh -c 'fabric-ca-..."   21 hours ago        Up 21 hours         0.0.0.0:7054->7054/tcp                           ca.org1.example.com
 61ffea441cc6        hyperledger/fabric-orderer:x86_64-1.0.3                                                                                      "orderer"                21 hours ago        Up 21 hours         0.0.0.0:7050->7050/tcp                           orderer.example.com
 8034e8baa63e        hyperledger/fabric-couchdb:x86_64-1.0.3                                                                                      "tini -- /docker-e..."   21 hours ago        Up 21 hours         4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp       couchdb
 Ix:~ sperry$

With this:

Screenshot 2017-11-06 11.43.13

You can see the same info, but in a GUI format. Now, I love the command line as much as anybody, but sometimes figuring out what is going on with Docker through the command line can be frustrating. Not that the information isn’t there; just that it’s not super easy to tease out the information I’m looking for.

What else?

With Kitematic, you can see the logs for each container, just as you can with the docker logs command.

Compare this:

$ docker logs 61ffea441cc6

2017-11-05 20:19:38.265 UTC [orderer/main] main -> INFO 001 Starting orderer:
 Version: 1.0.3
 Go version: go1.7.5
 OS/Arch: linux/amd64
2017-11-05 20:19:38.274 UTC [bccsp_sw] openKeyStore -> DEBU 002 KeyStore opened at [/etc/hyperledger/msp/orderer/msp/keystore]...done
2017-11-05 20:19:38.274 UTC [bccsp] initBCCSP -> DEBU 003 Initialize BCCSP [SW]
2017-11-05 20:19:38.275 UTC [msp] getPemMaterialFromDir -> DEBU 004 Reading directory /etc/hyperledger/msp/orderer/msp/signcerts
.
.
2017-11-06 16:49:34.979 UTC [fsblkstorage] updateCheckpoint -> DEBU a7f Broadcasting about update checkpointInfo: latestFileChunkSuffixNum=[0], latestFileChunksize=[213666], isChainEmpty=[false], lastBlockNumber=[7]
2017-11-06 16:49:34.979 UTC [orderer/multichain] WriteBlock -> DEBU a80 [channel: composerchannel] Wrote block 7
2017-11-06 16:49:34.979 UTC [fsblkstorage] retrieveBlockByNumber -> DEBU a81 retrieveBlockByNumber() - blockNum = [7]
2017-11-06 16:49:34.979 UTC [fsblkstorage] newBlockfileStream -> DEBU a82 newBlockfileStream(): filePath=[/var/hyperledger/production/orderer/chains/composerchannel/blockfile_000000], startOffset=[205101]
2017-11-06 16:49:34.979 UTC [fsblkstorage] nextBlockBytesAndPlacementInfo -> DEBU a83 Remaining bytes=[8565], Going to peek [8] bytes
2017-11-06 16:49:34.979 UTC [fsblkstorage] nextBlockBytesAndPlacementInfo -> DEBU a84 Returning blockbytes - length=[8563], placementInfo={fileNum=[0], startOffset=[205101], bytesOffset=[205103]}
2017-11-06 16:49:34.979 UTC [orderer/common/deliver] Handle -> DEBU a85 [channel: composerchannel] Delivering block for (0xc4208547c0)

With this:

You can see the logs (under the Home tab on the right-hand side of the UI), in addition to information about the port that is exposed on localhost, the volumes that are mounted, and settings for both.

Get Kitematic

How do you use Kitematic? Go to the Docker drop-down menu in the status tray (mine is at the top of the screen on my Mac),

And select Kitematic. The first time you do this, you’ll see this dialog:

Click the “here” link (which takes you to https://download.docker.com/kitematic/Kitematic-Mac.zip). Once the ZIP file is downloaded, unzip it. It contains the Kitematic app, which you then drag and drop into your Applications folder. The next time you select Kitematic from the Docker drop-down, it fires up seamlessly.

You can sign into Docker Hub if you have an ID (if not, just select “Skip for Now”).

That’s it! Now you can use Kitematic.

Fini

I’m not suggesting you shouldn’t use the Docker command line tools. They’re great. But Kitematic is another tool in your Docker arsenal.

Enjoy. And thanks for reading!

–jsp

Container Orchestration with Kubernetes, Part 1

Why Containers?

Hey everybody! In this post, I’d like to talk about Container-centric infrastructure with Kubernetes. This is Part 1 of a series of posts on using installing and using Kubernetes.

Let’s start with the kubernetes documentation.

(Source: https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/#why-containers)

Host-Centric infrastructure

Before containers, we installed applications directly on the target host, along with libraries our apps needed, plus the libraries any other applications needed as well.

At best, it presents DevOps with maintenance challenges. And at worst, integration conflicts between applications, versions of dependent libraries, and so on, can lead to poor application performance and even downtime.

Container-Centric infrastructure

With containers, on the other hand, all the applications run in their own isolated mini environments, away from each other, and the underlying OS.

And this is what you get with Kubernetes!

Conclusion

Container-centric infrastructure like that provided by Kubernetes has tremendous benefits, and in Part 2 I’ll give you an overview of Kubernetes, and talk about Pods, Nodes, and the “desired state” of the cluster. Stay tuned for that.

Want to know more?

If you want to learn more about Kubernetes, check out this post at IBM developerWorks: What is Kubernetes?

Check out this video at IBM developerWorks TV, where I show you everything I covered in Parts 1-5 of this series:

Introduction to Kubernetes:

Thanks for reading!

–jsp

What is Apache Kafka?

Change the way you think about data.

You’ve probably heard of Apache Kafka. You know, that Big Data, publish/subscribe message broker, time ordered log thingamajig that integrates all your application data, and provides real-time materialized views while being crazy scalable?

Yeah, that one. In this article at the IBM developerWorks Blog, I talk about Apache Kafka. I’ll assume you’re the  perfect candidate for applying Kafka, and talk about your architecture today. Then I’ll describe where you want to be, what is blocking the path to your getting there, and finally, how Kafka can help.

Check it out if you get a chance. Kafka is really cool.

And don’t forget to follow my blog, and follow me on Twitter @jstevenperry

–jsp

What is Node-RED?

Ah, the Internet of Things (IoT for all you acronym aficionados). It’s awesome, right? Throw in some JavaScript, and a browser, and what do you have? Node-RED!

Node-RED was created by IBM’s Emerging Technology Services team, and allows you to create functionality by wiring together flows of data between nodes using a browser. And it has gained tremendous popularity in the IoT space, by modeling bits of application functionality between IoT devices like sensors, cameras, and wireless routers.

But it’s so much more than that, and in this article, I’ll break it down for you.

What Problems Does Node-RED Solve?

It’s helpful to understand the problems a technology solves, or you might misapply it. So what problems does Node-RED solve?

High Barrier to Entry

Let’s face it, not everyone writes great code (I’m certainly not talking about you or me, of course). Coding has a high barrier to entry that can sometimes stifle innovation due to the gap between the idea and the code to implement it.

What happens to all those great, innovative ideas out there because nobody is around to code them?

Not only that, but not everyone thinks in code. Some people are more visual.

Prototypes are critical to innovation

If you’re going to pitch an innovative idea, you need something to show. You know, a straw man. That means you’ll need a prototype.

Let’s say you build a prototype. Now what? Prototypes are often just toy applications. How do you get from prototype to something useful?

And what about someone who doesn’t code? How would they build a prototype?

What Node Red is NOT

A new technology stack

Node-RED uses battle tested technologies as part of its technology stack like Node.js, JSON for metadata, JavaScript for coding, and almost any web browsers.

Of course, nodes can be written to allow processing in pretty much any way you like. Node-RED is super flexible!

XML based

I’m not terribly fond of XML – not because it’s not awesome but because in practice it’s so often misapplied (application metadata comes to mind) – and Node-RED doesn’t need a single shred of XML metadata to function.

Sure, there are contribs that allow you to work with XML, but the metadata of a Node-RED flow is not built on XML (but much more – IMHO – readable JSON).

A toy

Build a prototype in Node-RED and wow your customer. Then build on that prototype to create the finished product, whether that is a single new function, a collection of new functionality, or an entire application from browser to database.

Serious application functionality can be built using Node-RED. Don’t take my word for it. Using IBM’s BLOOP BLURP

Just for IoT

While Node-RED shines at the Edge, it can be used to solve pretty much any problem that can be modeled as a flow-based program (FBP).

An FBP is made up of processing components (the nodes) that process data (e.g., transform it from one form to another) and write the data out to other components to which they’re connected in a network of such components. FBP lends itself well to problems that don’t naturally fit traditional, sequential program models (e.g., multiplexing, and recursive programs).

The Solution

Enter Node-RED

Node-RED is:

  • A browser-based flow editing tool,
  • Built on Node.js, and
  • Designed for reuse – lots of pre-built nodes, or build your own and share them!

Okay, you may be thinking, how does that solve the problems listed earlier?

High Barrier to Entry

First off, Node-RED is simple to setup and use. You just install a few things (like Node.js, and the node-red package) and it’s ready to use. Seriously.

Then, once you’ve fired up Node-RED, you can use is to create flows visually. See, Flow-based programming is all about the flow of data: data in, data out. Visualize the data, Neo.

Finally, using Node-RED’s graphical flows makes building prototypes a snap:

  • Select pre-built nodes from the palette
  • Wire them together
  • Test
  • Repeat

Conclusion

So what are you waiting for? Give Node-RED a spin today.

You’ll be glad you did.

–jsp

Java News and Code, Episode 14

Welcome

Welcome to Java News and Code, where I tell you about interesting goings-on in the world of IT in general, and Java in particular.

And there’s always code.

On This Episode:

  • Oracle wants to open source Java EE
  • Android Oreo is here
  • Code: A custom Maven archetype for JUnit 5

TLDR;

TODO: VIDEO HERE

Oracle wants to open source Java EE

On August 17th, Oracle Software Evangelist David Delabassee said Oracle wants to open up Java EE.

The announcement on the Oracle Blog says that as the summer ends, releases wrap up, and JavaOne 2017 approaches, this is an “…opportunity to rethink how Java EE is developed in order to make it more agile and responsive to changing industry and technology demands.”

As InfoQ points out, Oracle has been criticized regarding how it’s steered Java EE in the past, which has led to efforts such as the Java EE Guardians, a group “…committed to moving the Java EE platform forward through active community participation and advocacy.”

Java EE Guardians’ members include James Gosling, the Father of Java, and Cameron McKenzie, editor-in-chief of the Server side. It’s not clear what qualifies someone to be a Java EE Guardian, but qualifications span the spectrum from Father of Java, to Java Blogger, to this Java EE Guardian, whose qualifications include Student.

Reaction outside Oracle seems to be positive, with IBM’s Ian Robinson weighing in on the WASDev blog saying, “…we are delighted that Java EE is moving with the times to an open foundation for its ongoing development following the completion of Java EE 8 this year.”

So who will take over Java EE?

Responses to an unofficial Twitter poll by Java EE Guardian Reza  Rahman asking that question seem overwhelmingly in favor of handing stewardship of the platform over to Apache.

So what does this mean for Java EE? It depends on whom you ask, but given the popularity and prevalence of the platform, I’m sure it will stay in good hands.

Be sure to check out the show notes page, where you can find more information on this story, and everything I talk about in this episode.

Android Oreo is here

We’ve been following Android O for some time now here at Java News and Code, and at long last, Android O, named Oreo, has been released!

For users, Android Oreo has a bunch of new features, some of which are immediately obvious like picture in picture, and per-app notification options.

Other changes like background limits, are designed to improve device responsiveness and extend battery life.

For developers, things get more complicated. The optimizations and improvements come at a cost, which will be paid on the development side. And rightly so.

What does that mean for developers like me, who have apps in the Play Store?

For now, probably not much. Oreo is not even showing up on the Android Dashboard as of today, but of course, it’s only a matter of time.

Android Marshmallow has the single greatest share of installations, and it was released in October of 2015, coming on two years. So we have time.

But as developers we need to be ready. Fortunately, there is information available already to help us get ahead of this, like this video on the Android Developers YouTube channel.

This release is huge, there’s no denying it. And in my opinion totally worth it. Check out this article at Gizmodo, that lays out 11 cool new features in Oreo.

Be sure to check out the show notes page, where you can find more information on this story, and everything I talk about in this episode.

Code: A custom Maven archetype for JUnit 5

On August 24th, JUnit 5, Release Candidate 3 was released.

This release, which signals the impending General Availability (GA) Release, includes the usual spate of bug fixes, breaking changes, and new features.

The GA release is scheduled for September 6th, according to the milestones page.

A Maven archetype is a special type of Maven project that is used to generate other Maven projects. If you’ve ever used the New Project wizard in Eclipse to create a Maven project, chances are you’ve used the maven-quick-start archetype. Guess what? That’s a Maven archetype!

For this episode’s code talk through, I want to show you how to create a Maven archetype that you can use to create new projects that will come with JUnit 5 dependencies and boilerplate code.

The source code for the archetype is available in GitHub. And in this episode I want to do a quick walk through of the code for a Maven archetype, how it’s structured, and how to use it to generate a new project.

Let’s get started.

First, clone the code from GitHub. Open a Terminal window, navigate to the directory where you want the code to land and execute the git clone command:

git clone https://github.com/makotogo/JUnitJupiterArchetype

Next, import the code into Eclipse.

With the code in Eclipse, let’s take a quick tour. Expand all of the nodes in the source tree. Open the src/main/resources/META-INF/maven/archetype-metadata.xml file, which tells the archetype generator what source files, test  source files, resources and so forth you want to be included in new projects generated from the archetype.

A Maven archetype project is itself a Maven project, so there are two POM files to deal with:

The one that controls the archetype project itself, located in the project root:

  4.0.0
  com.makotojava.learn
  junit-jupiter-archetype
  0.9.1
  Archetype - jupiterArchetype
  http://maven.apache.org
  jar

And the POM file that will be included in any projects created from the archetype, src/main/resources/archetype-resources/pom.xml.

The archetype project also includes boilerplate code like this:

package $package;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.log4j.Logger;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

/**
 * Unit test for simple App.
 */
@RunWith(JUnitPlatform.class)
public class AppTest {
  
  private static final Logger log = Logger.getLogger(AppTest.class);

  private App classUnderTest;

  @BeforeEach
  void setUp() {
    classUnderTest = new App();
  }

  @Test
  @DisplayName("assertAll on a bunch of Strings should succeed.")
  public void doIt() {
    log.info("Testing doIt()...");
    assertAll(
        () -> assertEquals("String", classUnderTest.doIt("String")),
        () -> assertEquals("StringToo", classUnderTest.doIt("StringToo")),
        () -> assertEquals("String2", classUnderTest.doIt("String2")),
        () -> assertEquals("StringAlso", classUnderTest.doIt("StringAlso")));
  }

}

To use the archetype to create new projects, you first have to build the archetype project itself. Like I said, it’s a Maven project, so to build it go to Run As > Maven Project and specify the install and archetype:crawl goals to install it in your local Maven repository, and update your local archetype catalog, respectively.

Next, make sure the archetype is included in your local Maven archetype catalog, which is located in the repository root in a file called archetype-catalog.xml. And there it is.

Screen Shot 2017-08-28 at 12.07.50 PM

Now, I can use that to create projects in Eclipse. First, make sure Eclipse recognizes the local maven catalog. Under Preferences > Maven > Archectypes, click on Add Local Catalog. Browse or enter the path to your local archetype-catalog.xml file, give it a description you’ll recognize later, click Ok. Then Click Apply and close.

To create a new project based on the new archetype, go to File > New Project > Maven Project. Click Next. Click Next again. Select the archetype in the list, and complete the wizard.

Screenshot 2017-08-28 12.14.44

When the wizard completes, you now have a new project based on your custom archetype!

How cool is that?

For a detailed walkthrough of this, check out this video.

Fini

Well, that’s it for today’s episode. Be sure to check out the show notes page, where you can find more information on this story, and everything I talk about in this episode.

Thanks for watching Java News and Code (and reading this post).

I’ll see you next time.

–jsp

Quickly Diff Two Strings in Bash Shell

Hey everyone!

I am working on a Raspberry Pi 3 project (my first, I’m embarrassed to say :-p) and downloaded NOOBS OS installer to install Raspbian on my new Pi.

Even though I torrented the ZIP file, I still wanted to check the SHA256 checksum of the download. I’m careful that way.

Here’s what I want to do: compute the SHA256 hash of the NOOBS Zip file and compare that to the checksum from the website:

Screenshot 2017-08-25 09.56.05

Drop out to a command window. Enter this command (I don’t want the file name, so I run it through sed after the shamus command):

shasum -a 256 ./NOOBS_v2_4_3.zip | sed 's/^\(.[a-f0-9]*\) .*$/\1/'

Which returned:

94790b8d87086d46d16413bd1967e3f5eb709cb5e124d8213d40e5707da18437

Now, copy the line from the Terminal window, open a text editor, paste it in. Hit enter to get a new line in the editor, copy the SHA-256 hash from the NOOBS download page, and paste that in below it:

Screenshot 2017-08-25 10.09.00

By eye, I can easily see these match, but I thought, “Sheesh, what a lot of work! There has to be a better way!” Spoiler alert: there is!

I found this post at StackOverflow, which linked to this post about process substitution.

So through a single command, I can compute the SHA-256 hash of the downloaded ZIP file, and compare that to the checksum from the NOOBS downloads page (okay, okay, I still have to manually copy the checksum from the downloads page and paste it into the command line after the echo):

diff <(shasum -a 256 ./NOOBS_v2_4_3.zip | sed 's/^\(.[a-f0-9]*\) .*$/\1/') \
<(echo 94790b8d87086d46d16413bd1967e3f5eb709cb5e124d8213d40e5707da18437)

Note: I’m using “\” to indicate the line is too long for WordPress to display, but the command works just fine with the backslash in there. Here’s a screenshot in case you need further convincing (I ran it twice, once with and once without the backslash):

Screenshot 2017-08-25 10.39.55

Voila! When the command line comes back with no output (as diff does when there are no differences between “files”), I know the checksum is good!

So, to sum up, the basic format for this command (assumes you’re checking an SHA-256 hash) is:

diff <(shasum -a 256 /path/to/file/FILE_TO_CHECK | \
sed 's/^\(.[a-f0-9]*\) .*$/\1/') <(echo CHECKSUM_FROM_WEBSITE)

Hope you enjoyed this tip. Thanks for reading!

Now if you’ll excuse me, I have a Raspberry Pi 3 project to get to!

–jsp

Injecting Parameters into JUnit 5 Unit Tests (@ Baeldung.com)

Check out my first (hopefully of many!) post at Baeldung.

The post is called: Inject Parameters into JUnit Jupiter Unit Tests

If you’ve been wanting to make the move to JUnit 5, now is the time! JUnit 5 Release Candidate 2 is out (as of this writing), and the final release is getting closer every day!

I love writing about software testing. I believe as developers we don’t pay enough attention to unit testing (myself included), so it’s important to me to get the word out: unit testing is important!

There, I said it.

–jsp

p.s., Keep checking back here for more information on JUnit, including tutorials, like this one I wrote for IBM developerWorks.

Spring Boot Basics

Hello, World! Would you like to learn Spring Boot? I have written a new tutorial for IBM developerWorks that shows you how to use Spring Boot.

The tutorial starts off with a simple Hello World (actually, Hello Spring Boot) application.

Then I crank up the volume and show you a more complicated use of Spring Boot with a Spring MVC application called SpringBootDemo.

You can access the tutorial at IBM developerWorks by clicking this link.

A video accompanies the tutorial. You can access it from the tutorial, view it embedded below, or click here to view it on YouTube.

Please let me know what you think. Thanks for reading!

–jsp