[JAVA] Migrating from Eclipse server function (Tomcat) to Embed Tomcat

There is a web application that was created in the Java 1.4 era a long time ago and still uses active Tomcat. This is a story when Embed Tomcat was introduced to such a Web application development environment by chance. Detailed setting information about the introduction is scattered all over the place, so here is a quick read of the process. .. ..

Prerequisites

What happened in the first place

The Java project of Eclipse was not normally recognized as a dynamic server project when building the environment.

All applications and server settings are stored in Git in Eclipse project format. Basically, the environment construction should have been almost completed by importing the project including the introduction of the server function Tomcat. (The members so far were okay with that) Then, what is the difference from the development members so far? .. ..

The development machine is ** Mac **!

What is that? At most, the project structure was such that it wouldn't work when the environment changed to Mac! !!

Consider shifting to an environment-independent method

For the time being, I gave up the cause investigation early for the following reasons

Application-wide migration. .. .. I want to

As mentioned at the beginning, because it is a web application from the old days and the framework is also unique, Better yet, while having a delusion that it would be okay if I moved to Spring (Boot) However, I came to consider a method like a matter that refers to the framework of the current generation.

Change to Embed Tomcat

Speaking of the taste of Java, if it is made into bytecode, it does not depend on the environment! Of course Tomcat is no exception, so try not to depend on the OS or IDE as much as possible. I vowed to move to Embed Tomcat.

Work done during the transition

Installation of required jar

All the necessary jars are manually inserted into the build path, because it is a project format with no dependencies or crap. Download the jar below and add it to your build path.

Tomcat launcher Main method creation

LaunchEmbedTomcat.java


	public static void main(String[] args) throws Exception {

		Tomcat tomcat = new Tomcat();

		StandardContext ctx = (StandardContext) tomcat.addWebapp("/hogecontext",
				Paths.get("WebContent/").toAbsolutePath().toString());

		WebResourceRoot resources = new StandardRoot(ctx);
		DirResourceSet dirSet = new DirResourceSet();
		dirSet.setBase(Paths.get("build/classes").toAbsolutePath().toString());
		dirSet.setWebAppMount("/WEB-INF/classes");

		resources.addPreResources(dirSet);

		ctx.setResources(resources);

		tomcat.getService().addConnector(createAJPConnector());

		ctx.getNamingResources().addResource(createDBContext());

		tomcat.start();
		tomcat.getServer().await();
	}


	static Connector createAJPConnector() {
		Connector ajpConnector = new Connector("AJP/1.3");
		ajpConnector.setPort(8009);
		return ajpConnector;
	}

	static ContextResource createDBContext() {
		ContextResource jdbcResource = new ContextResource();
		jdbcResource.setName("jdbc/hogedb");
		jdbcResource.setType(DataSource.class.getName());
		jdbcResource.setAuth("Container");
		jdbcResource.setProperty("factory", BasicDataSourceFactory.class.getName());
		jdbcResource.setProperty("driverClassName", org.postgresql.Driver.class.getName());
		jdbcResource.setProperty("url", "jdbc:postgresql://192.168.33.10:5432/hogedb");
		jdbcResource.setProperty("username", "user");
		jdbcResource.setProperty("password", "password");
		return jdbcResource;
	}

I will omit the detailed process explanation, but "WebResourceRoot # addPreResources" is required to make the compiled classes directory appear to be under WEB-INF of Embed Tomcat.

Start tomcat

Execution-> When started with a Java application, it was confirmed that the "load-on-startup" defined in web.xml was executed in sequence. This is an error for a while I thought it was cool!

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

Upon examination, it seems that the JNDI initial position "java: comp / env" itself is not defined. In the API reference, it was written as follows, so it seemed that Embed had to enable it manually. (Or, is there a setting for activation somewhere in the normal server function? I haven't investigated that much, I'm sorry)

Enables JNDI naming which is disabled by default.

Therefore, I added the following line to the startup method.

LaunchEmbedTomcat.java


tomcat.enableNaming();

Start tomcat again

The following display appears on the console.

12 11, 2017 12:47:48 AM org.apache.coyote.AbstractProtocol start Info: Starting ProtocolHandler ["ajp-nio-8009"]

Apparently, it started normally. I was able to confirm the normal operation of the application by communicating via AJP.

Impressions of trying to operate

It is much faster than Tomcat started from the Eclipse server function. I haven't investigated what the overhead is, but the feeling is so light that I can feel it. This was lucky.

Whether it actually works on Mac

Actually, I haven't actually tried it on a Mac yet (because this post came earlier in terms of date and time ...) I think it's okay due to the characteristics of Java. I will report the result at a later date! !! it must be no problem! → It worked fine without any problems! !! !!

Remaining work

I have created a gradle task for the build, but I would like to add a task to start Tomcat as well.

Finally

I am glad that I was able to eliminate even one setting that depends on the environment. However, I just can't get rid of my feelings now. I want to move to a modern framework as soon as possible! !!

Postscript

Tomorrow is @ yam0918.

Recommended Posts

Migrating from Eclipse server function (Tomcat) to Embed Tomcat
How to start tomcat local server without using eclipse
Migrating from vargrant to docker
[Opens aml] NoClassDefFoundError occurs when migrating from Tomcat to weblogic
Stop resending from client to server
Notes on migrating from CircleCI 1.0 to 2.0
Switch from Eclipse to VS Code
Shortcut comparison for those migrating from Eclipse to IntelliJ IDEA (Windows)
Connect from Java to MySQL using Eclipse
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
From installing Eclipse to executing Java (PHP)
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
Precautions when migrating from VB6.0 to JAVA
Migration from Eclipse to IntelliJ (on the way)
When the server fails to start in Eclipse
Migrate from Java to Server Side Kotlin + Spring-boot
Minecraft BE server development from PHP to Java
The right way to see the tomcat source in eclipse
How to switch Tomcat context.xml with WTP in Eclipse
How to jump from Eclipse Java to a SQL file
The story of migrating from Paperclip to Active Storage
To connect from Spring to MySQL on virtual server (unsolved)
[Eclipse] Summary of environment settings * Updated from time to time
[Android] Uploading images from your device to the server