Making Frontale a microservice

This article is the 17th day of FUJITSU Advent Calendar 2017.

In this article, we will introduce how to create a program that provides frontale player information using the REST API using Launcher, which is Fujitsu's MicroProfile implementation.

Before that, what is MicroProfile?

MicroProfile is a community that studies specifications for realizing enterprise microservices in Java. (http://microprofile.io) In this article, we will use the following four APIs defined in MicroProfile 1.1.

Things to prepare in advance when creating a program

Since all the above four APIs are included in Launcher, no other library or development environment for Java EE is required.

Process flow

  1. Use the Config API to get the URL of the Frontale page and, if necessary, PROXY information.
  2. Get the HTML from Frontale Page. The acquired HTML is analyzed and a player information list is created. (This has nothing to do with MicroProfile)
  3. Create a REST endpoint with JAX-RS / CDI.
  4. When the request comes in, we will return it in JSON.

Creating the source

Only the Config API of Frontale.java is explained.

Frontale.java


     71     Config config = ConfigProvider.getConfig();

Get the Config object from the ConfigProvider. The actual property value is obtained through this Config object.

Frontale.java


     77       url = config.getValue("frontale.url", String.class);

Pass the property name in the first argument of the getValue method and the type in the second argument. If the property value is not found, a NoSuchElementException is thrown.

There are various ways to specify the property, but this program uses the default property file and describes it as follows.

microprofile-config.properties


  frontale.url = http://www.frontale.co.jp/profile/2017/number.html

compile

% javac -classpath launcher-1.0.jar -d classes *.java

Creating a jar

The class file and property file are put together in a jar as shown below.

% jar tf frontale.jar
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/classes/
WEB-INF/classes/META-INF/
WEB-INF/classes/kzr/
WEB-INF/classes/kzr/frontale/
WEB-INF/classes/META-INF/microprofile-config.properties
WEB-INF/classes/kzr/frontale/Frontale$1.class
WEB-INF/classes/kzr/frontale/Frontale$Parser.class
WEB-INF/classes/kzr/frontale/Frontale$Parser2.class
WEB-INF/classes/kzr/frontale/Frontale.class
WEB-INF/classes/kzr/frontale/FrontaleApplication.class
WEB-INF/classes/kzr/frontale/FrontaleService.class
WEB-INF/classes/kzr/frontale/Player.class

Launch the app

Just run the following command. No prior installation is required except for the JDK.

% java -Djava.io.tmpdir=. -jar launcher-1.0.jar --deploy frontale.jar​

Execution result

--List of player numbers (http : // localhost: 8080 / list)

--Kenta Kano's information (http : // localhost: 8080/25)

in conclusion

"Microservices" was an overstatement, but as introduced here, MicroProfile makes it easy to create applications that run in lightweight containers.

Recommended Posts

Making Frontale a microservice
Making a minute repeater part.2
Making a minute repeater part.1
Make a language! (Making a simple calculator ②)
Make a language! (Making a simple calculator ①)
Try making a calculator app in Java