0
votes

I have a Java Servlet which invokes a MMQ Listener. I am deploying the app in Tomcat server

AppServlet.java

package com.cisco.cstg.platform;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AppServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    MessageListenerImpl m;

    public void init(ServletConfig config) throws ServletException {
        //super.init(config);
        //MainApp.main(null);
        m = MainApp.init();
        MainApp.start(m);
    }

     public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        PrintWriter out = res.getWriter();
        boolean dbStatus = EMANApi.isDBUpAndRunning();
        if (dbStatus == true) {
            out.print("SUCCESS");
        } else {
            out.print("FAILED");
        }
    }

    public void destroy() {

        MainApp.stop(m);

    }

}

MainApp.java

package com.cisco.cstg.platform;

import com.cisco.cstg.utilities.UtilClass;

public class MainApp {

    public static final org.apache.log4j.Logger log = org.apache.log4j.Logger
            .getLogger(MainApp.class);
    // Input arguments
    public static long keepAliveMinutes = 1L;
    public static boolean debug;

    public static void main(String args[]) {
        MainApp.fetchArgs(args);

        initLogging();
        if (debug)
            log.info(UtilClass.getCurrentDate()
                    + "Run Connector Message App for reading the queue");

        MessageListenerImpl m = MainApp.init();
        MainApp.start(m);
        UtilClass.keepAlive(keepAliveMinutes);
        // MainApp.stop(m);

        if (debug)
            log.info(UtilClass.getCurrentDate()
                    + "Run Connector CSOC Message App");

    }

    private static void fetchArgs(String[] args) {
        debug = log.isDebugEnabled();
        keepAliveMinutes = Integer.MAX_VALUE; // value is 2147483647 ms

        if (debug)
            log.info(UtilClass.getCurrentDate() + "Found keepAliveMinutes: "
                    + keepAliveMinutes + " debug: " + debug);

    }

    private static void initLogging() {

        if (debug)
            log.info(UtilClass.getCurrentDate() + "::initLogging Started");

        if (debug)
            log.info(UtilClass.getCurrentDate()
                    + "::Log level set"
                    + UtilClass.getProperties().getProperty(
                            UtilClass.PropertyKeys.PROP_LOG_LEVEL));

    }

    public static MessageListenerImpl init() {

        if (debug)
            log.info(UtilClass.getCurrentDate()
                    + "::Instantiating MessageListenerImpl");

        MessageListenerImpl m = new MessageListenerImpl();

        if (debug)
            log.info(UtilClass.getCurrentDate()
                    + "::Instantiated MessageListenerImpl");

        return m;
    }

    public static void start(MessageListenerImpl m) {

        if (debug)
            log.info(UtilClass.getCurrentDate() + "::Starting instance");
        m.start();
        if (debug)
            log.info(UtilClass.getCurrentDate() + "::Started instance");
    }

    public static void stop(MessageListenerImpl m) {

        if (debug)
            log.debug(UtilClass.getCurrentDate() + "::Stopping instance");
        m.stop();
        System.exit(1); // to stop the java program
        if (debug)
            log.debug(UtilClass.getCurrentDate() + "::Stopped instance");

    }

}

The servlet basically invokes the init method in the MainApp which starts the Listener. But when i am directly invoking the init method the logging is not working. When i invoke the main method directly from the AppServlet the Tomcat server is not getting started. Please let me know what is going wrong here.

web.xml

<session-config>
    <session-timeout>60</session-timeout>
</session-config>
<display-name>Archetype Created Web Application</display-name>
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>com.cisco.cstg.platform.AppServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/eman</url-pattern>
</servlet-mapping>

Catalina.out

31-Jul-2015 09:13:40.822 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.24 31-Jul-2015 09:13:40.823 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:
Jul 1 2015 20:19:55 UTC 31-Jul-2015 09:13:40.823 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:
8.0.24.0 31-Jul-2015 09:13:40.823 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:
Windows 7 31-Jul-2015 09:13:40.824 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:
6.1 31-Jul-2015 09:13:40.824 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:
amd64 31-Jul-2015 09:13:40.824 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:
C:\Program Files\Java\jdk1.8.0_25\jre 31-Jul-2015 09:13:40.824 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_25-b18 31-Jul-2015 09:13:40.824 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:
Oracle Corporation 31-Jul-2015 09:13:40.824 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:
C:\apache-tomcat-8.0.24 31-Jul-2015 09:13:40.825 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:
C:\apache-tomcat-8.0.24 31-Jul-2015 09:13:40.825 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\apache-tomcat-8.0.24\conf\logging.properties 31-Jul-2015 09:13:40.825 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 31-Jul-2015 09:13:40.826 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.endorsed.dirs=C:\apache-tomcat-8.0.24\endorsed
31-Jul-2015 09:13:40.826 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\apache-tomcat-8.0.24 31-Jul-2015 09:13:40.827 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=C:\apache-tomcat-8.0.24 31-Jul-2015 09:13:40.828 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=C:\apache-tomcat-8.0.24\temp 31-Jul-2015 09:13:40.828 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.1.33 using APR version 1.5.1. 31-Jul-2015 09:13:40.829 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 31-Jul-2015 09:13:41.537 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.0.1m 19 Mar 2015) 31-Jul-2015 09:13:41.686 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"] 31-Jul-2015 09:13:41.695 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"] 31-Jul-2015 09:13:41.697 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1342 ms 31-Jul-2015 09:13:41.726 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina 31-Jul-2015 09:13:41.727 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.24 31-Jul-2015 09:13:41.756 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive C:\apache-tomcat-8.0.24\webapps\CSPC_greenfield.war 31-Jul-2015 09:13:43.653 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 31-Jul-2015 09:13:43.907 INFO [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [222] milliseconds.

1
I will not attempt an answer because so much critical information (e.g. your web.xml) is missing. But I can tell you that if you run the MainApp class from its main() method then you are running a Java console application and not a web application. You need to let the servlet container handle this for you.Tim Biegeleisen
hi TimBiegeleisen added the web.xmlwandermonk
What is your expected output and what do you see instead? This code is a very big mess.Tim Biegeleisen
Probably one of your methods MainApp.init() or MainApp.start(m) never returns (in which case try running start(m) in a thread). Now, I'm not a 100% sure of what you're doing, Are you sure that Listener is meant to run in the way you're doing it? also, it seems you're logging everything, What does the log tell you? can you copy/paste the relevant part of catalina.out?morgano
The expected functionality here is calling the main() method of MainApp from the servlet and start the tomcat. In the earlier case only the init() method is MainApp is getting called and the logging does not take part and tomcat server starts. In the later case the main method gets invoked but the tomcat server is not getting started. I am very new to the servlets concept and i am trying to create a servlet layer for the existing app for monitoring purpose.wandermonk

1 Answers

-1
votes

The issue resolved when i called the debug and other methods in MainApp within MainApp.init() method.