I am creating a RESTApi using spring framework. My IDE is STS. Embedded Tomcat concept is pretty useful and easy to implement. Just export a jar, run it and boom your service is up and running, but embedded Tomcat is using Nio HTTP Connector. I did some research and find out that APR HTTP Connector is much more better than Nio and I want to use APR. Still there is an option : export a war file, deploy it into a tomcat and configure its HTTP connector. But I really like embedded tomcat and I am wondering is it possible to change its HTTP Connector from Nio to APR ?
We can enable APR in springboot embeded tomcat by overiding the org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory and providing new org.apache.catalina.connector.Connector
with org.apache.coyote.http11.Http11AprProtocol protocol.
The below code might help to get it done.
#Bean
public TomcatServletWebServerFactory servletContainerFactoryProd() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
#Override
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
// to create new directories and files and add them to Context
return super.getTomcatWebServer(tomcat);
}
};
Connector connector = new Connector("org.apache.coyote.http11.Http11AprProtocol");
Http11AprProtocol protocol = (Http11AprProtocol) connector.getProtocolHandler();
connector.setProperty("compression", "on");
connector.setProperty("compressableMimeType", "text/html,text/xml,text/plain,application/json,application/xml");
// can also enable ssl and provide certificate details
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
Related
Hi Java and Springboot Gurus,
This regarding a project I have created in Java using springboot.
I was able to successfully create and setup an https project in spring boot.
Then to set the HTTPS port I have just to set via commandline instead in application.properties via this command in the terminal:
$ java -Dserver.port=6001 -jar myapp-0.0.1.jar
I made it like this so that I can easily create other instances that will be running in other ports let say 6002, 6003 and so on...
But the problem is that, there is this default HTTP Port that tomcat sets:
2019-08-16 02:42:36.768 INFO 6 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 6001 (https) 8080 (http)
I want to get rid of this or perhaps assign it to another port e.g. 6081.
I have read this article, and its setting it up programmatically.
But I want to avoid that.
I just want it to be something I can add as parameter in the command line like -Dserver.port=6001. To avoid further complicating my project as much as I can.
I tried doing this:
$ java -Dserver.port=6001 -Dserver.http.port=6081 -jar myapp-0.0.1.jar
... but it doesn't work.
Your ideas and inputs will be much appreciated!
Have you tried setting server.port=-1 in your application.properties. See Spring Boot doc.
[Update]
You may redirect your HTTP to HTTPS.
To achieve this you need another connector. SpringBoot does not allow to configure multiple connector with application.properties. So we can write a connector programmatically. Connector example for Tomcat:
#Configuration
public class MyTomcatConnector {
#Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
#Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
return tomcat;
}
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(6001);
return connector;
}
}
In the example listed on the undertow documentation site, it shows how to configure 2 servlets and their mappings. But I can't find how to configure the default Servlet to allow directory listing and resource serving.
DeploymentInfo servletBuilder = Servlets.deployment()
.setClassLoader(ServletServer.class.getClassLoader())
.setContextPath("/myapp")
.setDeploymentName("test.war")
.addServlets(
Servlets.servlet("MessageServlet", MessageServlet.class)
.addInitParam("message", "Hello World")
.addMapping("/*"),
Servlets.servlet("MyServlet", MessageServlet.class)
.addInitParam("message", "MyServlet")
.addMapping("/myservlet"));
DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
manager.deploy();
PathHandler path = Handlers.path(Handlers.redirect("/myapp"))
.addPrefixPath("/myapp", manager.start());
Undertow server = Undertow.builder()
.addHttpListener(8080, "localhost")
.setHandler(path)
.build();
server.start();
It's an embedded undertow server in a runnable jar.
When adding servlets, also add the "default" servlet in the list of servlets with
Servlets.servlet("default", DefaultServlet.class)
.addInitParam(DefaultServlet.DIRECTORY_LISTING, "true")
.addInitParam(DefaultServlet.DEFAULT_ALLOWED, "true")
.addInitParam(DefaultServlet.ALLOW_POST, "false")
.addInitParam(DefaultServlet.RESOLVE_AGAINST_CONTEXT_ROOT, "true")
Then before deploying the container add this to the DeploymentInfo
servletBuilder.setResourceManager(new ClassPathResourceManager(App.class.getClassLoader(), "webapp"));
Then anything you put inside the webapp folder that is packaged in the jar will work exactly the same as any normal war/webapp folder deployed in any app server.
You can use any name or folder, webapp was just an example.
I have an issue setting up an rmi port on Apache Tomcat server
(set JAVA_OPTS=%JAVA_OPTS% -DlocalRmiRegistryPort=9401)
Here are the steps i have done and getting the error respectively.
Issue with MoSKito tool while configuring RMI port on Tomcat
I am also tried to configure MoSkito inspect on my java web application. as part of it, i have been trying to make RMI port using "set JAVA_OPTS=%JAVA_OPTS% -DlocalRmiRegistryPort=9401" on catalina.bat. there is no error as such but 9401 is not getting connected.
Steps followed:
1) My OS is Windows 7
2) I have installed Apache Tomcat v7
3) I have downloaded Moskito inspect .war file from http://www.moskito.org/download.html and
4) Installed Moskito inspect on my tomcat server, the MoSKito inspect web UI is loading good.
5) I have created a sample java web application and deployed on same tomcat server. and web application also working good.
6) Now i have taken step to configure RMI port 9041 using below command on \bin\catalina.bat file(since i am on Windows OS).
I have put the below command on top of the file in \bin\catalina.bat
set JAVA_OPTS=%JAVA_OPTS% -DlocalRmiRegistryPort=9401
7) and started tomcat server, and then opened Moskito application on browser and gone to Quick Connect option on and given localhost and rmi port 9401 to call remote invocation.
Its failed.... its always giving error as MoSKito encountered an error:
Cannot connect to localhost#localhost:9401, due: Can not resolve manually set reference. Server at localhost, port: 9401 is down or not properly configured
[net.anotheria.moskito.webui.util.APILookupUtility.findRemote(APILookupUtility.java:168), net.anotheria.moskito.webui.util.APILookupUtility.findRemote(APILookupUtility.java:142), net.anotheria.moskito.webui.util.APILookupUtility.getAdditionalFunctionalityAPI(APILookupUtility.java:113),
Q1: How to set -DlocalRmiRegistryPort=9401 on tomcat on Windows OS.
Q2: Once we set port, how to ensure port is set properly and its up and ready state for connection.
Q3: Once port is set properly, then how to set / configure specified java web application needs to be configured for MoSKito, since we could have more than one java web application on same tomcat server.
Please guide me, thank you :)
in order to be able to connect to a MoSKito monitored application you have to enable an agent that binds itself to port 9401 and answers your replies. To do so, you have to add a dependency to your pom (if you are using maven):
<!-- Enabling MoSKito Inspect Embedded -->
<dependency>
<groupId>net.anotheria</groupId>
<artifactId>moskito-inspect-remote</artifactId>
<version>${moskito.version}</version>
</dependency>
Please keep in mind that the current MoSKito version is 2.5.4
If you are using a servlet 3.0 compatible container (tomcat 7, jboss wildfly) you are done. If you are using an older container you may need to add following to your web.xml:
<listener>
<listener-class>net.anotheria.moskito.webui.embedded.StartMoSKitoInspectBackendForRemoteListener</listener-class>
</listener>
If you are using no container at all, you can start it manually:
net.anotheria.moskito.webui.embedded.StartMoSKitoInspectBackendForRemote.startMoSKitoInspectBackend()
Now for you questions:
Q1: via catalina.bat. You were doing it correctly.
Q2: if the agent is configured everything will work correctly.
Q3: You may want to check out step by step guide here:
http://blog.anotheria.net/msk/the-complete-moskito-integration-guide-step-1/
Regards
Leon
I want to install Apache Tomcat in fedora 20.
I have try to install tomcat using below code
yum install tomcat.
Then, enable Tomcat on system startup:
systemctl enable tomcat
Command is executed properly, but when try http://127.0.0.1:8080 it shows a blank page.
Can anyone tell me, how I have to configure tomcat to make it work properly?
I recently got the same problem. And if you are used to yum but not used to install applications manually you can find below how I resolved the problem using yum:
# yum search tomcat
Loaded plugins: langpacks, refresh-packagekit
tomcat-native.x86_64 : Tomcat native library
jglobus-ssl-proxies-tomcat.noarch : Globus Java - SSL and proxy certificate support for Tomcat
jspc-compiler-tomcat6.noarch : JSPC Compiler for Tomcat6
openwebbeans-tomcat7.noarch : Apache OpenWebBeans Tomcat 7 plugin
springframework-instrument-tomcat.noarch : Spring Instrument Tomcat Weaver
**tomcat-admin-webapps.noarch : The host-manager and manager web applications for Apache Tomcat**
tomcat-docs-webapp.noarch : The docs web application for Apache Tomcat
tomcat-javadoc.noarch : Javadoc generated documentation for Apache Tomcat
tomcat-jsp-2.2-api.noarch : Apache Tomcat JSP API implementation classes
tomcat-jsvc.noarch : Apache jsvc wrapper for Apache Tomcat as separate service
tomcat-lib.noarch : Libraries needed to run the Tomcat Web container
tomcat-servlet-3.0-api.noarch : Apache Tomcat Servlet API implementation classes
**tomcat-webapps.noarch : The ROOT and examples web applications for Apache Tomcat**
tomcatjss.noarch : JSSE implementation using JSS for Tomcat
pki-tps-tomcat.noarch : Certificate System - Token Processing Service
tomcat.noarch : Apache Servlet/JSP Engine, RI for Servlet 3.0/JSP 2.2 API
tomcat-el-2.2-api.noarch : Expression Language v1.0 API
Because I ran into this dependency conflict:
Error: tomcatjss conflicts with tomcat-native-1.1.30-1.fc20.x86_64
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest
I run yum install with the skip-broken flag:
# yum install --skip-broken tomcat*
And now after
# service tomcat restart
the Tomcat start Page appears as expected.
Hi
We started to create our applications with J2EE. We now created a Webservice and deployed it to the Glassfish Server. We have written an apache proxy rule to access it via https://our.server.com/webservice-war (only https port is open to that server):
ProxyRequests Off
ProxyPass /webservice-war http://our.server.com:8080/webservice-war
ProxyPassReverse /webservice-war http://our.server.com:8080/webservice-war
Now everything works fine, but when we go to the to the ServiceEndpoint page (which is automatically generated) there is a link to the WSDL page:
http://our.server.com:8080/webservice-war/HostaliasSearchImplService?wsdl
which is obously wrong (Glassfish listens to port 8080). and also https is changed to http
Anyone an idea how I can fix it, that the automatically generated link is:
https://our.server.com/webservice-war/HostaliasSearchImplService?wsdl
BR, Rene
I discovered what I consider to be a very simple and elegant way to deal with the issue: use mod_substitute. Since those of us with this problem are already using Apache, and it's built in and simple, I liked this approach best.
I put a block similar to the below in one of my Apache conf files and found joy:
<Location />
AddOutputFilterByType SUBSTITUTE text/xml
Substitute "s|http://internal:8080/foo|https://external/foo|ni"
</Location>
Found the solution!
Anonym gave me a good hint about mod_jk. So here the complete configuration (for RHEL5).
First of all Download the mod_jk module for apache: http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/linux/jk-1.2.31/x86_64/
Put in in the modules directory /etc/httpd/modules and make it executeable:
chmod +x mod_jk-1.2.31-httpd-2.2.x.so
After that create /etc/httpd/conf/workers.properties:
# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
The Port 8009 is the where the Glassfish jk connector listens (we come to that later).
No we have to configure mod_jk, therefore create the file: /etc/httpd/conf.d/mod_jk.conf with the following content:
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.x.so
JkWorkersFile /etc/httpd/conf/workers.properties
# Where to put jk logs
JkLogFile /var/log/httpd/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel debug
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send everything for context /atsi-war to worker named worker1 (ajp13)
JkMount /yourapp-war/* worker1
(This means everything from your http://apache.webserver.com/yourapp-war/ will bi redirected to Glassfish yourapp-war application context)
Important, if you are using virtual hosts on apache, you have to set the option:
JkMountCopy On
for your virtual servers. Explication:
If this directive is set to "On" in
some virtual server, the mounts from
the global server will be copied to
this virtual server, more precisely
all mounts defined by JkMount or
JkUnMount.
Now we have to create the jk connecter in glassfish:
asadmin create-http-listener --listenerport 8009 --listeneraddress 0.0.0.0 --defaultvs server jk-connector
asadmin set configs.config.server-config.network-config.network-listeners.network-listener.jk-connector.jk-enabled=true
Restart Glassfish, and everything sould work.
As for rewriting the https -> http, I'm not sure that's possible(yet) without using mod_jk, see here
, but see also this little guide
Though, generally, you'll need configure Glassfish and set http.proxyPort (and probably http.proxyHost too). Hopefully that should reflect in the autogenerated WSDL URL.
Here's 3 different ways to do this:
1
Use asadmin (in the Glassfish bin/ directory, run
asadmin create-jvm-options "-Dhttp.proxyPort=80"
asadmin create-jvm-options "-Dhttp.proxyHost=our.server.com"
2
Edit domain.xml and add under the <java-config> element
<jvm-options>-Dhttp.proxyPort=80</jvm-options>
<jvm-options>-Dhttp.proxyHost=our.server.com</jvm-options>
3.
Open the Glassfish admin web page, under Application Server -> VM Settings -> JVM Options and add these options
http.proxyPort=80
http.proxyHost=our.server.com
Setting
server-config.network-config.protocols.protocol.http-listener-1.http.server-name=MyHost:80
on GlassFish Server Open Source Edition 3.1.2.2 (build 5) solved problem to me.