ServerSocketChannelImpl ClassCastException从服务器写入客户端Java Grizzly

时间:2014-12-27 22:06:48

标签: java server grizzly

当使用TCPNIOServerConnection在FilterChain外部“写”到指定的客户端时,我总是得到以下异常:

INFORMATION: Write to /127.0.0.1:50559
Dez 27, 2014 10:57:11 PM org.glassfish.grizzly.filterchain.DefaultFilterChain execute
WARNUNG: GRIZZLY0013: Exception during FilterChain execution
java.lang.ClassCastException: sun.nio.ch.ServerSocketChannelImpl cannot be cast to java.nio.channels.SocketChannel
    at org.glassfish.grizzly.nio.transport.TCPNIOUtils.writeSimpleBuffer(TCPNIOUtils.java:108)
    at org.glassfish.grizzly.nio.transport.TCPNIOAsyncQueueWriter.write0(TCPNIOAsyncQueueWriter.java:126)
    at org.glassfish.grizzly.nio.transport.TCPNIOAsyncQueueWriter.write0(TCPNIOAsyncQueueWriter.java:106)
    at org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter.write(AbstractNIOAsyncQueueWriter.java:260)
    at org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter.write(AbstractNIOAsyncQueueWriter.java:169)
    at org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter.write(AbstractNIOAsyncQueueWriter.java:71)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransportFilter.handleWrite(TCPNIOTransportFilter.java:126)
    at org.glassfish.grizzly.filterchain.TransportFilter.handleWrite(TransportFilter.java:191)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$8.execute(ExecutorResolver.java:111)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.write(DefaultFilterChain.java:413)
    at org.glassfish.grizzly.nio.NIOConnection.write(NIOConnection.java:407)
    at org.glassfish.grizzly.nio.NIOConnection.write(NIOConnection.java:395)
    at net.vistahr.moose.server.connection.push.ServerConnection.writeTo(ServerConnection.java:58)
    at net.vistahr.moose.server.connection.push.ServerConnection.broadcast(ServerConnection.java:46)
    at net.vistahr.moose.server.service.auth.AuthService.startServerPush(AuthService.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at net.vistahr.moose.server.filter.DelegateServiceFilter.handleRead(DelegateServiceFilter.java:43)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)

使用FilterChainContext将消息写入指定的客户端没有问题,但只有每次都会出现Connection the Exception。

TCPNIOServerConnection connection = getTransport().bind(host, port);
getTransport().start();
connection( destinationAddress, pushMessage, null );

有没有其他方式与客户沟通(在FilterChain之外)?

1 个答案:

答案 0 :(得分:0)

您无法写入服务器端TCPNIOServerConnection,您只能写入客户端TCPNIOConnection。 例如:

Connection c = transport.connect("www.google.com", "80");
c.write(....);