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) {
    super();
    this.processName = processName;
    this.total= 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;

 @PostConstruct
 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 , , , , | 2 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 spring.io, 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.report(FutureTask.java:122)
 at java.util.concurrent.FutureTask.get(FutureTask.java:192)
 at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
 at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:799)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/MavenWebDemo]]
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
 ... 6 more
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
 at org.apache.tomcat.websocket.server.WsServerContainer.<init>(WsServerContainer.java:147)
 at org.apache.tomcat.websocket.server.WsSci.init(WsSci.java:131)
 at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:47)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5456)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
 ... 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:

<properties>
    <tomcat.version>7.0.52</tomcat.version>
</properties>
<dependencies>
    ...

 

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 Superfish-for-Drupal-master.zip 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:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.stevewall123.ItemServiceTest.testFindITems(ItemServiceTest.java:35)

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:

Bug type NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE (click for details)
In class com.wall.steve.HelperMethods$1
In method com.wall.steve.HelperMethods$1.apply(MyItem)
Parameter item
At HelperMethods.java:[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>() {
    @Override
    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>() {
    @Override
    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