How to retrieve Spring context loaded in Tomcat from outside application - java

I'm trying to do the following:
Imagine you have a Java EE application running on Tomcat using Spring as the IoC engine.
I have another jar in the application that has full access to all the resources. i.e. I can instantiate the same application context that is running in tomcat but it takes around 30 seconds to instantiate all the dependencies.
Anyone knows if it is feasible to retrieve current tomcats ApplicationContext from the outside?
There is a way to ask for the WebApplicationContext inside a servlet but I'm not on it, I only have a jar with a main method.

No. You have to be running within tomcat (a webapp) to be able to access the servlet context (and from there - the application context). You are not even in the same runtime with the main method.
If you want to get some information from the context, you should expose it as a service. For example:
a restful service, via Spring-MVC
via JMX
via JNDI

Related

Accessing Tomcat 8 JNDI from Standalone JVM

I have a legacy webapp recently converted to running on a Tomcat 8 server that calls a java command (Runtime.getRuntime().exec) in the contextInitialized method. This java program then needs to lookup and use the resources (database info) that are set up in the Tomcat context.xml file. It doesn't appear that Tomcat exposes these resources like Websphere.
So what is my best route to access these resources in this separate JVM? Maybe there is a context friendly way to spin off the java process?
Simply put, you can't. The JNDI context is available as read-only from within the webapp.

Why can't java webapps have 2 webcontexts?

A java webapp must choose between either static context or "webcontext". Why do we need a webcontext just for a web server like jetty and why must we route everything to the same "webcontext" ?
Because Jetty is a JEE servlet container and in the JEE world there is a one to one relationship between a webapplication and the web context. The intention is to be able to run several independent webapplications within the same servlet container. So it is easy to route to the appropriate webapplication by the first part of the URL path.
Theoretically it would be possible to declare more than one webcontext for a webapplication but it is specified otherwise. See section 10.2 "Relationship to ServletContext" in Java Servlet Specification 3.1:
The servlet container must enforce a one to one correspondence between
a Web application and a ServletContext. A ServletContext object
provides a servlet with its view of the application.

Load webapp on container startup without web.xml in Tomcat+Jersey?

I currently have a Jersey webapp without a web.xml. It deploys nicely, but doesn't start up until it receives its first web request.
To get the webapp to load at startup, I could create a web.xml for the webapp and give a load-on-startup tag. However, I'd strongly prefer not to make a web.xml.
Is there a way to get a JAX-RS application to load at startup without web.xml? I'll even accept a solution that is specific to Jersey and/or Tomcat.
EDIT: I would also accept a solution that loads all apps in a Tomcat instance eagerly.
EDIT: Let me give a little more information on how the app is being deployed, per a comment.
The deployment process is not sophisticated.
The App will live on an EC2 instance running Ubuntu 12.04. I'm setting up one instance of the App by hand; once it works, I will make an AMI of the app and create additional copies of it as needed.
To deploy the app on the initial instance, I'm simply copying the WAR file to /var/lib/tomcat7/webapps and restarting Tomcat. No other webapps will be running on this Tomcat instance.
If any additional information would be useful, let me know! I'll happily add it.
EDIT: For clarity's sake, this is how my webapp Application class looks, at a high level:
#ApplicationPath("/")
public class App extends ResourceConfig {
// ...
}
I'm using the Jersey-specific ResourceConfig class instead of the more general JAX-RS Application class because I'm using Jersey's built-in HK-2 to do some dependency injection.
The only way I can think of to do that is to switch to setting up the Jersey ServletContainer yourself and set its "load on startup" value to something greater than zero. You might use a ServletContainerInitializer (no relation--the naming is just a coincidence) to do it. If you happen to be using Spring, its WebApplicationInitializer offers the same mechanism with a slightly more convenient interface.
Another, rather hacky, way would be to write a class that extends ServletContainer and give it an appropriate Servlet 3.0 annotation, something like #WebServlet(value="/", loadOnStartup=1)
One solution would be to force a first request to the app by simply adding a call to curl or wget to your deployment script. It has the additional advantage of warming up any caches. And it can be used for testing if the deployment and the app really work. (Just check HTTP status code or some text on the response page...)

Accessing servlet class in Message Driven Bean

We have a web application which is an ear file containing 1 war and 1 MDB jar.
Problem is that I load some properties file in my servlet when first request for servlet comes. These properties I will also need in my MDB. How can I access my Servlet class(inside war) in the EJB jar classes. Is there any way to use my war file as a utility jar.
Application is running on weblogic server.
You don't as a servlet is invoked through a HTTP call. You could hack it by doing an URLConnection to the servlet, but I would rather refactor your code a bit so you can call the initialization code on server startup in stead of by invoking the servlet. A context listener or an EJB init() could already do it.
Extract out your property management into a common, utility JAR that can be accessed from both tiers of your app.

Embedded Jetty and Spring Web MVC

For a pet project I would like to have an embedded Jetty run a Spring Web MVC app. I've used Spring in web containers (where it's easy to tell "where to start") and I've used embedded Jetty without Spring.
It feels a bit like the chicken or the egg problem if I want both to work together. What is the best way to organize the project? In other words, what shall I put in main()? Should it be a Spring app that happens to have Jetty as a bean (what about contexts then?)? Or should I start Jetty alone and plug Spring in via servlet listener? What are the caveats?
Jetty in a Spring container is used to start webapp, springified or not. The webapp and your webapp don't have the same Spring context without tricks.
So, you have to create a Jetty server in your main, add your webapp and start the server. The best way is using a web.xml like a common Java EE server, and add this descriptor to your Jetty server.
I think it is more reasonable to start Jetty alone and plug Spring in via servlet listener in web.xml. Let Spring manager all the app specific beans and let jetty focus on running your app, and maybe some day you can deploy you app to antoher servlet container without changing anything.
This is one way to embed Jetty in Spring
http://www.springbyexample.org/examples/embedded-spring-web-services.html

Categories

Resources