Using Spring to populate Hazelcast Map at Start-up

The application I work on has been using Hazelcast for awhile now, but today we had a new scenario where we wanted to initially populate the Hazelcast Map from a relational database but did not want the our Map to be backed by a data store.  We wanted our Map to continue to only be in memory (for performance reasons).

As a relatively “newbie” to Hazelcast, I initially focused on the MapLoader interface, but as the documentation on that interface explains, it is used when the Map is backed by a data store.

We ended up using Spring to populate our Map by using the init method on our beans.

Here is the relevant code…

First, with our Hazelcast Map the keys will be of type String and the values will be of type ProcessStats.  The ProcessStats class:

public class ProcessStats implements Serializable {
  private static final long serialVersionUID = 1L;
  private String processName;
  private Long total;
  public ProcessStats(String processName, Long total) {
    this.processName = processName; total;

  // Getters and Setters left out for brevity


Finally, here is the Spring bean that we used to populate the Map.

public class HazelcastCacheLoader {
 HazelcastInstance hz;

 public void init() 
   IMap<String, Object> processStatsMap = hz.getMap("processStats");
   List<Proccess> processes = // Retrieve this list from the database
   for (Process proc : processes)
     String key = proc.getProcessID();
     String processStats = calcStatsForProcess(proc);
     processStatsMap.put(key, processStats);


Posted in Hazelcast, Spring | Tagged , | Leave a comment

Spring Boot Web Application – Starting in Eclipse

I am going through the Spring Boot Getting Started Tutorial and trying to follow the steps in Eclipse (and not STS).  I made it to the end and was thrown off a little by how to “start” it up.

I wanted to create a Tomcat Server within Eclipse for the project and start.  However the tutorial does not mention that step, instead saying to run the Maven command:

mvn spring-boot:run

I was initially confused as I was wanting to start my Tomcat server within Eclipse, perhaps with some additional start up parameters.  NO!

Instead do the following steps:

  1. In your Project Explorer view, right click and select Run As -> Maven build…
  2. On the Edit Configurations dialog, in the “Goals” field enter:  mvn spring-boot:run
  3. Finally, click Run

That Maven goal will actually start up an embedded Tomcat server (behind the scenes).  It will not use any Tomcat server you have have created in the Eclipse Servers view.

In the end, the tutorial steps are correct, it was just me not following the steps.


Posted in Eclipse, Maven, Spring, Spring Boot, Tomcat | Tagged , , , , | 3 Comments

Spring Boot Application and Tomcat error: A child container failed during start

I was attempting to create my first Spring Boot web application this afternoon by following the Spring Boot Getting Started Tutorial on, yet attempting to do the steps in Eclipse Luna with Java 8 and an embedded Tomcat 7 server.

Upon trying to start the Tomcat 7 server from within Eclipse, I received the following error – Notice hidden in the error stack: Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName():

SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/MavenWebDemo]]
 at java.util.concurrent.FutureTask.get(
 at org.apache.catalina.core.ContainerBase.startInternal(
 at org.apache.catalina.core.StandardHost.startInternal(
 at org.apache.catalina.util.LifecycleBase.start(
 at org.apache.catalina.core.ContainerBase$
 at org.apache.catalina.core.ContainerBase$
 at java.util.concurrent.ThreadPoolExecutor.runWorker(
 at java.util.concurrent.ThreadPoolExecutor$
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/MavenWebDemo]]
 at org.apache.catalina.util.LifecycleBase.start(
 ... 6 more
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
 at org.apache.tomcat.websocket.server.WsServerContainer.<init>(
 at org.apache.tomcat.websocket.server.WsSci.init(
 at org.apache.tomcat.websocket.server.WsSci.onStartup(
 at org.apache.catalina.core.StandardContext.startInternal(
 at org.apache.catalina.util.LifecycleBase.start(
 ... 6 more

After some searching, I found the solution buried in the Spring Boot documentation for how to embed Servlet containers.  It was as simple as adding a “tomcat.version” property to your Maven project’s pom.xml file:



Spring Boot defaults to Tomcat 8, so I needed the tomcat.version property to tell Spring Boot that I intend to target Tomcat 7.

I restarted my server and it started right up!


Posted in Java 8, Maven, Spring, Spring Boot, Tomcat | Tagged , , , , | 6 Comments

Drupal Superfish Menu is rendering without styles

I am upgrading a Drupal website I administer and decided to install the Superfish module for the first time.  As part of the installation, I also uploaded the to my /sites/all/libraries directory and then extracted all the files.  I then went through the rest of the install steps, including enabling a Superfish block.

However, when I went to view my menu on my website, the menu options and links were all there but there was no menu-like styling or behavior.  I inspected the HTML using Chrome Developer Tools and each of the menu link elements had Superfish CSS classes added to them.  The problem was the CSS files themselves were not being found.

The solution was simply to rename the Superfish directory in my /sites/all/libraries directory from “Superfish-for-Drupal-master” to “Superfish”.  I still feel like this is a missing step in the installation steps (or at a minimum isn’t called out as it should).

I hope this helps someone else as I spent way too much time on this!

Posted in Drupal | Tagged | Leave a comment

Using Matches for null parameter with Mockito

I was using Mockito to write a unit test today and had a brief struggle trying to write a Matcher for a parameter I expected to be null.

Here is the method in my interface that was causing problems:

public interface ItemService
    List<Item> findItems(String query, Map<String, String> qualifiers);

In my unit test I started with writing this for argument verification since I expected the second parameter for the Map to be null:

 verify(itemService).findItems(anyString(), null);

This resulted in the following error when running the unit test:

Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.stevewall123.ItemServiceTest.testFindITems(

The error is pretty straight-forward.  Once you use a Matcher on one argument then they need to be used on all arguments.  To fix the error, I needed to use the Matchers class isNull() method.  This method will verify the argument is null.  So in my unit test, I changed the argument verification line to:

 verify(itemService).findItems(anyString(), isNull(Map.class);

And now the test passes!

Posted in Java, Mockito | Tagged , | Leave a comment

FindBugs warning with Guava’s Predicate interface

When running FindBugs on some code I wrote, I received a  NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE warning:

In class com.wall.steve.HelperMethods$1
In method com.wall.steve.HelperMethods$1.apply(MyItem)
Parameter item
At[line 27]

This was the code I had written:

//com.wall.steve.HelperMethods class
public static final Predicate<MyItem> NON_REJECTED_ITEMS = new Predicate<MyItem>() {
    public boolean apply(final MyItem item)  //This is Line 27
        return !item.isRejected();

The issue was with my implementation of the apply() method.  Guava has the parameter of the apply method marked @Nullable.

//Guava 18.0 - Predicate class 
boolean apply(@Nullable T input);

FindBugs flagged my implementation since it does not allow for the item parameter to be null.  To fix this issue, I used the @Nonnull annotation (from the javax.annotation package):

//com.wall.steve.HelperMethods class
public static final Predicate<MyItem> NON_REJECTED_ITEMS = new Predicate<MyItem>() {
    public boolean apply(@Nonnull final MyItem item)  //Line 27
        return !item.isRejected();

This question on Stack Overflow was very similar and provides some more background in answering “why”.

Posted in Guava, Java | Tagged , | Leave a comment

My Presentation at the Twin Cities Java User Group

This week I gave a presentation titled “What’s New in Java 8” at the November meeting of the Twin Cities Java User Group (TCJUG).

Topics covered include:

  1. Interfaces – static and default methods
  2. Lambdas
  3. Functional Programming
  4. Streams API
  5. New java.time API
  6. Nashorn JavaScript Engine
  7. Other new classes and methods

The code I wrote during the presentation can be found on my GitHub site.

Posted in Java, Java 8 | Tagged , | Leave a comment