简体   繁体   English

RabbitMQ java.lang.OutOfMemoryError

[英]RabbitMQ java.lang.OutOfMemoryError

Firstly I apologise if this question has been handled elsewhere, I just haven't found something that solved my particular problem.首先,如果这个问题在其他地方得到了处理,我深表歉意,我只是没有找到解决我特定问题的方法。

I have a gateway server which receives msgs externally.我有一个从外部接收消息的网关服务器。 It puts it in a queue that my Order processing server is listening on.它将它放在我的订单处理服务器正在侦听的队列中。 My order processing server listens on 2 queues (in a thread).我的订单处理服务器侦听 2 个队列(在一个线程中)。 Queue 1 - gateway server, Queue 2 - clearing server.队列 1 - 网关服务器,队列 2 - 清算服务器。

So in my order processor, I have worker threads.所以在我的订单处理器中,我有工作线程。 I'm using ExecutorService to manage my threads.我正在使用 ExecutorService 来管理我的线程。 The problem is in the worker thread.问题出在工作线程中。

In the worker thread I make two instance of MQs that I use to publish message to either the clearing server or the gateway server.在工作线程中,我创建了两个 MQ 实例,用于将消息发布到清算服务器或网关服务器。 I basically need to do some processing and then publish that message to these queues.我基本上需要做一些处理,然后将该消息发布到这些队列。

What I want to know is, should I close the channel and connection in my worker thread each time I finish dealing with a message?我想知道的是,每次处理完一条消息时,我是否应该关闭我的工作线程中的通道和连接?

If I don't close the MQ connections on every worker thread after processing a message then after processing 8-900 messages, I start getting following exception intermittently :如果我在处理一条消息后没有关闭每个工作线程上的 MQ 连接,那么在处理 8-900 条消息后,我开始间歇性地收到以下异常:

java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Unknown Source)
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:307)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:516)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:533)
    at interfaces.MQ.<init>(MQ.java:41)
    at orderProcessor.ProcessOrders.<init>(ProcessOrders.java:109)
    at orderProcessor.ProcessIncomingCSThread.spawnThread(ProcessIncomingCSThread.java:52)
    at orderProcessor.ProcessIncomingCSThread.spawnThread(ProcessIncomingCSThread.java:70)
    at orderProcessor.ProcessIncomingCSThread.spawnThread(ProcessIncomingCSThread.java:70)
    at orderProcessor.ProcessIncomingCSThread.spawnThread(ProcessIncomingCSThread.java:70)
    at orderProcessor.ProcessIncomingCSThread.spawnThread(ProcessIncomingCSThread.java:70)
    at orderProcessor.ProcessIncomingCSThread.spawnThread(ProcessIncomingCSThread.java:70)
    at orderProcessor.ProcessIncomingCSThread.routeIncoming(ProcessIncomingCSThread.java:45)
    at interfaces.ProcessIncomingThread.run(ProcessIncomingThread.java:47)

If I do close the connections on every worker thread after processing a msg then after a awhile I get following exception intermittently :]如果我在处理 msg 后确实关闭了每个工作线程上的连接,那么一段时间后我会间歇性地收到以下异常:]

Exception in thread "AMQP Connection 127.0.0.1:5672" java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Unknown Source)
    at com.rabbitmq.client.impl.ChannelManager.scheduleShutdownProcessing(ChannelManager.java:108)
    at com.rabbitmq.client.impl.ChannelManager.handleSignal(ChannelManager.java:94)
    at com.rabbitmq.client.impl.AMQConnection.finishShutdown(AMQConnection.java:696)
    at com.rabbitmq.client.impl.AMQConnection.shutdown(AMQConnection.java:669)
    at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:550)

I am using basicQos when I create a MQ connection for consumption to keep RabbitMQ's internal queue reasonable.我在创建用于消费的 MQ 连接时使用basicQos以保持 RabbitMQ 的内部队列合理。

I create my MQ connections for consumption in following manner :我以下列方式创建我的 MQ 连接以供使用:

_channel.queueDeclare(this._mqName.toString(), true, false, false, null);
_channel.basicConsume(this._mqName.toString(), true, _consumer);
_channel.basicQos(50);

Thanks for looking at this and any suggestions or help would be much appreciated.感谢您查看此内容,任何建议或帮助将不胜感激。 it is more than likely that I'm not doing things correctly for my context..我很可能没有根据我的上下文正确地做事..

Seems like you have a memory leak.好像你有内存泄漏。 Use a profiler.使用分析器。

I'm not familiar with RabbitMQ, but I suspect that either you or RabbitMQ are trying to create more threads that the OS is configured to handle.我不熟悉 RabbitMQ,但我怀疑您或 RabbitMQ 正在尝试创建操作系统配置为处理的更多线程。

Maybe this two links can help you:也许这两个链接可以帮助您:

Thanks for the input guys.感谢您的输入。 I resolved the issue.我解决了这个问题。

I was creating connection in every worker thread.我在每个工作线程中创建连接。 Now I create the connection on the main thread and pass that down to worker threads who create their channels from that connection.现在我在主线程上创建连接并将其传递给从该连接创建通道的工作线程。 this seems to work a treat.这似乎是一种享受。

This however means, I'll have to re-design my MQ class to handle this workflow.然而,这意味着,我将不得不重新设计我的 MQ 类来处理这个工作流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM