/// <summary> /// Convert HostOption & ExchangeOption & QueueOption to MessageQueueOption /// </summary> /// <param name="option"></param> /// <returns></returns> protected (HostOption, ExchangeOption, QueueOption) ConvertOption(MessageQueueOption option) { var hostOption = new HostOption { ClientName = option.ClientName, Host = option.Host, Port = option.Port, VirtualHost = option.VirtualHost, UserName = option.UserName, Password = option.Password }; var exchangeOption = new ExchangeOption { ExchangeName = option.ExchangeName, ExchangeType = option.ExchangeType, IsAutoDeleted = option.ExchangeAutoDelete, IsDurable = option.ExchangeDurable, RoutingKey = option.RoutingKey }; var queueOption = new QueueOption { ConsumerTag = option.CustomerTag, IsAutoDeleted = option.QueueAutoDelete, IsDurable = option.QueueDurable, IsExclusive = option.QueueExclusive, QueueName = option.QueueName, AutoAck = option.QueueAutoAck }; return(hostOption, exchangeOption, queueOption); }
public void Start(MessageQueueOption option) { var hostOption = new HostOption { ClientName = option.ClientName, Host = option.Host, Port = option.Port, VirtualHost = option.VirtualHost, UserName = option.UserName, Password = option.Password }; var exchangeOption = new ExchangeOption { ExchangeName = option.ExchangeName, ExchangeType = option.ExchangeType, IsAutoDeleted = option.ExchangeAutoDelete, IsDurable = option.ExchangeDurable, RoutingKey = option.RoutingKey }; var queueOption = new QueueOption { QueueName = option.QueueName, IsDurable = option.QueueDurable, IsAutoDeleted = option.QueueAutoDelete, IsExclusive = option.QueueExclusive, }; Start(hostOption, exchangeOption); }
/// <summary> /// Start publisher /// </summary> /// <param name="hostOption"></param> /// <param name="exchangeOption"></param> public void Start(HostOption hostOption, ExchangeOption exchangeOption, QueueOption queueOption) { mConnectionFactory = new ConnectionFactory() { ClientProvidedName = hostOption.ClientName, HostName = hostOption.Host, Port = hostOption.Port, VirtualHost = hostOption.VirtualHost, UserName = hostOption.UserName, Password = hostOption.Password, RequestedConnectionTimeout = TimeSpan.FromSeconds(hostOption.ConnectionTimeout), RequestedHeartbeat = TimeSpan.FromSeconds(hostOption.HeartBeat) }; mExchangeName = exchangeOption.ExchangeName; mExchangeType = exchangeOption.ExchangeType; mRoutingKey = exchangeOption.RoutingKey; mConnection = mConnectionFactory.CreateConnection(); mChannel = mConnection.CreateModel(); if (queueOption != null && !string.IsNullOrWhiteSpace(queueOption.QueueName)) { mQueueName = queueOption.QueueName; mQueueDurable = queueOption.IsDurable; mQueueAutoDelete = queueOption.IsAutoDeleted; mQueueExclusive = queueOption.IsExclusive; mChannel.QueueDeclare(mQueueName, mQueueDurable, mQueueExclusive, mQueueAutoDelete); } mChannel.ExchangeDeclare(mExchangeName, mExchangeType); }
public QueueReceiver(QueueOption queueOption, string queueName, ILogger <QueueReceiver <T> > logger) { queueOption.VerifyNotNull(nameof(queueOption)); queueName.VerifyNotEmpty(nameof(queueName)); _messageReceiver = new MessageReceiver(queueOption.ToConnectionString(), queueName, ReceiveMode.PeekLock); _logger = logger; }
public static void Verify(this QueueOption subject) { subject.VerifyNotNull(nameof(subject)); subject.Namespace.VerifyNotEmpty(nameof(subject.Namespace)); subject.QueueName.VerifyNotEmpty(nameof(subject.QueueName)); subject.KeyName.VerifyNotEmpty(nameof(subject.KeyName)); subject.AccessKey.VerifyNotEmpty(nameof(subject.AccessKey)); }
public QueueAdmin(QueueOption queueOption, ILogger <QueueAdmin> logging) { queueOption.VerifyNotNull(nameof(queueOption)); logging.VerifyNotNull(nameof(logging)); ConnectionString = queueOption.ToConnectionString(); _managementClient = new ManagementClient(ConnectionString); _logging = logging; }
public static string ToConnectionString(this QueueOption subject) { return(new ServiceBusConnectionStringBuilder { Endpoint = $"{subject.Namespace}.servicebus.windows.net", SasKeyName = subject.KeyName, SasKey = subject.AccessKey, TransportType = TransportType.Amqp, }.ToString()); }
/// <summary> /// Creates a new FIFO queue /// </summary> /// <param name="limit">The maximum number of items allocated in the queue</param> /// <param name="collection">Queue is filled with items from the given collection. All the items from the given collection will /// be initially added to the queue, even if there are more then the specified limit. However, the next time an item is enqueued, /// the excess items will be removed until the queue is back within it's size limit.</param> /// <param name="queueOption">The options associated with the queue <see cref="QueueOption"/></param> public QueueFifoConcurrent(int limit, System.Collections.Generic.IEnumerable <TItem>?collection, QueueOption queueOption = QueueOption.None) { if (limit <= 0) { throw new ArgumentOutOfRangeException(nameof(limit), limit, $"Must be >= 0"); } _queue = collection == null ? new ConcurrentQueue <TItem>() : new ConcurrentQueue <TItem>(collection); LimitSize = limit; Option = queueOption; }
public QueueClient(QueueOption queueOption, IQueueAwaiterService awaiterService, ILogger <QueueClient> logger) { queueOption.VerifyNotNull(nameof(queueOption)); awaiterService.VerifyNotNull(nameof(awaiterService)); logger.VerifyNotNull(nameof(logger)); _awaiterService = awaiterService; _logger = logger; _messageSender = new MessageSender(queueOption.ToConnectionString(), queueOption.Queue); }
public QueueClient(Func <T, Guid?> getId, QueueOption queueOption, IAwaiterCollection <T> awaiterService, ILogger <QueueClient <T> > logger) { getId.VerifyNotNull(nameof(getId)); queueOption.VerifyNotNull(nameof(queueOption)); awaiterService.VerifyNotNull(nameof(awaiterService)); logger.VerifyNotNull(nameof(logger)); _getId = getId; _awaiterService = awaiterService; _logger = logger; _messageSender = new MessageSender(queueOption.ToConnectionString(), queueOption.QueueName); }
/// <summary> /// Start consume /// </summary> /// <param name="hostOption"></param> /// <param name="exchangeOption"></param> /// <param name="queueOption"></param> public void Start(HostOption hostOption, ExchangeOption exchangeOption, QueueOption queueOption) { BindQueue(hostOption, exchangeOption, queueOption); //consuming by event mEventConsumer = new EventingBasicConsumer(mChannel); //registe events mEventConsumer.Received += Consumer_Received; mEventConsumer.Registered += Consumer_Registered; mEventConsumer.Shutdown += Consumer_Shutdown; mLastConsumerTag = mChannel.BasicConsume(mQueueName, mIsAutoAck, mLastConsumerTag, mEventConsumer); }
/// <summary> /// BindQueue /// </summary> /// <param name="hostOption"></param> /// <param name="exchangeOption"></param> /// <param name="queueOption"></param> protected void BindQueue(HostOption hostOption, ExchangeOption exchangeOption, QueueOption queueOption) { mHostOption = hostOption; mExchangeOption = exchangeOption; mQueueOption = queueOption; mQueueName = queueOption.QueueName; mIsAutoAck = queueOption.AutoAck; mLastConsumerTag = queueOption.ConsumerTag; if (string.IsNullOrWhiteSpace(mLastConsumerTag)) { mLastConsumerTag = string.Empty; } try { var factory = new ConnectionFactory() { ClientProvidedName = hostOption.ClientName, HostName = hostOption.Host, Port = hostOption.Port, VirtualHost = hostOption.VirtualHost, UserName = hostOption.UserName, Password = hostOption.Password, RequestedConnectionTimeout = TimeSpan.FromSeconds(hostOption.ConnectionTimeout), RequestedHeartbeat = TimeSpan.FromSeconds(hostOption.HeartBeat) }; mConnection = factory.CreateConnection(); mChannel = mConnection.CreateModel(); mChannel.ExchangeDeclare(exchangeOption.ExchangeName, exchangeOption.ExchangeType); mChannel.QueueDeclare(queueOption.QueueName, queueOption.IsDurable, queueOption.IsExclusive, queueOption.IsAutoDeleted); mChannel.QueueBind(queueOption.QueueName, exchangeOption.ExchangeName, exchangeOption.RoutingKey); mChannel.BasicQos(0, queueOption.Qos, false); } catch (BrokerUnreachableException bex) { ConnectFail?.Invoke(this, new ConnectionFailEventArgs { Message = bex.Message }); } catch (ConnectFailureException cex) { ConnectFail?.Invoke(this, new ConnectionFailEventArgs { Message = cex.Message }); } catch (SocketException sex) { ConnectFail?.Invoke(this, new ConnectionFailEventArgs { Message = sex.Message }); } catch (Exception ex) { UnknowError?.Invoke(this, new UnknownExceptionEventArgs { Message = ex.Message }); } }
public override void Visit(QueueOption node) { this.action(node); }
public override void ExplicitVisit(QueueOption fragment) { _fragments.Add(fragment); }
static void Main(string[] args) { IConsumer consumer = new Consumer(new SimpleJsonMessageDecoder()); IConsumer <TestModel> consumerT = new Consumer <TestModel>(new SimpleJsonMessageDecoder()); HostOption hostOption = new HostOption { ClientName = "TestCreamCustardBun", Host = "192.168.1.10", Port = 5672, VirtualHost = "/dev", UserName = "******", Password = "******", }; ExchangeOption exchangeOption = new ExchangeOption { ExchangeName = "temp.exchange", ExchangeType = "direct", IsDurable = false, IsAutoDeleted = false, RoutingKey = "temp.routingkey", }; QueueOption queueOption = new QueueOption { IsDurable = false, IsAutoDeleted = false, IsExclusive = false, QueueName = "temp.queue", ConsumerTag = "", }; MessageQueueOption option = new MessageQueueOption { ClientName = "TestCreamCustardBun", Host = "192.168.1.10", Port = 5672, VirtualHost = "/dev", UserName = "******", Password = "******", ExchangeName = "temp.exchange", ExchangeType = "direct", ExchangeDurable = false, ExchangeAutoDelete = false, RoutingKey = "temp.routingkey", QueueDurable = false, QueueAutoDelete = false, QueueExclusive = false, QueueName = "temp.queue", CustomerTag = "", QueueAutoAck = true }; consumerT.MessageArrived += Consumer_MessageArrivedT; consumerT.Start(option); consumer.MessageArrived += Consumer_MessageArrived; consumer.Start(option); Console.ReadLine(); }
/// <summary> /// Creates a new FIFO queue /// </summary> /// <param name="limit">The maximum number of items allocated in the queue</param> /// <param name="queueOption">The options associated with the queue <see cref="QueueOption"/></param> public QueueFifoConcurrent(int limit, QueueOption queueOption = QueueOption.None) : this(limit, null, queueOption) { }