It was a little difficult to launch the H2 database with Dropwizard and use it, so I decided to post it. The flow is to create a Dropwizard project in Eclipse, create a class to start the H2 server, and let dropwizard's Environment lifecycle manage this class. Then, access the started H2 server with DB Viewer.
The software used is as follows.
First, let's create a project in Eclipse. Select File → New → Maven Project in Eclipse.
For Maven project, enter dropwizard in the filter and select "java-simple" in "io.dropwizard.archetypes". If this item does not appear here, add it from "Add Archetype".
To add it, enter it as shown in the figure.
It should appear in the list.
Name the project group ID and artifact ID whatever you like. The "name" in "Archetype Available Properties" will be used for the class name of the class that will be created automatically later. Press Finish to create the project.
Add the plugin to pom.xml. The plugins to add are: Plugin name: Group ID: Artifact ID.
Make sure it looks like the figure below.
Create a class to start the H2 server. I created it under resources.
When creating it, create it as follows.
Add org.junit.rules.ExternalResource to the superclass and io.dropwizard.lifecycle.Managed to the interface. ExternalResource is useful to add when testing with JUnit, and the Managed interface is inherited for addition to the Dropwizard Environment lifecycle.
The code for this class is as follows:
H2ServerResource.java
package hoge.fuga.resources;
import org.h2.tools.Server;
import org.junit.rules.ExternalResource;
import io.dropwizard.lifecycle.Managed;
public class H2ServerResource extends ExternalResource implements Managed {
private Server tcpServer = null;
private Server webConsole = null;
public H2ServerResource() {
//TODO auto-generated constructor stub
super();
}
@Override
public void start() throws Exception {
try {
this.before();
} catch (Throwable e) {
//TODO auto-generated catch block
e.printStackTrace();
throw new Exception("H2 server startup failure", e);
}
}
@Override
public void stop() throws Exception {
this.after();
}
@Override
protected void before() throws Throwable {
//Connection reception with tcp and Web console
if (tcpServer == null) {
try {
tcpServer = Server.createTcpServer("-tcpAllowOthers", "-tcpPort", "20020");
webConsole = Server.createWebServer("-webPort", "20010");
tcpServer.start();
webConsole.start();
} catch (Throwable e) {
//TODO auto-generated catch block
e.printStackTrace();
throw new Exception("H2 server startup failure", e);
}
}
}
@Override
protected void after() {
//Server shutdown
tcpServer.shutdown();
webConsole.shutdown();
}
}
If you inherit ExternalResource, you should override before and after. It feels good with JUnit. Inheriting the Managed interface requires overriding start and stop. Since they are the same process, they are called from either side. One thing to note here is that Dropwizard will call start and stop, so don't create an instance of Server in the constructor. It throws an error because it creates double instances. Also, just in case, null check is done to prevent double instance creation. The argument tcpAllowOthers of createTcpServer can be removed if it is accessed only from the local host.
Then, change the automatically created main class as follows.
piyoApplication.java
package hoge.fuga;
import hoge.fuga.resources.H2ServerResource;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
public class piyoApplication extends Application<piyoConfiguration> {
public static void main(final String[] args) throws Exception {
new piyoApplication().run(args);
}
@Override
public String getName() {
return "piyo";
}
@Override
public void initialize(final Bootstrap<piyoConfiguration> bootstrap) {
// TODO: application initialization
}
@Override
public void run(final piyoConfiguration configuration,
final Environment environment) {
// TODO: implement application
H2ServerResource h2serverResource = new H2ServerResource();
environment.lifecycle().manage(h2serverResource);
}
}
Register an instance of H2ServerResource at run. Looking at the original text, it means that you should use it for DB connection or thread pool.
Managed Objects
Most applications involve objects which need to be started and stopped: thread pools, database connections, etc. Dropwizard provides the Managed interface for this. You can either have the class in question implement the #start() and #stop() methods, or write a wrapper class which does so. Adding a Managed instance to your application’s Environment ties that object’s lifecycle to that of the application’s HTTP server. Before the server starts, the #start() method is called. After the server has stopped (and after its graceful shutdown period) the #stop() method is called.
That's all for the program. From here, we will start compiling.
Right-click on the project and select Run ® Run Configuration.
When the Run Configuration window opens, select Maven Build and create a new one.
Select the project folder in the base directory, enter package or clean package in the goal, apply and run. Then it will be built.
This will launch the H2 server. Dropwizard Tools is a useful plugin for running Dropwizard.
This plugin will automatically fill in the necessary arguments such as server and config.yml when running dropwizard. Once installed, open the debug configuration.
When the debug configuration window opens, select the main class.
And run. In this state, you can access the DB access (20020) and open the Web console (20010) by accessing the following ports entered earlier, respectively.
tcpServer = Server.createTcpServer("-tcpAllowOthers", "-tcpPort", "20020");
webConsole = Server.createWebServer("-webPort", "20010");
Let's access localhost: 20010. The web console opens.
Let's put localhost: 20020 in JDBC URL :.
If you access from the console with the following URL, h2db.mv.db will be created in the project folder.
jdbc:h2:tcp://localhost:20020/./h2db
Go back to Eclipse and open it from DB Viewer. Open the DBViewer window and press Register.
When the database definition window opens, select the h2 driver from Add File.
Look for it in the maven local repository folder.
In my environment it was in the following folder.
%USERPROFILE%\.m2\repository\com\h2database\h2\1.4.196
Then press Next.
In the connection information settings, the connection string was entered on the Web console earlier,
jdbc:h2:tcp://localhost:20020/./h2db
Enter the.
Set the connection user to "sa" and empty the connection schema.
Then make sure you can make a test connection.
With the above, it is from the place where you create a project with Dropwizard to the place where you can connect with DB Viewer.
Recommended Posts