Rabbitmq Server向我提供了消息,“访问端的心跳”。
RabbitMQ服务器正在向我提供消息:客户端错过心跳。我知道,当客户端停止将心跳发送到服务器时,然后RabbitMQ服务器关闭连接时,我知道此消息。 问题是我在 localhost 中使用RabbitMQ,因此我认为这与网络阻止无关。我的客户端使用EasyNetq(.NET 4.6.1 /组件版本:6.3.1)组件,它应该本身就可以处理此心跳。
为什么客户即使我在Localhost中使用RabbitMQ也不会发送心跳?
Bellow我代码的示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyNetQ;
using EasyNetQ.Topology;
using InteraxaFramework.Genesys.Common;
using log4net;
namespace Bridge.Genesys.Services
{
public class RabbitProducer
{
private string _hostName;
private int _port;
private string _userName;
private string _password;
private IConnection _connection;
private IModel _channel;
private int _timeout;
private IBus bus;
private MessageProperties _msgProperties;
private Dictionary<string, Exchange> _exchages;
public RabbitProducer(string hostName, int port, string userName, string password, int timeout = 60)
{
_hostName = hostName;
_port = port;
_userName = userName;
_password = password;
_timeout = timeout;
createConnection();
_msgProperties = new MessageProperties();
_exchages = new Dictionary<string, Exchange>();
}
private Exchange GetExchange(string exchange)
{
if (!_exchages.ContainsKey(exchange))
{
_exchages[exchange] = new Exchange(exchange);
}
return _exchages[exchange];
}
private void createConnection()
{
if (bus == null)
{
bus = RabbitHutch.CreateBus($"host={_hostName}:{_port};username={_userName};password={_password};timeout={_timeout}");
}
}
public async Task PublishExchangeAsync(string exchange, byte[] body)
{
await bus.Advanced.PublishAsync(
GetExchange(exchange),
string.Empty,
false,
_msgProperties,
body);
}
public void Disconnect()
{
if (_exchages != null)
{
_exchages.Clear();
}
if (bus != null)
{
bus.Dispose();
}
}
}
}
我的代码的其他部分将此类用作Singleton。该程序是一个Windows服务,它始终保持运行,并且在服务寿命期间仅使用一个连接和一个通道。
Singleton对象的创建:
this.rabbitProducer = new RabbitProducer("localhost", 5672, "guest", "guest", 60);
发布者的利用:
var bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, JsonObj);
var bytes = ms.ToArray();
var bodyBytes = bytes;
await rabbitProducer.PublishExchangeAsync(queueName, bodyBytes);
}
The RabbitMQ Server is giving me the message: Missed heartbeats from client. I know this message occour when the cliente stop sending the heartbeat to server, then RabbitMq server close the connection.
The problem is that I'm using RabbitMq in localhost, so I think it isn't about network blocking. My client uses the EasyNetQ(.Net 4.6.1 / Component Version: 6.3.1) component and it should had handled this heartbeat by itself.
Why the client wouldn't send the heartbeat even I'm using RabbitMq in localhost?
Bellow an example from my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyNetQ;
using EasyNetQ.Topology;
using InteraxaFramework.Genesys.Common;
using log4net;
namespace Bridge.Genesys.Services
{
public class RabbitProducer
{
private string _hostName;
private int _port;
private string _userName;
private string _password;
private IConnection _connection;
private IModel _channel;
private int _timeout;
private IBus bus;
private MessageProperties _msgProperties;
private Dictionary<string, Exchange> _exchages;
public RabbitProducer(string hostName, int port, string userName, string password, int timeout = 60)
{
_hostName = hostName;
_port = port;
_userName = userName;
_password = password;
_timeout = timeout;
createConnection();
_msgProperties = new MessageProperties();
_exchages = new Dictionary<string, Exchange>();
}
private Exchange GetExchange(string exchange)
{
if (!_exchages.ContainsKey(exchange))
{
_exchages[exchange] = new Exchange(exchange);
}
return _exchages[exchange];
}
private void createConnection()
{
if (bus == null)
{
bus = RabbitHutch.CreateBus(quot;host={_hostName}:{_port};username={_userName};password={_password};timeout={_timeout}");
}
}
public async Task PublishExchangeAsync(string exchange, byte[] body)
{
await bus.Advanced.PublishAsync(
GetExchange(exchange),
string.Empty,
false,
_msgProperties,
body);
}
public void Disconnect()
{
if (_exchages != null)
{
_exchages.Clear();
}
if (bus != null)
{
bus.Dispose();
}
}
}
}
Other parts of my code uses this class as singleton. The program is a windows service that keeps always running and uses just one connection and one channel during the service lifetime.
The creation of singleton object:
this.rabbitProducer = new RabbitProducer("localhost", 5672, "guest", "guest", 60);
The utilization of the publisher:
var bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, JsonObj);
var bytes = ms.ToArray();
var bodyBytes = bytes;
await rabbitProducer.PublishExchangeAsync(queueName, bodyBytes);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论