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

I went ahead and added an example to the Embedded Jetty Cookbook.

https://github.com/jetty-project/embedded-jetty-cookbook/blob/master/src/main/java/org/eclipse/jetty/cookbook/DelayedWebAppDeployExample.java

That explains how to accomplish what you need.

Joakim Erdfelt / joakim@xxxxxxxxxxx


On Mon, Oct 7, 2019 at 9:36 AM Bill Ross <ross@xxxxxxxxxxxx> wrote:
(and return after the 503)


On 10/7/19 7:35 AM, Bill Ross wrote:
> 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
> _______________________________________________
> 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
>
_______________________________________________
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