I am using Apache Tomcat 9 server as a Maven dependency in my project. It is working fine and now I need to add a health endpoint so that it will return 200 OK if everything is running fine.
I came to know about HealthCheckValve (https://tomcat.apache.org/tomcat-9.0-doc/config/valve.html#Health_Check_Valve) option in Tomcat 9 which is helpful. But I am not been able to figure out where to add this and the process of configuring this valve. As I know if server is standalone we can configure in Server.xml but as the Tomcat Server is a maven dependency I don't know how and where I should configure this.
Can somebody please help me in configuring health endpoint in Apache Tomcat 9 (as a maven dependency) ?
See the documentation, then add the HealthCheckerValve to server.xml. Valves go into either the Engine, Host or Context element. In the server.xml packaged with Tomcat you can find comments that should direct you to the right location.
When embedding a version of Tomcat, you won't have this file available, and so you need to assemble instances of these containers programmatically.
Check the launcher application in this example: https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/basic_app_embedded_tomcat/basic_app-tomcat-embedded.html
While I could not find methods like addValve() I found an init() method that you could use to provide a server.xml which will be read by Tomcat.
I saw the documentation of all valves available in Tomcat 9.0.x.
In order to find the solution of this specific task, I tried looking for configuration of other valves such as Remote Address Valve in embedded tomcat.
I found a solution by user967710 after searching a lot.
I did the following to add a Health Check Valve to my Tomcat 9.0.64 :
Tomcat tomcat = new Tomcat();
tomcat.getEngine().setName(UUID.randomUUID().toString());
tomcat.setPort(context.port);
tomcat.setHostname(context.hostname);
tomcat.getHost().setAppBase(".");
Valve valve = new HealthCheckValve();
tomcat.getHost().getPipeline().addValve(valve);
It doesn't matter how you configure the Tomcat for your project i.e from line 1 ~ 5 but actually last 2 lines i.e 6 and 7 are important where you are adding the valve.
The health endpoint can be accessible on host:port/health.
For e.g if it is hosted at http://localhost:4000 then the health endpoint would be http://localhost:4000/health
This endpoint will return 200 OK with a simple JSON response stating the Tomcat server status i.e "UP" if everything is up and running.
Related
We have a java web application which was hosted in tomcat 8. For session Management , we have been using the memcache which maintains non sticky based sessions. This has been working fine without any issues.
Now there is a requirement to upgrade the tomcat from 8 to tomcat 9 and the upgradation with the necessary jars for the tomcat 9 has been done. After the upgradation, we are facing an issue in the application where the session is becoming null. Some of the link in the app associated with the session are working fine and some of them when clicked gets me logged out of the session. Checking the logs, I see the session object null.
The memcached jars used as part of the tomcat 9 and copied under tomcat9/lib
memcache-session-manager-2.3.2
memcache-session-manager-tc9-2.3.2
jettison-1.1.jar
spymemcached-2.12.0
http-core-4.3
http-core-nio-4.3
we are using the 3rd party Serialization for this use case and they are also copied in the tomcat lib folder
kryo-3.0.3.jar
kryo-serializers-0.37.jar
minlog-1.3.0.jar
msm-kryo-serializer-1.9.3.jar
objenesis-2.1.jar
protobuf-java-2.6.1.jar
reflectasm-1.10.1.jar
With these jars , I am able to see the tomcat startup without any issues.
**<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:ip:port"
lockingMode="auto"
sticky="false"
requestUriIgnorePattern= ".*\.(png|gif|jpg|css|js)$"
sessionBackupAsync= "false"
sessionBackupTimeout= "100"
copyCollectionsForSerialization="false"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>**
Followed the below link for setting up the memcache configuration in my application.
https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration
Please help me if i am missing anything in the configuration.
Thanks
Pradeep
Have solved this issue by removing the param **requesturiignore pattern ** from the context.xml as this was creating different sessions.
Generally, we downloaded the TOMCAT to the computer and configure the context where it stores the project path then run the TOMCAT. So we can access our web project.
Is there a possibility that we can start the TOMCAT by just using java code.
like
Tomcat tomcat = new Tomcat(configuration);
tomcat.run();
Can not understand what are you trying to achieve? But, Spring Boot comes with embedded tomcat and your application is deployed automatically on tomcat (with default configurations). If you want to change configurations for the tomcat you can specify those configurations in application.properties.
Like, e-g if you want to change port of your server you can specify following property in application.properties.
server.port = 8085
There are other server related properties for which you can refer to https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html.
You can also implement WebServerFactoryCustomizer
in order to configure tomcat with java. For this purpose you can refer to https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-web-servers.html
It is a web service deployed on Apache Karaf using camel-cxf. I am able to see the cxf service listing in URL localhost:8181/cxf which has some rest and soap services deployed on it.
The problem is it is returning the service listing whenever any request comes with keyword "services". For example the url http://localhost:8181/abcd/services returns cxf service listing page instead of processing the actual request.
I got to know from http://cxf.apache.org/docs/jaxrs-services-description.html that its is because of the default value of service-list-path of CXFServet is services.
Here is my Question. If I want to override this, I should set this property in etc/org.apache.cxf.osgi.cfg. This cfg file is not present under etc folder in my karaf. What are the steps to be taken if I am creating this property file manually? What features I need to install? Or creating this cfg is sufficient ?
Appreciate your help !
There should be no extra installation requirements, just create a new file etc/org.apache.cxf.osgi.cfg.
There are three settings you may be interested in:
org.apache.cxf.servlet.context = /mycxf
org.apache.cxf.servlet.service-list-path = /myservices
org.apache.cxf.servlet.hide-service-list-page = false
Where the default URL for the CXF service listing is usually like http://localhost:8181/cxf/services, with the changes above the URL would become http://localhost:8181/mycxf/myservices
If you change from false (default value) to true, then your services will be hidden and you will instead get a page stating No service was found.
Because these are initialisation settings you need to shut down Karaf for the changes to apply.
I see several points here --
The CXF framework is installed by default in karaf under the context-path /cxf.
/cxf/services can be considered as a CXF internal app that displays the list of services deployed in CXF. I don't think you can configure the name "services" here (and why would you change that?)
the "url-pattern in web.xml" you speak of (if I understand correctly) determines the context path of your servlet/application. You can specify this is camel like this:
<cxf:rsServer id="secureRsServer" address="https://0.0.0.0:8182/my/path/"
serviceClass="....">
(for the RS Server, probably same for the WS server).
This question already has answers here:
Set CORS header in Tomcat
(4 answers)
Closed 7 years ago.
I am trying to consume some web services which are cross domain. When I disable chrome's web-security it is working fine. I want it to work without this so I have tried adding cross-domain.xml and still it didnt work. When i searched more, came to know about CORS enabling in tomcat.
from http://www.w3.org/wiki/CORS_Enabled
For Apache
Apache can be configured to expose this header using mod_headers. This is enabled by default in Apache, however you may want to ensure it's enabled in your deployment by running the following command:
a2enmod headers
To expose the header, you can add the following line inside , , and sections, or within an .htaccess file.
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
Can anyone please let me know where to add these configurations in TOMCAT and in which files exactly. I am using tomcat from eclipse.
Appreciate any help.
CORS support in Tomcat is provided via a filter. You need to add this filter to your web.xml file and configure it to match your requirements. Full details on the configuration options available can be found in the Tomcat Documentation.
Check this answer: Set CORS header in Tomcat
Note that you need Tomcat 7.0.41 or higher.
To know where the current instance of Tomcat is located try this:
System.out.println(System.getProperty("catalina.base"));
You'll see the path in the console view.
Then look for /conf/web.xml on that folder, open it and add the lines of the above link.
Just to add a bit of extra info over the right solution. Be aware that you'll need this class org.apache.catalina.filters.CorsFilter. So in order to have it, if your tomcat is not 7.0.41 or higher, download 'tomcat-catalina.7.0.41.jar' or higher ( you can do it from http://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina ) and put it in the 'lib' folder inside Tomcat installation folders.
I actually used 7.0.42
Hope it helps!
I am not clear on the following:
If we have a web application named: SomeWebApp under Tomcat's webapp directory, the url to access it is:
http://localhost:8080/SomeWebApp
My question is, is it possible to configure Tomcat so that other URLs would point to that web application?
E.g.
http://localhost:8080/ADifferentApp will also point to the SomeWebApp?
From the web.xml I think is not possible since it is about the url patterns when you are inside the SomeWebApp scope.
So what is the proper way to do it? If it is possible that is.
The approach I found to work best is to install Apache2 on the server and proxy all requests. Tomcat is surprisingly difficult to configure in other ways than intended. In my experience, Tomcat doesn't provide this functionality declaratively.
I'd rather recommend Nginx than Apache as proxy. I'm recently working on a project that incorporates tomcat and nginx works as proxy.
Once you've got nginx you can acctualy map as many url's to access the same web application as you want.
Yes,its possible to map different context path to single application edit conf/server.xml file
> **> <Context docBase="D:\Servers\apache-tomcat-7\webapps\SomeWebApp"
> > path="/SomeWebApp" />
> > <Context docBase="D:\Servers\apache-tomcat-7\webapps\SomeWebApp" path="/ADifferentApp "/>**
Access application with 2 URL's