juneau-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steve Blackmon <st...@blackmon.org>
Subject Re: Trouble getting servlets to initialize on startup (probably user error)
Date Thu, 29 Mar 2018 20:03:21 GMT
 I went down the path of jetty.xml trial-and-error for a while, but
ultimately found it easier to solve like this:

package com.peoplepattern.streams.execution.api

import org.apache.juneau.microservice.RestMicroservice
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.ServletContextHandler

import scala.collection.JavaConversions._

object StreamsExecutionMicroservice extends RestMicroservice {

  @throws[Exception]
  def main(args: Array[String]): Unit = {
    new StreamsExecutionMicroservice(args(0)).start.join
  }

}

class StreamsExecutionMicroservice(args: String*) extends
RestMicroservice(args(0)) {

  override def createServer() : Server = {
    val server = super.createServer()
    val context = new ServletContextHandler(ServletContextHandler.SESSIONS)
    context.addServlet(classOf[RootResources].getCanonicalName,
"/*").setInitOrder(0)
    server.setHandler(context)
    server
  }

}

Working exactly as expected now.  Thanks for the pointer, James.


On Mar 29, 2018 at 12:18 PM, James Bognar <jamesbognar@apache.org> wrote:


Hi Steve,

I don't have a complete answer, but....

The behavior is defined in Jetty.  It looks like
ServletHolder.setInitOrder(0) is the programmatic way of forcing a servlet
to initialize during startup.

The XML you see below....

    <New id="context"
class="org.eclipse.jetty.servlet.ServletContextHandler">
        <Set name="contextPath">/</Set>
        <Call name="addServlet">
            <Arg>com.peoplepattern.streams.execution.api.RootResources</Arg>
            <Arg>/*</Arg>
        </Call>
        <Set name="sessionHandler">
            <New class="org.eclipse.jetty.server.session.SessionHandler" />
        </Set>
    </New>

...is equivalent to calling...
servletContextHandler.addServlet("
com.peoplepattern.streams.execution.api.RootResources", "/*");

There is a 2nd method on ServletContextHandler that allows you to pass in a
ServletHolder...
addServlet(ServletHolder servlet, java.lang.String pathSpec)

So just taking a stab at it, maybe something like this would work?  (I'm no
expert on the jetty.xml files, so I have no idea if this is valid
syntax)....

    <New id="context"
class="org.eclipse.jetty.servlet.ServletContextHandler">
        <Set name="contextPath">/</Set>
        <Call name="addServlet">
            <Arg>
                <New class="org.eclipse.jetty.servlet.ServletHolder">
                <Set name="servlet">
com.peoplepattern.streams.execution.api.RootResources</Set>
                <Call name="setInitOrder">
                    <Arg>0</Arg>
                </Call>
            <Arg>/*</Arg>
        </Call>
        <Set name="sessionHandler">
            <New class="org.eclipse.jetty.server.session.SessionHandler" />
        </Set>
    </New>

</Arg>




On Thu, Mar 29, 2018 at 12:16 PM, Steve Blackmon <sblackmon@apache.org>
wrote:

> I’m trying to configure Juneau to ensure that initialization of my
> resources occurs as the server comes up.
>
> I’m using @RestHook(INIT) and @RestHook(POST_INIT_CHILD_FIRST) to
> annotate methods which instantiate helper classes, and confirm that
> everything is healthy, respectively.
>
> I’m finding however that those methods are not called until the first
> request comes in.
>
> My RootResources class extends ResourceGroup and has RestHook annotated
> methods.  It also defines ~10 sub-resources which have them as well.
>
> I’ve attached my microservice.cfg and jetty.xml files.
>
> Any advice would be greatly appreciated!
>
> Steve
>
>
>

Mime
View raw message