0

I have one scenario, where I have to test remote functionality through multiple JVMs. I am trying to run JettyTestServer with two different ports. But I am getting below error while running Jetty on second port.

ERROR - Failed to Boot! Your application may not run properly
java.lang.IllegalStateException: Cannot modify after boot.

However, my test-cases are passing successfully. Remote functionality is working fine.

Here is my code:-

object JettyTestServer {
private val serverPort = System.getProperty("SERVLET_PORT", "8989").toInt
private var baseUrl = "http://localhost:" + serverPort

private val server: Server = {
val server = new Server(8989)
val context = getContext
server.setHandler(context)
context.addServlet("org.eclipse.jetty.servlet.DefaultServlet", "/")
val connector: SelectChannelConnector = getConnector(8989)
val connList: List[Connector] = List(connector)
server.setConnectors(connList.toArray);
server
}

private val remoteServer: Server = {
val server = new Server(8991)
val context = getContext
server.setHandler(context)
context.addServlet("org.eclipse.jetty.servlet.DefaultServlet", "/")
val connector: SelectChannelConnector = getConnector(8991)
val connList: List[Connector] = List(connector)
server.setConnectors(connList.toArray);
server
}

private def getContext: WebAppContext = {
val context = new WebAppContext()
context.setContextPath("/")
context.setDescriptor("src/main/webapp/WEB-INF/web.xml")
context.setResourceBase("src/main/webapp")
context.setParentLoaderPriority(true)
context
}

private def getConnector(port: Int): SelectChannelConnector = {
val connector: SelectChannelConnector = new SelectChannelConnector()
connector.setPort(port)
connector.setConfidentialPort(8443)
connector.setAcceptors(2)
connector.setMaxIdleTime(30000)
connector
}

def urlFor(path: String) = baseUrl + path

lazy val start = {
server.start()
}

lazy val startRemoteServer = {
remoteServer.start()
}

In my test cases, I am using below lines to start jetty:

com.test.api.JettyTestServer.start

com.test.api.JettyTestServer.startRemoteServer

Let me know if I am missing anything in the configuration.

Thanks in advance.

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
Ayush Mishra
  • 567
  • 1
  • 7
  • 19

0 Answers0