在OSGi中从Restlet启动Jetty

我在OSGi(equinox)中使用Restlet。 我一直遇到一个java.io.IOException: The thread blocked at the cyclic barrier has timed out. 例外与默认的HTTP服务器,所以我试图使用Jetty代替。 从阅读各种论坛和文章看来,使用Jetty的正确方法是简单地将相应的JAR添加到类路径中,而Restlet完成艰苦的工作。 这似乎不工作:(

我的bundle激活器的start方法如下所示:

public void start(BundleContext bundleContext) throws Exception {

    List<ConnectorHelper<Server>> servers = Engine.getInstance().getRegisteredServers();
    System.out.println("Server connectors - "+servers.size());
    for (ConnectorHelper<Server> connectorHelper : servers) {
        System.out.println("connector = "+connectorHelper.getClass());
    }

    component = new Component();
    component.getServers().add(Protocol.HTTP, 8182);

    final Application app = new MyApplication();

    component.getDefaultHost().attachDefault(app);
    component.start();
}

运行此打印:

Server connectors - 2
connector = class org.restlet.engine.local.RiapServerHelper
connector = class org.restlet.engine.connector.HttpServerHelper

所以它没有检测到Jetty,这让我认为org.restlet.ext.jetty插件不在类路径中。 但是,在同一个方法中,我可以访问Jetty helper类。 我已经尝试以编程方式启动码头:

    Server server = new Server(Protocol.HTTP, 8184, new MyApplication());
    JettyServerHelper jetty = new HttpServerHelper(server); 
    jetty.start();

其中显示Jetty位于类路径中,并且启动了Jetty,但是当我向http:// localhost:8184发出请求时,出现以下异常:

2015-01-28 13:44:57.182:INFO:oejs.Server:jetty-8.1.14.v20131031
2015-01-28 13:44:57.217:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8184
Jan 28, 2015 1:45:13 PM org.restlet.Restlet handle
WARNING: Unable to start the Restlet
java.net.BindException: Address already in use
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:444)
at sun.nio.ch.Net.bind(Net.java:436)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:67)
at org.restlet.engine.connector.ServerConnectionHelper.createServerSocketChannel(ServerConnectionHelper.java:158)
at org.restlet.engine.connector.ServerConnectionHelper.start(ServerConnectionHelper.java:320)
at org.restlet.Server.start(Server.java:590)
at org.restlet.Restlet.handle(Restlet.java:315)
at org.restlet.Server.handle(Server.java:513)
at org.restlet.engine.ServerHelper.handle(ServerHelper.java:72)
at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:152)
at org.restlet.ext.jetty.JettyServerHelper$WrappedServer.handle(JettyServerHelper.java:170)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:971)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1033)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:644)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:667)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:744)

有谁知道我配置不正确?

非常感谢,吉姆

编辑:正在运行的捆绑包列表:

id  State       Bundle
0   ACTIVE      org.eclipse.osgi_3.9.1.v20140110-1610
            Fragments=156, 157
2   ACTIVE      org.eclipse.equinox.common_3.6.200.v20130402-1505
40  RESOLVED    org.eclipse.core.runtime.compatibility.registry_3.5.200.v20130514-1256
            Master=118
53  ACTIVE      org.eclipse.core.runtime.compatibility.auth_3.2.300.v20120523-2004
65  ACTIVE      javax.inject_1.0.0.v20091030
95  ACTIVE      org.eclipse.core.jobs_3.5.300.v20130429-1813
97  ACTIVE      org.eclipse.equinox.app_1.3.100.v20130327-1442
101 ACTIVE      org.eclipse.equinox.preferences_3.5.100.v20130422-1538
118 ACTIVE      org.eclipse.equinox.registry_3.5.301.v20130717-1549
            Fragments=40
119 ACTIVE      org.eclipse.osgi.services_3.3.100.v20130513-1956
124 ACTIVE      org.eclipse.core.runtime_3.9.100.v20131218-1515
126 ACTIVE      javax.xml_1.3.4.v201005080400
127 ACTIVE      javax.servlet_3.0.0.v201112011016
130 ACTIVE      org.eclipse.core.contenttype_3.4.200.v20130326-1255
134 ACTIVE      javax.annotation_1.1.0.v201209060031
156 RESOLVED    org.eclipse.equinox.weaving.hook_1.0.200.v20130327-1442
            Master=0
157 RESOLVED    org.eclipse.equinox.transforms.hook_1.0.401.v20130327-1442
            Master=0
160 ACTIVE      org.restlet.ext.ssl_2.1.4.v20130907-1635
161 ACTIVE      org.restlet.ext.jetty_2.1.4.v20130907-1635
162 ACTIVE      jsslutils_1.0.5
163 ACTIVE      org.restlet_1.0.0
165 ACTIVE      org.restlet.ext.servlet_2.1.4.v20130907-1635
166 ACTIVE      org.eclipse.equinox.console_1.0.100.v20130429-0953
167 ACTIVE      org.apache.felix.gogo.runtime_0.10.0.v201209301036
169 ACTIVE      org.apache.felix.gogo.shell_0.10.0.v201212101605
170 ACTIVE      org.eclipse.jetty.http_8.1.14.v20131031
171 ACTIVE      org.eclipse.jetty.continuation_8.1.14.v20131031
172 ACTIVE      org.eclipse.jetty.servlet_8.1.14.v20131031
173 ACTIVE      org.eclipse.jetty.util_8.1.14.v20131031
174 ACTIVE      org.eclipse.jetty.security_8.1.14.v20131031
175 ACTIVE      org.eclipse.jetty.io_8.1.14.v20131031
176 ACTIVE      org.eclipse.jetty.server_8.1.14.v20131031
180 ACTIVE      osgi.restlet.jetty.test_1.0.0.qualifier

我的包是osgi.restlet.jetty.test


在OSGi环境中使用Restlet时,有关连接器检测有点棘手。 实际上,您需要确保在Jetty连接器注册到Restlet引擎后启动Restlet服务器。 我看到两种方法:

  • 使用OSGi容器的开始级别来确保在Restlet包( org.restlet.ext.jettyorg.restlet之后启动启动Restlet引擎的bundle。

  • 所有软件包启动时,使用OSGi框架事件启动Restlet引擎。 这种方法的一个例子如下所述:

    public class ServerActivator implements BundleActivator {
        public void start(BundleContext bundleContext) throws Exception {
            this.context.addFrameworkListener(new FrameworkListener() {
                public void frameworkEvent(FrameworkEvent event) {
                    if (event.getType() == FrameworkEvent.STARTED) {
                        Component component = new Component();
                        Server server = new Server(Protocol.HTTP, 8080);
                        server.setName("Sample Server");
                        server.setNext(new SampleApplication());
                        component.getServers().add(server);
                        component.start();
                    }
                }
            });
        }
    }
    
  • 希望能帮助到你。 蒂埃里

    链接地址: http://www.djcxy.com/p/74741.html

    上一篇: Starting Jetty from Restlet in OSGi

    下一篇: Jetty8 FileNotFoundException: No XML configuration