protected override void ProcessRecord() { string[] chunks = Context.ChunkPath(this.SessionState.Path.CurrentLocation); if (chunks.Length > 1) { this.WriteError( new ErrorRecord( new InvalidOperationException( "Messages can only be sent for queues and topics, make sure you are in that location"), "0", ErrorCategory.InvalidOperation, this.SessionState.Path.CurrentLocation.Path)); } else { MessageSender sender = this.Factory.CreateMessageSender(this.To); try { sender.Send(new BrokeredMessage(this.Message)); } finally { sender.Close(); } } }
private void Scenario11_QueueReceiver(object obj) { string queue = (string)obj; ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(this.ConnectionString); builder.TransportType = TransportType.Amqp; MessagingFactory factory = MessagingFactory.CreateFromConnectionString(this.ConnectionString); QueueClient client = factory.CreateQueueClient(queue); while (true) { BrokeredMessage request = client.Receive(); request.Complete(); MessageSender sender = factory.CreateMessageSender(request.ReplyTo); MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("Response")); BrokeredMessage response = new BrokeredMessage(stream); response.CorrelationId = request.MessageId; response.Properties["time"] = DateTime.UtcNow; sender.Send(response); sender.Close(); } }
public void Scenario10_QueueRequestResponse(string queue, string replyTo) { WorkerThread receiverThread = new WorkerThread(this.Scenario11_QueueReceiver); receiverThread.Start(queue); ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(this.ConnectionString); builder.TransportType = TransportType.Amqp; MessagingFactory factory = MessagingFactory.CreateFromConnectionString(this.ConnectionString); MessageSender sender = factory.CreateMessageSender(queue); MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("Request")); BrokeredMessage message = new BrokeredMessage(stream); message.MessageId = Guid.NewGuid().ToString(); message.ReplyTo = replyTo; message.Properties["time"] = DateTime.UtcNow; sender.Send(message); MessageReceiver receiver = factory.CreateMessageReceiver(replyTo); BrokeredMessage response = receiver.Receive(); response.Complete(); sender.Close(); receiver.Close(); factory.Close(); }
public bool Stop(HostControl hostControl) { watcher.EnableRaisingEvents = false; tokenSource.Cancel(); messagingFactory.Close(); imageSender.Close(); configReciver.Close(); return(true); }
public void SendResponse(BrokeredMessage message) { try { _sender.Send(message); } finally { if (_sender != null) { _sender.Close(); } } }
private async Task EnsureSend(BrokeredMessage message, string path) { Trace(); while (true) { try { //first check if sender exists but for a different destination if (_sender != null && !_sender.Path.Equals(path)) { try { _sender.Close(); } catch (Exception e) { Error(e.ToString(), nameof(EnsureSend)); _sender.Abort(); Info("Called sender abort", nameof(EnsureSend)); } finally { _sender = null; } } //second check if sender is null or closed if (_sender == null || _sender.IsClosed) { Trace("Creating sender", nameof(EnsureSend)); _sender = MessagingFactory.CreateMessageSender(path); } Trace("Trying to send now"); //try to send the message and break from the endless loop await _sender.SendAsync(message); break; } catch (Exception e) { //kill the messenger if it acted up. Error($"There was an exception while trying to send a message: {e.ToString()}"); _sender.Abort(); Info("Called sender abort", nameof(EnsureSend)); _sender = null; } } }
private void Stop( ) { _logger.LogInfo("Service stopping... "); _dataIntakeLoader.StopAll( ); // close web host first (message intake) if (_webHost != null) { _webHost.Close( ); _webHost = null; } // shutdown processor (message processing) _batchSenderThread.Stop(STOP_TIMEOUT_MS); // shut down connection to event hub last if (_MessageSender != null) { _MessageSender.Close( ); } _logger.LogInfo("...stopped"); }
private static async Task <SendResult> DoQueueSendOperation(string connectionString, string path, TimeSpan timeout, bool useCached, Func <MessageSender, Task <SendResult> > operation) { MessagingFactory mfactory = null; MessageSender requestClient = null; try { if (useCached) { requestClient = ServiceBusMessagingFactory.Instance.GetMessageSender(connectionString, path, timeout); } else { mfactory = ServiceBusMessagingFactory.CreateMessagingFactoryWithTimeout(connectionString, timeout); requestClient = mfactory.CreateMessageSender(path); } return(await operation.Invoke(requestClient).ConfigureAwait(false)); } finally { requestClient?.Close(); mfactory?.Close(); } }
void ICachedMessagingEntity.Close() => _implementation.Close();
protected override void OnStopped(bool isForced) { workerQueueClient.Close(); deciderSender.Close(); messagingFactory.Close(); }
public static void Main() { string serviceBusConnectionString; // Read user credentials Console.Write("Please provide a connection string to Service Bus (/? for help):\n "); serviceBusConnectionString = Console.ReadLine(); if ((String.Compare(serviceBusConnectionString, "/?") == 0) || (serviceBusConnectionString.Length == 0)) { Console.Write("To connect to the Service Bus cloud service, go to the Windows Azure portal and select 'View Connection String'.\n"); Console.Write("To connect to the Service Bus for Windows Server, use the get-sbClientConfiguration PowerShell cmdlet.\n\n"); Console.Write("A Service Bus connection string has the following format: \nEndpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=<keyName>;SharedAccessKey=<key>"); serviceBusConnectionString = Console.ReadLine(); Environment.Exit(0); } // Using a ServiceBusNamespaceClient, create a queue for incoming messages and a queue for outgoing ones. NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(serviceBusConnectionString); // Create a queue with a relatively short PeekLock timeout Console.WriteLine("Creating Queues..."); if (namespaceManager.QueueExists(QueueName)) { namespaceManager.DeleteQueue(QueueName); } QueueDescription queueDescription = namespaceManager.CreateQueue(new QueueDescription(QueueName) { LockDuration = TimeSpan.FromSeconds(15) }); // Create a MessagingFactory to send and receive messages MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(serviceBusConnectionString); // Create communication objects to send and receive on the queue MessageSender sender = messagingFactory.CreateMessageSender(queueDescription.Path); MessageReceiver receiver = messagingFactory.CreateMessageReceiver(queueDescription.Path, ReceiveMode.PeekLock); //------------------------------------------------------------------------------------- // 1: Send/Complete in a Transaction and Complete Console.WriteLine("\nScenario 1: Send/Complete in a Transaction and then Complete"); //------------------------------------------------------------------------------------- SendAndCompleteInTransactionAndCommit(sender, receiver); Console.WriteLine(); Console.WriteLine("Press [Enter] to move to the next scenario."); Console.ReadLine(); //------------------------------------------------------------------------------------- // 2: Send/Complete in a Transaction and Abort Console.WriteLine("\nScenario 2: Send/Complete in a Transaction and do not Complete"); //------------------------------------------------------------------------------------- SendAndCompleteInTransactionAndRollback(sender, receiver); Console.WriteLine(); Console.WriteLine("Press [Enter] to exit."); Console.ReadLine(); // Cleanup: receiver.Close(); sender.Close(); messagingFactory.Close(); namespaceManager.DeleteQueue(QueueName); }
public void Dispose() { _messageSender.Close(); }