2017-10-17 145 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") 
    } 
} 
相关问题