Netty ExceptionInInitializerError

时间:2018-05-24 21:59:05

标签: netty ikvm

我正在进入一个物联网项目并试图理清学习曲线和奇怪的接近角度。我是一名.Net程序员,它似乎是基于Java的Netty。我们的客户已经在研究将它放在他们的设备上并坚持使用HTTP而不是MQTT,因为他们不希望防火墙配置成为正常因素。

所以,我应该让你知道的第一件事就是我试图将DotNetty用作客户端和代理,但是由于它没有文档我无法打结,因此我无法进行库的正面或反面工作。我在对象浏览器中看到的对象是我在Netty文档中看到的对象。然后我想到,也许我可以做一些我在Xamarin中看到的东西,我正在从C#代码中清楚地访问Java库。这导致我进入LKVM,它允许我将Netty jar文件转换为DLL文件。

因此,当我使用Visual Studio和C#时,我现在可以按照Netty教程和文档进行操作,这可以获得相当多的进展。

我写了一个小的控制台应用程序,应该像一个带有Netty客户端的物联网设备。我使用我生成的DLL来编写客户端。我在尝试连接时遇到错误并假设因为我没有经纪人,所以它只是找不到端点。所以今天我写了服务器代码。一旦我完成,我尝试再次连接设备并得到相同的错误。

然后我解雇了Fiddler所以我可以查看流量​​,看到没有。所以错误发生在任何连接尝试之前。

所以,在我再继续之前,这是客户端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JSON = Newtonsoft.Json.JsonConvert;
using io.netty.util;
using io.netty.bootstrap;
using io.netty.buffer;
using io.netty.channel;
using io.netty.channel.nio;
using io.netty.channel.socket;
using io.netty.channel.socket.nio;
using io.netty.handler.codec.http;
using io.netty.handler.codec.http.websocketx;
using io.netty.handler.codec.http.websocketx.extensions.compression;
using io.netty.handler.ssl;
using io.netty.handler.ssl.util;
using java.io;
using java.net;
using io.netty.util.concurrent;

namespace Device
{
    public class WebSocketClient
    {
        public string URL { get; set; }
        public URI Uri = null;
        public Uri netUri = null;
        public string Host;
        public int Port;
        public Channel Channel;
        public Bootstrap BootStrap = new Bootstrap();
        public WebSocketClientHandler Handler;
        public EventLoopGroup Group = new NioEventLoopGroup();
        public SslContext SslCtx = null;

        public WebSocketClient(string URL)
        {
            this.URL = URL;
            netUri = new Uri(this.URL);
            Uri = new URI(URL);
            Host = netUri.Host;
            Port = netUri.Port;//Unlike the Netty example, .Net knows the ws/wss ports.
            bool SSL = Port == 443;


            if (SSL)
            {
                SslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
            }

            Handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(Uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()));
            //BootStrap.group(Group).channel(new NioSocketChannel().GetType().handler(new ChannelInitializer(Handler, Host, Port, SslCtx))).connect(Host, Port).sync().channel();

        }

        public void Connect()
        {
            //Channel = BootStrap.connect(Host, Port).sync().channel();            
            //Channel = BootStrap.connect().sync().channel();
            //((Bootstrap)BootStrap.group(Group).channel(new NioSocketChannel().GetType()).handler(new ChannelInitializer(Handler, Host, Port, SslCtx))).connect(Host, Port).sync().channel();

            BootStrap = BootStrap.group(Group) as Bootstrap;
            BootStrap = BootStrap.channel(new NioSocketChannel().GetType()) as Bootstrap;
            BootStrap = BootStrap.handler(new ChannelInitializer(Handler, Host, Port, SslCtx)) as Bootstrap;            
            //BootStrap.remoteAddress(Host, Port);
            //BootStrap.localAddress(Host, Port);
            ChannelFuture cf = BootStrap.connect(Host, Port);
            cf = cf.sync();
            Channel = cf.channel();

            Handler.HandshakeFuture().sync();
        }

        public void Close()
        {
            Group.shutdownGracefully();
        }

        public void Ping()
        {
            WebSocketFrame frame = new PingWebSocketFrame(Unpooled.wrappedBuffer(new byte[] { 8, 1, 8, 1 }));
            Channel.writeAndFlush(frame);
        }

        public void Send(string content)
        {
            Channel.writeAndFlush(content);
            Channel.newPromise().addListener(new Listener());
        }

    }

    public class Listener : GenericFutureListener
    {
        public void operationComplete(Future f)
        {
            System.Console.WriteLine(JSON.SerializeObject(f.get()));
        }
    }
}

在构造函数中有一条注释行,模仿客户端示例中的内容。因为它是链接的,所以很难分辨出错误的来源。所以,我把它移到我的连接功能中并将其分解。

在连接功能中,在以下行中我看到了第一个出现故障的迹象:

ChannelFuture cf = BootStrap.connect(Host, Port);

如果我看看“手表”中的物体,我可以看到似乎已经存在错误。它看起来像这样:

{DefaultChannelPromise@67755d(failure: java.lang.ExceptionInInitializerError)}

但是,它不会在该行上出错,而​​是出现在以下行:

cf = cf.sync();

堆栈跟踪如下所示:

at io.netty.util.internal.PlatformDependent0.throwException(Exception )
   at io.netty.util.internal.PlatformDependent.throwException(Exception t)
   at io.netty.util.concurrent.DefaultPromise.rethrowIfFailed()
   at io.netty.util.concurrent.DefaultPromise.sync()
   at io.netty.channel.DefaultChannelPromise.sync()
   at io.netty.channel.DefaultChannelPromise.<bridge>sync()
   at io.netty.channel.DefaultChannelPromise.io.netty.channel.ChannelFuture/()Lio.netty.channel.ChannelFuture;sync()
   at Device.WebSocketClient.Connect() in c:\Users\ME\Documents\Visual Studio 2017\Projects\MQTT Sandbox\Device\WebSocketClient.cs:line 70
   at Device.Program.SocketTest() in c:\Users\ME\Documents\Visual Studio 2017\Projects\MQTT Sandbox\Device\Program.cs:line 106
   at Device.Program.Main(String[] args) in c:\Users\ME\Documents\Visual Studio 2017\Projects\MQTT Sandbox\Device\Program.cs:line 72

我唯一的另一个领导是,如果我探索该异常,则异常的子异常(与内部异常相反)会出现:“未知指针大小”。堆栈跟踪如下所示:

enter image description here

我今天花了大量时间进行修补和研究,而且我已经找不到任何关于错误的线索。任何帮助将不胜感激。

提前感谢您的帮助!

更新 我能够从ChannelFuture cf = BootStrap.connect(Host,Port)中提取更好的堆栈跟踪;

May 29, 2018 8:25:37 AM io.netty.channel.DefaultChannelId defaultProcessId
WARNING: Failed to find the current process ID from ''; using a random value: -251539282
May 29, 2018 8:25:43 AM io.netty.channel.AbstractChannel$AbstractUnsafe register
WARNING: Force-closing a channel whose registration task was not accepted by an event loop: [id: 0x6312b48a]
java.lang.ExceptionInInitializerError
        at io.netty.util.internal.shaded.org.jctools.queues.LinkedArrayQueueUtil.modifiedCalcElementOffset(LinkedArrayQueueUtil.java:24)
        at io.netty.util.internal.shaded.org.jctools.queues.BaseMpscLinkedArrayQueue.offer(BaseMpscLinkedArrayQueue.java:308)
        at io.netty.util.concurrent.SingleThreadEventExecutor.offerTask(SingleThreadEventExecutor.java:330)
        at io.netty.util.concurrent.SingleThreadEventExecutor.addTask(SingleThreadEventExecutor.java:321)
        at io.netty.util.concurrent.SingleThreadEventExecutor.execute(SingleThreadEventExecutor.java:765)
        at io.netty.channel.AbstractChannel$AbstractUnsafe.register(AbstractChannel.java:479)
        at io.netty.channel.SingleThreadEventLoop.register(SingleThreadEventLoop.java:81)
        at io.netty.channel.SingleThreadEventLoop.register(SingleThreadEventLoop.java:74)
        at io.netty.channel.MultithreadEventLoopGroup.register(MultithreadEventLoopGroup.java:86)
        at io.netty.bootstrap.AbstractBootstrap.initAndRegister(AbstractBootstrap.java:333)
        at io.netty.bootstrap.Bootstrap.doResolveAndConnect(Bootstrap.java:163)
        at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:145)
        at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:126)
        at cli.Device.WebSocketClient.Connect(WebSocketClient.cs:70)
        at cli.Device.Program.SocketTest(Program.cs:106)
        at cli.Device.Program.Main(Program.cs:72)
Caused by: java.lang.IllegalStateException: Unknown pointer size
 at io.netty.util.internal.shaded.org.jctools.util.UnsafeRefArrayAccess.<clinit>(UnsafeRefArrayAccess.java:51)
        ... 11 more

May 29, 2018 8:25:43 AM io.netty.util.concurrent.DefaultPromise safeExecute
SEVERE: Failed to submit a listener notification task. Event loop shut down?
java.lang.NoClassDefFoundError: Could not initialize class io.netty.util.internal.shaded.org.jctools.util.UnsafeRefArrayAccess
        at io.netty.util.internal.shaded.org.jctools.queues.LinkedArrayQueueUtil.modifiedCalcElementOffset(LinkedArrayQueueUtil.java:24)
        at io.netty.util.internal.shaded.org.jctools.queues.BaseMpscLinkedArrayQueue.offer(BaseMpscLinkedArrayQueue.java:308)
        at io.netty.util.concurrent.SingleThreadEventExecutor.offerTask(SingleThreadEventExecutor.java:330)
        at io.netty.util.concurrent.SingleThreadEventExecutor.addTask(SingleThreadEventExecutor.java:321)
        at io.netty.util.concurrent.SingleThreadEventExecutor.execute(SingleThreadEventExecutor.java:765)
        at io.netty.util.concurrent.DefaultPromise.safeExecute(DefaultPromise.java:764)
        at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:438)
        at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:104)
        at io.netty.channel.DefaultChannelPromise.trySuccess(DefaultChannelPromise.java:84)
        at io.netty.channel.AbstractChannel$CloseFuture.setClosed(AbstractChannel.java:1148)
        at io.netty.channel.AbstractChannel$AbstractUnsafe.register(AbstractChannel.java:491)
        at io.netty.channel.SingleThreadEventLoop.register(SingleThreadEventLoop.java:81)
        at io.netty.channel.SingleThreadEventLoop.register(SingleThreadEventLoop.java:74)
        at io.netty.channel.MultithreadEventLoopGroup.register(MultithreadEventLoopGroup.java:86)
        at io.netty.bootstrap.AbstractBootstrap.initAndRegister(AbstractBootstrap.java:333)
        at io.netty.bootstrap.Bootstrap.doResolveAndConnect(Bootstrap.java:163)
        at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:145)
        at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:126)
        at cli.Device.WebSocketClient.Connect(WebSocketClient.cs:70)
        at cli.Device.Program.SocketTest(Program.cs:106)
        at cli.Device.Program.Main(Program.cs:72)

1 个答案:

答案 0 :(得分:1)

我在IKVM的分支中找到了解决方案并实施了修复程序。 https://github.com/Draspax/ikvm8

基本上,在io.netty.util.internal.shaded.org.jctools.util.UnsafeRefArrayAccess类中,有一个对UnsafeAccess.UNSAFE.arrayIndexScale(Object []。class)的调用。应该返回4或8,并且IKVM Unsafe实现不考虑Object []。class,因此它返回默认值1。我已经添加了对象数组作为arrayIndexScale返回4的选项。

希望这些家伙能够使IKVM保持生命状态,它们很快就会被包含在https://github.com/wwrd/ikvm8中。