views:

60

answers:

3

Is there any Tomcat API or configuration available which can tell an application (probably on startup), what port its running on without a request??

*EDIT*

Imagine a scenario where there are two web applications running in the same tomcat and one of which need to invoke a web service from the other one. We don't want the request to leave the tomcat (if you use the apache server name or absolute URL, the request will go out and come back again and it can go to any instance) and come back in. For that I know the name of the machine but no way to get the port number. I know I can hard code this information but I don't want to do this as I want my war file to be application server agnostic.

**I know that we can find it if we have a HTTPServletRequest

A: 

Hmm, how would an application get started in Tomcat without a request? Maybe I'm going brain dead for a moment here, but I don't think any classes will load until a request hits. Sure, you could have classes independent of any particular request, but they'd need a request to get them fired off at some point.

Jay
I mean when the application deploys in any application server, there will be some bootstrap code or init params in servlets which bring application up and running ready to serve requests. When spring MVC application loads it loads all the configuration and I need to know the port number in this process. I will update the question with more details.
Teja Kantamneni
A: 

I am not entirely sure if you can access the Tomcat port from code in the environment configuration you need. Did you consider actually having the full URL to the web service passed as a configuration param/setting (probably in a .properties file) to the app?

This way you wouldn't have to hardcode the port and de-couple both your apps so that you could technically have the web service on an external tomcat but still access it by just changing the property, avoiding code re-build.

Sarat
Yes, we actually considered it, but the problem here is every machine has more than one tomcat instance (can this be any more complicated?) and obviously they run on different ports. So the port cannot be hardcoded.
Teja Kantamneni
Are you saying that the web service (URL and port) moves around so often that passing it through a setting will not work? If so, it looks like there is a problem with the architecture. If not, then passing it through the settings wont really be hard coding it as long as we know (manually) where the web service (URL + port) resides on?
Sarat
@Sarat In a cluster there could be number of tomcat instances and my goal is to redirect the request from one app to another app should go to same tomcat instance not any random tomcat instance.
Teja Kantamneni
A: 

You could use crossContext. But I don't think that's app server agnostic.

I would share a custom class, behaving as a registry of running applications in the same tomcat instance through JDNI, as I explained here.

During startup, through a ContextListener or through an Spring container event, I would obtain the registry through a JNDI lookup, add my web app instance with an url obtained from the servletcontext.contextpath, and finally register a listener to hear other applications registering themselves. That's the more server agnostic I can think of.

Obtaining the port won't be server agnostic, you should use a context parameter.

Regards.

EDIT: I'm sorry, forgot to say that what I've described is to share objects among contexts, but no, you can't not know the port unless you use some server API (not agnostic at all).

mrrtnn