Where to initialize a MySQL Connection Pool in a Jersey REST service? - java

I would like to use a MySQL Connection Pool with a Jersey REST service.
For performance reasons I don't want to create single MySQL connections each time a Jersey resource method is called. Instead I would like to create a ConnectionPool at startup-time of the server and then borrow and return connections when the service resource methods are executed.
Where would be the best place to put the initialization of the ConnectionPool within the Jersey REST framework?

Create a class and implement the ServletContextListener interface.
package com.example.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyAppServletContextListener
implements ServletContextListener{
#Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContextListener destroyed");
}
//Run this before web application is started
#Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("ServletContextListener started");
}
}
Put it in the deployment descriptor.
<web-app ...>
<listener>
<listener-class>
com.example.listener.MyAppServletContextListener
</listener-class>
</listener>
</web-app>
For more details check this link

Related

What is the main method of a WAR file?

Recently, I've reapackaged my java spring application to become a WAR file for deployment in tomcat. After some testing I noticed, that public static void main(String[] args) is not executed. Some necessary initialization of my application is done in main. Is there something like a main method in a WAR file? What is the appropiate place in a WAR file to run some initialization?
You can add a listener to your web.xml file:
<listener>
<description>Application startup and shutdown events</description>
<display-name>Test</display-name>
<listener-class>com.package.package.StartClass</listener-class>
</listener>
public class StartClass implements ServletContextListener {
#Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
//Context destroyed code here
}
#Override
public void contextInitialized(ServletContextEvent servletContextEvent)
{
//Context initialized code here
}
}
Well, You will have to create a listener in your web.xml that will be invoked by container at the time of startup.
<listener>
<listener-class>com.rdv.example.WebAppContext</listener-class>
</listener>
And this class will be implementing ServletContextListener
public class WebAppContext implements ServletContextListener {
public void contextInitialized(ServletContextEvent servletContextEvent) {
// Do your processing that you are trying to do in main method.
}
I've found another way, that is independent of spring and tomcat: The #PostConstruct annotation. In code:
#PostConstruct
public void init() {
// initialization code goes here
}
This method gets executed whether I run my application standalone or in tomcat.
For more information see How to call a method after bean initialization is complete?
or Init method in Spring Controller (annotation version)

how to execute some initialization code in a war package in tomcat?

I'm using a framework to deal with some request, now, I can start it from a main function like this:
public Main{
public static void main(String[] args) {
//init the framework
Init initer = new Init();
initer.initFramework();
//start my besiness code below
}
}
now I need to package my project in a war format and put it into tomcat, how can I be able to excute the initialization code?
When you start a war-file in Tomcat using servlet 2.3 or higher you can use the web.xml file to declare listeners.
<listener>
<listener-class>test.MyListener</listener-class>
</listener>
This listener should implement the ServletContextListener interface. Listeners gets notified of lifecycle changes (such as when the servlet context has been initialized). The listener can be implemented to run your initialization code as illustrated below:
public class MyListener implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent event) {
Init initer = new Init();
initer.initFramework();
}
#Override
public void contextDestroyed(ServletContextEvent event) {
// destroy stuff
}
}
If you are running the war-file in a servlet 3 environment there might not even be a web.xml file. In that case the #WebListener annotation can be used to indicate that your component should be called. The JavaDoc for #WebListener can be found here but basically what you need to do is to add the annotation to your listener.
Just implement org.springframework.web.WebApplicationInitializer and that's it, no xml.

With Spring how can I execute code after HIbernate is initialized but before the application starts?

I want to insert some data into the database using the application API if the application has started up with some empty database tables. How do I go about this?
I'm using Spring 3.1, Hibernate 4.1.1.
[edit]
Thanks to AlexR the answer is to sublcass ContextLoaderListener, call super in contextInitialized and then do whatever it is you need to do:
public class MyContextLoaderListener extends ContextLoaderListener {
public void contextInitialized(final ServletContextEvent event) {
super.contextInitialized(event);
// ... doStuff();
}
}
You may also need to wire this up in web.xml instead of the Spring one:
<listener>
<listener-class>com.example.MyContextLoaderListener</listener-class>
</listener>
You can use spring context listener.
Take a look on: http://fusesource.com/docs/framework/2.2/deploy_guide/CXFServletDeploySpring.html
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/context/ContextLoaderListener.html
http://forum.springsource.org/showthread.php?88896-Spring-MVC-3-is-ContextLoaderListener-needed

Trigger function when Undeploying application

How do I automatically trigger Java function to stop Quartz scheduler jobs when I deploy/undeploy/redeploy JEE5 application in Glassfish.
Implement ServletContextListener and hook on contextDestroyed().
Basic example:
public class Config implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
// Write code here which should be executed on webapp startup.
}
public void contextDestroyed(ServletContextEvent event) {
// Write code here which should be executed on webapp shutdown.
}
}
and register it as a <listener> in web.xml.
<listener>
<listener-class>com.example.Config</listener-class>
</listener>
Once you get to JAVA EE-6+, annotate a class with #WebListener and implement ServletContextListener on that class to get a shutdown notification. No need to deal with web.xml. See here

Is there a way to run a method/class only on Tomcat/Wildfly/Glassfish startup?

I need to remove temp files on Tomcat startup, the pass to a folder which contains temp files is in applicationContext.xml.
Is there a way to run a method/class only on Tomcat startup?
You could write a ServletContextListener which calls your method from the contextInitialized() method. You attach the listener to your webapp in web.xml, e.g.
<listener>
<listener-class>my.Listener</listener-class>
</listener>
and
package my;
public class Listener implements javax.servlet.ServletContextListener {
public void contextInitialized(ServletContext context) {
MyOtherClass.callMe();
}
}
Strictly speaking, this is only run once on webapp startup, rather than Tomcat startup, but that may amount to the same thing.
You can also use (starting Servlet v3) an annotated aproach (no need to add anything to web.xml):
#WebListener
public class InitializeListner implements ServletContextListener {
#Override
public final void contextInitialized(final ServletContextEvent sce) {
}
#Override
public final void contextDestroyed(final ServletContextEvent sce) {
}
}
I'm sure there must be a better way to do it as part of the container's lifecycle (edit: Hank has the answer - I was wondering why he was suggesting a SessonListener before I answered), but you could create a Servlet which has no other purpose than to perform one-time actions when the server is started:
<servlet>
<description>Does stuff on container startup</description>
<display-name>StartupServlet</display-name>
<servlet-name>StartupServlet</servlet-name>
<servlet-class>com.foo.bar.servlets.StartupServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

Categories