2017-10-17 180 views
0

我試圖在Apache Ignite服務中嵌入Jetty服務器(根據this thread),所以我可以使HTTP端點成爲我的數據管道的入口點。這裏是我的基本測試:如何在Apache Ignite服務中嵌入Jetty服務器?

Main.scala

object Main { 
    def main(args: Array[String]): Unit = { 
     val ignite = Ignition.start() 
     val group = ignite.cluster.forLocal 
     val services = ignite.services(group) 
     services.deployNodeSingleton("myTestService", new TestServiceImpl) 
    } 
} 

TestService.scala

trait TestService { 
    def test() 
} 

class TestServiceImpl extends Service with TestService { 
    val server = new Server(8090) 

    def cancel(ctx: ServiceContext) = { 
     server.stop() 
     server.join() 
    } 

    def init(ctx: ServiceContext) = { 
     println("TestServiceImpl#init") 
    } 

    def execute(ctx: ServiceContext) = { 
     println("TestServiceImpl#execute") 
     server.start() 
    } 

    def test() = { 
     println("Tested") 
    } 
} 

當我運行它,我得到以下錯誤:

[01:52:57] Ignite node started OK (id=626c1302) 
[01:52:57] Topology snapshot [ver=1, servers=1, clients=0, CPUs=8, heap=2.0GB] 
TestServiceImpl#init 
TestServiceImpl#execute 
Oct 17, 2017 1:52:57 AM org.apache.ignite.logger.java.JavaLogger error 
SEVERE: Service execution stopped with error [name=myTestService, execId=565f4fb4-5726-4c37-857d-0c74f3b334ce] 
java.util.concurrent.RejectedExecutionException: [email protected] 
    at org.eclipse.jetty.util.thread.QueuedThreadPool.execute(QueuedThreadPool.java:362) 
    at org.eclipse.jetty.io.SelectorManager.execute(SelectorManager.java:160) 
    at org.eclipse.jetty.io.SelectorManager.doStart(SelectorManager.java:258) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) 
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132) 
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106) 
    at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:256) 
    at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:81) 
    at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) 
    at org.eclipse.jetty.server.Server.doStart(Server.java:366) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) 
    at me.danellis.ignite.TestServiceImpl.execute(TestService.scala:23) 
    at org.apache.ignite.internal.processors.service.GridServiceProcessor$2.run(GridServiceProcessor.java:1160) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:748) 

有什麼需要的在Ignite或Jetty中配置不同,以使其工作?

回答

2

您應該實例Jetty服務器類的init方法,因爲它將在服務部署之後立即在目標機器上調用。在構造函數中實例化Server類是無用的 - 在創建Service實例(在大多數情況下可以在其他節點上執行)後,該實例將被序列化並添加到內部緩存中,並且只有在目標計算機上啓動後。

我認爲很明顯jetty Server對象無法正確序列化。例如,不能完成ThreadPool的序列化,因爲Thread實現包含本地代碼塊。

+0

啊,對。我甚至沒有想過序列化,因爲我現在正在本地開發。 – Derecho

0

我剛剛弄明白了,但我會留下來讓其他人有同樣的問題。

原來你不能在構造函數中實例化Server。你必須init的方法。 (不知道爲什麼,雖然,也許是線程池未設置構造尚未運行時。)

TestService.scala

class TestServiceImpl extends Service with TestService { 
    var server: Server = _ 

    def cancel(ctx: ServiceContext) = { 
     server.stop() 
     server.join() 
    } 

    def init(ctx: ServiceContext) = { 
     println("TestServiceImpl#init") 
     server = new Server(8090) 
    } 

    def execute(ctx: ServiceContext) = { 
     println("TestServiceImpl#execute") 
     server.start() 
    } 

    def test() = { 
     println("Tested") 
    } 
} 
相關問題