Sven Ruppert

HelloWorld - with RedHat Thorntail Swarm - Ramp up in a second

This tutorial shows you how to set up a small Vaadin project running in a RedHat Thorntail Swarm Container, based on Undertow.

Target of this project

The target of this project is a minimal ramp-up time for a first hello world. Why we need one more HelloWorld? Well, the answer is quite easy. If you have to try something out, or you want to make a small POC to demonstrate something, there is no time and budget to create a demo project. You don’t want to copy paste all small things together. Here you will get a Hello World-Project that will give you all in a second.

Clone the repo and start editing the class VaadinApp. Nothing more.

Used Servlet container?

Here we are using the RedHat Thorntail Swarm Container as Servlet-Container. https://thorntail.io/

The UI

The UI doesn’t contain many elements. There is only a button you can click. The counter is increased on every click. The component will be registered under the root-path, based on the Annotation @Route("") For more information’s about the routing itself have a look at https://vaadin.com/docs/flow/routing/tutorial-routing-annotation.html

@Route("")
public class VaadinApp extends Composite<Div> {

  private final Button         btnClickMe   = new Button("click me");
  private final Span           lbClickCount = new Span("0");
  private final VerticalLayout layout       = new VerticalLayout(btnClickMe, lbClickCount);

  private int clickcount = 0;

  public VaadinApp() {
    btnClickMe.addClickListener(event -> lbClickCount.setText(valueOf(++clickcount)));
    //set the main Component
    System.out.println("setting now the main ui content..");
    getContent().add(layout);

  }
}

Maven dependencies

To use this together with the RedHat Thorntail Swarm container, you should first add the Thorntail BOM (versions) followed by the Vaadin Platform BOM.

  <dependencyManagement>
    <dependencies>
      <!--Thorntail-->
      <dependency>
        <groupId>io.thorntail</groupId>
        <artifactId>bom</artifactId>
        <version>${thorntail.version}</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
      <!--Vaadin -->
      <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-bom</artifactId>
        <version>${vaadin.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

To use the Thorntail Swarm at runtime, we need to add the dependency that will provide the Undertow as Servlet container. Optional you could add the logging as well.

    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>logging</artifactId>
    </dependency>
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>undertow</artifactId>
    </dependency>

The last needed dependency is the thorntail maven plugin. This will make sure that all the needed preparations are done during the build and startup process.

      <plugin>
        <groupId>io.thorntail</groupId>
        <artifactId>thorntail-maven-plugin</artifactId>
        <version>${thorntail.version}</version>
        <executions>
          <execution>
            <id>package</id>
          </execution>
          <execution>
            <id>start</id>
          </execution>
          <execution>
            <id>stop</id>
          </execution>
        </executions>
      </plugin>

How to start it

The Thorntail plugin allows you to run the demo. Run the following maven command inside the folder that was created from git during the cloning process.

mvn clean install thorntail:run

The command builds the demo from scratch, fetches dependencies from Maven central, and starts Thorntail Swarm. You can access the application on http://localhost:8080/

Happy Coding.

Vaadin is an open-source framework offering the fastest way to build web apps on Java backends
GET STARTED

Comments (3)

Sven Ruppert
3 years ago Jun 19, 2019 6:09am
Sven Ruppert
3 years ago Jun 19, 2019 8:51am