[JAVA] Try moving WildFly wild-from installation to deployment-

Introduction

I will write an article for the first time on Qiita.

I'm currently using WildFly, but I feel that it's a little less well-known, so I wrote an article with the hope of helping it spread. If you have only used Tomcat, please try it. The goal was for beginners to be able to move (maybe) for the time being, so I will proceed without worrying about the details.

The confirmed operating environment is as follows.

What is WildFly

Roughly speaking

"WildFly"? What do you mean?

The great thing about WildFly

What to do this time

  1. Install WildFly and try it out.
  2. Make settings to connect to the database.
  3. Try to display the data obtained from the database with a simple web application.

Advance preparation

If you know this, please skip it. If you just want to run WildFly, skip the database (MySQL) related page as well. If you want to see it, click ▼ to expand it.

Use command prompt
For Windows 7, it is below. `Start button` → `All programs` → `Accessories` →` Command prompt ` For Windows 10, it is below. `Start button` → `Windows system tools` → `Command prompt`

When you start the command prompt, a black screen appears and you can enter commands. You can open as many as you like.

command prompt



Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\tamaru>

** Command prompt small story **

** Frequently used commands this time **

--List the files in the dir directory (folder). --Move the cd directory.

** If you find it troublesome to move folders with the cd command **

First type cd at the command prompt.

Drag the folder you want to move in Explorer. Drop it at the command prompt. When you release your finger from the mouse, the folder path will be entered automatically. If you accidentally move to a strange place, don't panic, keep your finger on the mouse and press the "Esc" button to cancel.

** If you don't like using the command prompt **

Type in the following to open Explorer.

command prompt


>explorer .
Check if Java 8 or above can be used
First, Java 8 or above must be installed. Open a command prompt and execute the following command to check.

command prompt


>java -version

If you are told as below, Java is not installed, so from Oracle's site, java SE (JDK) ) And install it.

command prompt


'java'Is an internal or external command,
It is not recognized as an operable program or batch file.

If you type java -version and the version information is displayed, it's OK.

command prompt


java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
Get WildFly
Download the latest `(Java EE Full & Web Distribution)` from WildFly's [Official Site](http://wildfly.org/downloads/ "Official Site"). This time I downloaded `wildfly-14.0.1.Final.zip`.
Prepare a connectable database (MySQL)

Use MySQL 8 as the test database. Use the zip version of mysql-8.0.13-winx64.zip instead of the installer version to make it as easy as possible. If you want to install it and use it for a long time, please refer to the articles and books of MySQL to build the environment.

Click the "Download" button of Windows (x86, 64-bit), ZIP Archive from the following. https://dev.mysql.com/downloads/mysql/

Next, the "Login" and "Sign Up" buttons will appear, but you can also download them from the "No thanks, just start my download." Link below.

Unzip the downloaded mysql-8.0.13-winx64.zip to a suitable folder. Here, unzip to C: \ mysql-8.0.13-winx64. Open a command prompt and type:

command prompt


>cd C:\mysql-8.0.13-winx64\bin

Then initialize the database.

command prompt


>mysqld --initialize-insecure

When finished, start mysqld.exe.

command prompt


>mysqld

It doesn't return as it is, but MySQL is running, so leave it as it is. mysqld is a MySQL service and must be running. Do not close until finished </ font>

: information_source: When I tried it on another Windows 10 PC, I got the following error. It seems that the cause is that the Visual C ++ Redistributable Package for Visual Studio 2017. is not included. Download and install x64: vc_redist.x64.exe from the Microsoft page below. It seems that you need to restart Windows after installation. https://support.microsoft.com/ja-jp/help/2977003/the-latest-supported-visual-c-downloads

With mysqld running, open another command prompt and log in to the MySQL service. You will be asked for a password, but since the root (administrative user) password has not been set, press Enter </ kbd>.

command prompt


>cd C:\mysql-8.0.13-winx64\bin
>mysql -uroot -p
Enter password:

You have successfully logged in to MySQL.

command prompt


Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.13

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Create a test database testdb. Enter the following and press Enter </ kbd>. CREATE DATABASE testdb DEFAULT CHARACTER SET UTF8;

command prompt


mysql> CREATE DATABASE testdb DEFAULT CHARACTER SET UTF8;
Query OK, 1 row affected, 1 warning (0.01 sec)

Create a test user "test". Enter the following and press Enter </ kbd>. CREATE USER 'test'@'localhost' IDENTIFIED BY 'test';

Next, set the authority. Enter the following and press Enter </ kbd>. GRANT ALL PRIVILEGES ON testdb.* TO 'test'@'localhost' WITH GRANT OPTION;

Now that the user has been created, quit; and log out.

command prompt


mysql> CREATE USER 'test'@'localhost' IDENTIFIED BY 'test';
Query OK, 0 rows affected (0.12 sec)

mysql> GRANT ALL PRIVILEGES ON testdb.* TO 'test'@'localhost' WITH GRANT OPTION;
Query OK, 0 rows affected (0.04 sec)

mysql>quit;

Log in to MySQL again. This time, log in with the user name "test" and password "test". Type mysql -utest -ptest test db and press Enter </ kbd>.

command prompt


c:\mysql-8.0.13-winx64\bin>mysql -utest -ptest testdb
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.13 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Create a strike table "TEST_TABLE" with the following SQL.

CREATE TABLE TEST_TABLE ( ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(30) NOT NULL );

command prompt


mysql> CREATE TABLE TEST_TABLE (ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(30) NOT NULL);
Query OK, 0 rows affected (0.09 sec)

Add data using: ʻINSERT INTO TEST_TABLE SET NAME = "aiueo"; ʻINSERT INTO TEST_TABLE SET NAME = "Kakikukeko";

command prompt


mysql> INSERT INTO TEST_TABLE SET NAME="AIUEO";
Query OK, 1 row affected (0.36 sec)
mysql> INSERT INTO TEST_TABLE SET NAME="Kakikukeko";
Query OK, 1 row affected (0.00 sec)

that's all.

To stop musqld, open a command prompt and type mysqladmin -u root -p shutdown. You will be prompted for the root password. Enter the password and press the Enter </ kbd> key.

command prompt


>cd C:\mysql-8.0.13-winx64\bin
>mysqladmin.bat -u root -p shutdown
Enter password: ****

that's all. It may not work depending on the installation environment. This time I used the latest version (as of December 2018) of MySQL 8.0.13, but I think you can check it with older versions. It's a WildFly article, so I thought I'd finish it briefly, but it's been rather long.

Prepare a JDBC driver for the target database
Since MySQL is used in this article, download the JDBC driver [Connector / J](https://dev.mysql.com/downloads/connector/j/ "Connector / j") for MySQL. Please. If you are using another database, please prepare a compatible JDBC driver.

Select Platform Independent fromSelect Operating System:and download Platform Independent (Architecture Independent), ZIP Archive.

If you unzip the downloaded mysql-connector-java-8.0.13.zip, you will get a file called mysql-connector-java-8.0.13.jar. This is the JDBC driver itself for MySQL.

A text editor that supports Linux line feed code (LF) is available
If you open the WildFly configuration file etc. with Notepad on Windows, line breaks will not occur due to the difference in line feed code, making it extremely difficult to see. Prepare a text editor that supports line feed codes such as Linux. I use [Sakura Editor]("https://sakura-editor.github.io/" Sakura Editor).
Prepare a web application (war file) to deploy to WildFly
Prepare a web application war (Web application ARchive) for deploying to WildFly. If you already have one, please use it.

Prepare an application to check the operation. The database will be accessed directly from jsp, but please note that the reader will get angry if you make it like this. This time I will do it without worrying about it.

First, create a folder called helloworld. Create a file named index.jsp in it with the following contents.

index.jsp


<%@page contentType="text/html; charset=UTF-8" %>
<%@page import="javax.naming.*" %>
<%@page import="javax.sql.*" %>
<%@page import="java.sql.*" %>
<html>
<body>
Hello World!<br>
<%
Context context = new InitialContext();
DataSource dataSource = (DataSource)context.lookup("java:/MySqlDS");
try (Connection connection = dataSource.getConnection();
    PreparedStatement preparedStatement = connection.prepareStatement("select * from test_table;");
    ResultSet resultSet = preparedStatement.executeQuery()) {
    while(resultSet.next()) {
        out.print(resultSet.getString("NAME"));
        out.print("<br>");
    }
}
%>
</body>
</html>

The java: / MySqlDS below should be the same as the name set in the data source described later.

DataSource dataSource = (DataSource)context.lookup("java:/MySqlDS");

Next, create an archived war file. It is an image to be zipped. First, open a command prompt and go to the helloworld folder you created.

command prompt


>cd C:\helloworld

Create a war file using the jar command that comes with java (JDK).

>jar cvf helloworld.war .\*
Manifest added
index.jsp is being added(Enter=1158)(Out=528)(54%Shrinked)

I think you have created helloworld.war. I don't have WEB-INF or web.xml, but I don't care this time.

Install WildFly

Unzip wildfly-14.0.1.Final.zip and move to the unzipped folder at the command prompt.

command prompt


>cd C:\wildfly-14.0.1.Final\bin

Since standalone.bat is the start command, execute it.

command prompt



>standalone.bat
Setting JAVA property to "C:\jdk\jdk1.8.0\bin\java"
===============================================================================

  JBoss Bootstrap Environment

  JBOSS_HOME: "C:\wildfly-14.0.1.Final"

  JAVA: "C:\jdk\jdk1.8.0\bin\java"

  JAVA_OPTS: "-Dprogram.name=standalone.bat -Xms64M -Xmx512M -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman "

===============================================================================

12:24:08,535 INFO  [org.jboss.modules](main) JBoss Modules version 1.8.6.Final

The character string is displayed all at once, but when the characters WildFly Full 14.0.1.Final (WildFly Core 6.0.2.Final) started in xxxxms appear, the startup is complete.

command prompt



12:24:24,525 INFO  [org.jboss.ws.common.management](MSC service thread 1-2) JBWS022052: Starting JBossWS 5.2.3.Final (Apache CXF 3.2.5.jbossorg-1)
12:24:24,822 INFO  [org.jboss.as.server](Controller Boot Thread) WFLYSRV0212: Resuming server
12:24:24,822 INFO  [org.jboss.as](Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
12:24:24,822 INFO  [org.jboss.as](Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
12:24:24,822 INFO  [org.jboss.as](Controller Boot Thread) WFLYSRV0025: WildFly Full 14.0.1.Final (WildFly Core 6.0.2.Final) started in 21716ms - Started 306 of 527 services (321 services are lazy, passive or on-demand)

WildFly runs on port 8080. If you access the following, the top screen will be displayed. http://localhost:8080/

This time, "** 1. Install WildFly and try it. **" is now complete.

Add admin user

You can go to the administration screen from the ʻAdministration Console link on the top page of WildFly. In order to use the management screen, it is necessary to register the management user. Add an admin user using the ʻadd-user.bat command under wildfly-14.0.1.Final \ bin.

Open a command prompt and execute "add-user.bat" as shown below.

command prompt


>cd C:\wildfly-14.0.1.Final\bin
>add-user.bat

What type of user do you wish to add?
 a) Management User (mgmt-users.properties)
 b) Application User (application-users.properties)
(a):

You will be asked whether you want to add a user of the type Management User or ʻApplication User. Select Management User of ʻa). It is selected by default, so you can just use Enter </ kbd>.

command prompt


Enter the details of the new user to add.
Using realm 'ManagementRealm' as discovered from the existing property files.
Username :

You will be asked for the user name to add, so enter any name you like. For the time being, let's call it "jboss1".

command prompt


Password recommendations are listed below. To modify these restrictions edit the add-user.properties configuration file.

 - The password should be different from the username
 - The password should not be one of the following restricted values {root, admin, administrator}
 - The password should contain at least 8 characters, 1 alphabetic character(s), 1 digit(s), 1 non-alphanumeric symbol(s)
Password :

Decide on a password. The same password as the user name, root, admin, administrator will ask you to include no, numbers, non-alphabetic characters, so follow it. Here, it is "12345 test!". ** If you are operating in a production environment, please use a more difficult password. ** **

command prompt


WFLYDM0048: Password is not strong enough, it is 'MODERATE'. It should be at least 'MEDIUM'.
Are you sure you want to use the password entered yes/no? yes
Re-enter Password :

Is such an ordinary password all right? You will be asked to enter "yes" and enter the password again.

command prompt


What groups do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[  ]:
About to add user 'jboss1' for realm 'ManagementRealm'
Is this correct yes/no? yes
Added user 'jboss1' to file 'C:\wildfly-14.0.1.Final\standalone\configuration\mgmt-users.properties'
Added user 'jboss1' to file 'C:\wildfly-14.0.1.Final\domain\configuration\mgmt-users.properties'
Added user 'jboss1' with groups  to file 'C:\wildfly-14.0.1.Final\standalone\configuration\mgmt-groups.properties'
Added user 'jboss1' with groups  to file 'C:\wildfly-14.0.1.Final\domain\configuration\mgmt-groups.properties'
Is this new user going to be used for one AS process to connect to another AS process?
e.g. for a slave host controller connecting to the master or for a Remoting connection for server to server EJB calls.
yes/no?

You will be asked for a group, but just press Enter </ kbd>. You will be asked "Is this correct yes / no?" For the final confirmation, but enter "yes". Do you want to continue using it to connect to other ASs (WildFly)? Is asked, but enter "no" to complete the addition.

Access the administration screen from the "Administration Console" at the top again. When the authentication dialog appears, enter the user name "jboss1" and password "12345test!" And press "OK".

The top page of the management screen is displayed. You can now use the management screen.

JDBC driver deployment

Install (deploy) the JDBC driver for MySQL to use the database from Java. There are the following three installation methods. This time we will do it in the simplest way.

** 1. Place it in the deployments folder ** Just put the file. This time we will proceed this way. ** 2. Deploy from the management screen ** This is a method for those who want to use the management screen. ** 3. Register as a module ** It can be used from any web application running on WildFly. There is no duplication of jars and management is easier.

: information_source: Decide which method you want to use, method 1 or method 2. If you deploy using method 2, the jar will not be placed in the deployments folder. After that, if you put the same jar in the deployments folder by the method of 1. without noticing it, it will be in a state like double deployment, and will it not disappear even if you delete it? You may fall into a state of strangeness. ~~ If you are interested, let's try it ~~

** Work procedure **

wildfly-14.0.1.Final/standalone/deploymentsフォルダに用意したmysql-connector-java-8.0.13.jarを放り込んでください。しばらくしてwildfly-14.0.1.Final/standalone/deploymentsmysql-connector-java-8.0.13.jar.deployedというファイルができたら完了です。

Data source settings

Javaからデータベースを利用するためにデータソースの設定を行う必要があります。設定方法は以下の3つの方法があります。いずれの方法も最終的には設定ファイルildfly-14.0.1.Final\standalone\configuration\standalone.xmlが更新されます。

** 1. Set using the administration screen "Administration Console" ** It is easy to understand because you can follow the instructions on the management screen. This time we will proceed this way. ** 2. Edit the configuration file (standalone.xml) ** Edit the configuration file (standalone.xml) with a text editor. WildFly may not start if you format it incorrectly. ** 3. Set using the management CLI (jboss-cli) command ** Use the management CLI (jboss-cli) command. The format is correct, but the command is complicated.

** Work procedure **

First, let's open the current configuration file wildfly-14.0.1.Final \ standalone \ configuration \ standalone.xml with a text editor and check it. If you search the document with "datasource", you will find the following settings. This is a data source setting called "Example DS" that is set from the beginning. WildFly itself has a database called "H2Database" and uses it internally. Add a new data source here.

xml:wildfly-14.0.1.Final\standalone\configuration\standalone.xml(Excerpt)


<subsystem xmlns="urn:jboss:domain:datasources:5.0">
    <datasources>
        <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
            <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
            <driver>h2</driver>
            <security>
                <user-name>sa</user-name>
                <password>sa</password>
            </security>
        </datasource>
        <drivers>
            <driver name="h2" module="com.h2database.h2">
                <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
            </driver>
        </drivers>
    </datasources>
</subsystem>

Go to the admin screen and click Deployments to verify that mysql-connector-java-8.0.13.jar is deployed.

Then select Configuration SubsystemDatasource & Drivers Datasources+ menu and select ʻAdd Datasource`.

A white pop-up window will appear, so set it. Check MySQL from the list and press the Next button to proceed. Enter Name and JNDI Name. From the beginning, MySqlDS is entered as java: / MySqlDS, but there is no problem with that name, so press Next to proceed. JNDI Name is usually a name that starts with" java: / "or" java: jboss / ". * When using the management screen, it seems to warn you properly even if you make a mistake. Select mysql-connector-java-8.0.13.jar from the Driver Name pull-down menu. Driver Module Name`` Driver Class Name is included from the beginning, so press Next to proceed. Enter jdbc: mysql: // localhost: 3306 / testdb? ServerTimezone = JST in the Connection URL. "/ Testdb" is the database name to use. "? ServerTimezone = JST" is a parameter of MySQL, but if you do not add it, an error will occur, so add it. Enter the ʻUser Name Password and press the Next` button to proceed.

Test if you can connect. Press the Test Connection button. If Test Connection Successful is displayed, it is successful. Press Next to proceed to the final confirmation screen. Press Finish to complete. Close the window with the Close button

Let's open wildfly-14.0.1.Final \ standalone \ configuration \ standalone.xml again with a text editor and check it<datasource jndi-name = "java: / MySqlDS" pool-name = "MySqlDS" I think the>tag has been added.

xml:wildfly-14.0.1.Final\standalone\configuration\standalone.xml(Excerpt)


<subsystem xmlns="urn:jboss:domain:datasources:5.0">
    <datasources>
        <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
            <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
            <driver>h2</driver>
            <security>
                <user-name>sa</user-name>
                <password>sa</password>
            </security>
        </datasource>
        <datasource jndi-name="java:/MySqlDS" pool-name="MySqlDS">
            <connection-url>jdbc:mysql://localhost:3306/testdb?serverTimezone=JST</connection-url>
            <driver-class>com.mysql.jdbc.Driver</driver-class>
            <driver>mysql-connector-java-8.0.13.jar</driver>
            <security>
                <user-name>test</user-name>
                <password>test</password>
            </security>
            <validation>
                <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
                <background-validation>true</background-validation>
                <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
            </validation>
        </datasource>
        <drivers>
            <driver name="h2" module="com.h2database.h2">
                <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
            </driver>
        </drivers>
    </datasources>
</subsystem>

This is the end of what to do this time, "** 2. Set up to connect to the database. **".

Deploy web application

Deploy the web application. There are the following three deployment methods. This time I will do it in the simplest way.

** 1. Place it in the deployments folder and deploy ** Just put the file. This time we will proceed this way. ** 2. Deploy from the management screen ** This is a method for those who want to use the management screen. ** 3. Deploy with management CLI ** This is a method for those who want to deploy with a command.

: information_source: Use method 1 or method 2 first, as with the JDBC driver.

** Work procedure ** wildfly-14.0.1.Final/standalone/deploymentsフォルダに用意したhelloworld.warを置いてください。しばらくしてwildfly-14.0.1.Final/standalone/deploymentshelloworld.war.deployedというファイルができたらデプロイ完了です。

If you access the deployed application and the top page is displayed, it is successful. If you used the procedure in this article, please access the following URL

http://localhost:8080/helloworld/

You should see something like the following.

Hello World! AIUEO Kakikukeko

This time, "** 3. Display the data acquired from the database with a simple Web application. **" is now complete.

That's all.

If you don't need it anymore, stop WildFly and MySQL, close the command prompt, put C: \ wildfly-14.0.1.Final and C: \ mysql-8.0.13-winx64 in the trash, and there's no trace. It disappears.

in conclusion

How to stop WildFly. Stop the command prompt running standalone.bat with Ctrl </ kbd> + C </ kbd>. When using the management CLI, stop it as follows.

Newly opened command prompt


>jboss-cli -c --command=":shutdown"

Press Enter </ kbd> and wait for a while, then WildFly will end at the command prompt where standalone.bat is running.

Command prompt with WildFly running


17:32:08,388 INFO  [org.wildfly.extension.undertow](MSC service thread 1-6) WFLYUT0004: Undertow 2.0.13.Final stopping
17:32:08,404 INFO  [org.jboss.as](MSC service thread 1-8) WFLYSRV0050: WildFly Full 14.0.1.Final (WildFly Core 6.0.2.Fi
nal) stopped in 234ms
Press any key to continue. . .
>

Reference URL

WildFly Documentation version 15.0.0.Final Getting Started with WildFly 15 JBoss EAP 7.1 Getting Started Guide JBoss Enterprise Application Platform 6 Construction and Operation Perfect Guide OpenStandia WildFly latest information

Recommended Posts

Try moving WildFly wild-from installation to deployment-
Wildfly installation
Rails Tutorial Chapter 1 From Zero to Deployment [Try]
Try WildFly with Docker
Try switching from the RHEL environment to the Ubuntu environment Server installation