Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] ServletContextListener defers server start

Would anything like these chunks from my code work for you? The secret is checking status in doGet().


public class GetMult extends HttpServlet {

    private ServletData servletData;

    public void init(ServletConfig config) throws ServletException {

        servletData = ServletData.get(); // singleton

    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException  {

        if (!servletData.isReady()) {

            res.sendError(503, "Put another quarter million in the slot");

        }

        // do it

    }

}

public class ServletData {

    private static Logger log = LoggerFactory.getLogger(ServletData.class);

    // calling the constructor here hangs sync on the parallel loads
    // for some reason
    private static ServletData single = null;

    public static ServletData get() {
        synchronized(ServletData.class) {
            if (single == null) {
                single = new ServletData();
            }
        }
        return single;
    }

    private ServletData() {

            Thread th1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        approvedV = ApprovalDao.getAllApprovedPairs(fconn, 1,
                                                     "v", null,
                                                     true, // d0
                                                     null, null,
                                                     null);
                    } catch (SQLException sqe) {
                        log.error("approvedV: " + sqe);
                        System.exit(1);
                    }
                }
            });
            th1.start();

    }

    public isReady() {

        return approvedV != null;

    }

}

On 10/7/19 5:34 AM, Dirk Olmes wrote:
On 10/7/19 11:21 AM, Bill Ross wrote:
Why not have a ContextHandler that starts a setup thread on init, and
answers 503 until that thread is done?
That's sort of what I currently do (starting a setup thread from the
main servlet's init method) but that causes all kinds of trouble because
the servlet context accepts requests before all the app logic is ready.

Based on your suggestion combined with Greg's before I came up with this
approach: Register a ContextHandler that answers 503 and let the server
start normally. After the server is started I launch the setup thread
where I register the real app context with the server and manually send
it a start(). After the context is started I unregister the
ContextHandler that answers 503.

The code is here:
https://github.com/dirk-olmes/jetty-delayed-context-startup/tree/deferred_context_startup

Ok, this approach works. But I was hoping that Jetty supported something
like that out of the box.

-dirk
_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/jetty-users


Back to the top