0

我有一种情况,我必须通过多个 JVM 测试远程功能。我正在尝试使用两个不同的端口运行 JettyTestServer。但是在第二个端口上运行 Jetty 时我遇到了错误。

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

但是,我的测试用例成功通过了。远程功能工作正常。

这是我的代码:-

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()
}

在我的测试用例中,我使用以下几行来启动码头:

com.test.api.JettyTestServer.start

com.test.api.JettyTestServer.startRemoteServer

如果我在配置中遗漏任何内容,请告诉我。

提前致谢。

4

0 回答 0