private async static Task MessageSenderTest() { // Create MessageSender object var messageSender = await messagingFactory.CreateMessageSenderAsync(QueueName); // Test MessageSender.SendBatchAsync: if the batch size is greater than the max batch size // the method throws a MessageSizeExceededException try { PrintMessage(CallingMessageSenderSendBatchAsync); await messageSender.SendBatchAsync(CreateBrokeredMessageBatch()); PrintMessage(MessageSenderSendBatchAsyncCalled); } catch (Exception ex) { PrintException(ex); } try { // Send the batch using the SendPartitionedBatchAsync method PrintMessage(CallingMessageSenderSendPartitionedBatchAsync); await messageSender.SendPartitionedBatchAsync(CreateBrokeredMessageBatch(), true); PrintMessage(MessageSenderSendPartitionedBatchAsyncCalled); } catch (Exception ex) { PrintException(ex); } }
/// <summary> /// Create a new orchestration of the specified name and version /// </summary> /// <param name="name">Name of the orchestration as specified by the ObjectCreator</param> /// <param name="version">Name of the orchestration as specified by the ObjectCreator</param> /// <param name="instanceId">Instance id for the orchestration to be created, must be unique across the Task Hub</param> /// <param name="input">Input parameter to the specified TaskOrchestration</param> /// <param name="tags">Dictionary of key/value tags associated with this instance</param> /// <returns>OrchestrationInstance that represents the orchestration that was created</returns> public async Task <OrchestrationInstance> CreateOrchestrationInstanceAsync(string name, string version, string instanceId, object input, IDictionary <string, string> tags) { if (string.IsNullOrWhiteSpace(instanceId)) { instanceId = Guid.NewGuid().ToString("N"); } var orchestrationInstance = new OrchestrationInstance { InstanceId = instanceId, ExecutionId = Guid.NewGuid().ToString("N"), }; string serializedInput = defaultConverter.Serialize(input); string serializedtags = tags != null?defaultConverter.Serialize(tags) : null; var startedEvent = new ExecutionStartedEvent(-1, serializedInput) { Tags = serializedtags, Name = name, Version = version, OrchestrationInstance = orchestrationInstance }; var taskMessage = new TaskMessage { OrchestrationInstance = orchestrationInstance, Event = startedEvent }; BrokeredMessage brokeredMessage = Utils.GetBrokeredMessageFromObject(taskMessage, settings.MessageCompressionSettings); brokeredMessage.SessionId = instanceId; MessageSender sender = await messagingFactory.CreateMessageSenderAsync(orchestratorEntityName).ConfigureAwait(false); await sender.SendAsync(brokeredMessage).ConfigureAwait(false); await sender.CloseAsync().ConfigureAwait(false); return(orchestrationInstance); }
public async Task Publish <T>(string queueName, T message) { var sender = _messageSenders.GetOrAdd(queueName, await _messagingFactory.CreateMessageSenderAsync(queueName)); var brokeredMessage = new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)))) { ContentType = "application/json" }; brokeredMessage.Properties.Add("messageType", message.GetType().Name); await sender.SendAsync(brokeredMessage); }
Task <MessageSender> GetSender(MessagingFactory factory, string topicName) { var sender = _senders.GetOrAdd(topicName, factory.CreateMessageSenderAsync); return(sender .ContinueWith(t => { if (t.IsFaulted || t.Result.IsClosed) { sender = factory.CreateMessageSenderAsync(topicName); _senders[topicName] = sender; } return _senders[topicName]; }) .Unwrap()); }
public async Task <bool> SendMessageToQueueAsync(string queueName, string message) { var sender = await _factory.CreateMessageSenderAsync(queueName); var brokeredMessage = new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes(message))) { ContentType = "application/json", Label = "", MessageId = Guid.NewGuid().ToString(), TimeToLive = TimeSpan.FromMinutes(30) }; await sender.SendAsync(brokeredMessage); await sender.CloseAsync(); return(true); }
public async Task <ISendTransport> GetSendTransport(Uri address) { IServiceBusHost host; if (!TryGetMatchingHost(address, out host)) { throw new EndpointNotFoundException("The endpoint address specified an unknown host: " + address); } var queueDescription = address.GetQueueDescription(); var namespaceManager = await host.NamespaceManager.ConfigureAwait(false); string queuePath; var namespacePath = namespaceManager.Address.AbsolutePath.Trim('/'); if (string.IsNullOrEmpty(namespacePath)) { queueDescription = await namespaceManager.CreateQueueSafeAsync(queueDescription).ConfigureAwait(false); queuePath = host.GetQueuePath(queueDescription); } else if (IsInNamespace(queueDescription, namespacePath)) { queueDescription.Path = queueDescription.Path.Replace(namespacePath, "").Trim('/'); queueDescription = await namespaceManager.CreateQueueSafeAsync(queueDescription).ConfigureAwait(false); queuePath = host.GetQueuePath(queueDescription); } else { namespaceManager = await host.RootNamespaceManager.ConfigureAwait(false); queueDescription = await namespaceManager.CreateQueueSafeAsync(queueDescription).ConfigureAwait(false); queuePath = queueDescription.Path; } MessagingFactory messagingFactory = await host.MessagingFactory.ConfigureAwait(false); MessageSender messageSender = await messagingFactory.CreateMessageSenderAsync(queuePath).ConfigureAwait(false); return(new ServiceBusSendTransport(messageSender, host.Supervisor)); }
/// <summary> /// Inserts the item async. /// </summary> /// <returns>The item async.</returns> /// <param name="item">Item.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public async Task SendBrokeredMessageAsync(object item, string gameId = "", string messageLabel = "", int delay = 0) { try { var json = JsonConvert.SerializeObject(item); var message = new BrokeredMessage(json); message.Label = messageLabel; message.Properties["DelayMinutes"] = delay; message.Properties["gameId"] = gameId; message.ScheduledEnqueueTimeUtc = DateTime.UtcNow.AddMinutes(delay); var sender = await _factory.CreateMessageSenderAsync(_queueName); await sender.SendAsync(message); } catch (Exception e) { Console.Error.WriteLine(@"ERROR {0}", e.Message); } }
public async Task <ISendEndpoint> GetSendEndpoint(Uri address) { IServiceBusHost host; if (!TryGetMatchingHost(address, out host)) { throw new EndpointNotFoundException("The endpoint address specified an unknown host: " + address); } TopicDescription topicDescription = await(await host.RootNamespaceManager.ConfigureAwait(false)).CreateTopicSafeAsync(address.GetTopicDescription()).ConfigureAwait(false); MessagingFactory messagingFactory = await host.MessagingFactory.ConfigureAwait(false); MessageSender messageSender = await messagingFactory.CreateMessageSenderAsync(topicDescription.Path).ConfigureAwait(false); var sendTransport = new ServiceBusSendTransport(messageSender); sendTransport.ConnectSendObserver(_sendObservable); return(new SendEndpoint(sendTransport, _serializer, address, _sourceAddress)); }
public async Task <ISendTransport> GetSendTransport(Uri address) { IServiceBusHost host = _hosts.FirstOrDefault( x => address.ToString().StartsWith(x.Settings.ServiceUri.ToString(), StringComparison.OrdinalIgnoreCase)); if (host == null) { throw new EndpointNotFoundException("The endpoint address specified an unknown host: " + address); } var queueDescription = address.GetQueueDescription(); var namespaceManager = await host.NamespaceManager; var namespacePath = namespaceManager.Address.AbsolutePath.Trim('/'); if (IsInNamespace(queueDescription, namespacePath)) { queueDescription.Path = queueDescription.Path.Replace(namespacePath, "").Trim('/'); queueDescription = await namespaceManager.CreateQueueSafeAsync(queueDescription); } else { namespaceManager = await host.RootNamespaceManager; queueDescription = await namespaceManager.CreateQueueSafeAsync(queueDescription); } MessagingFactory messagingFactory = await host.MessagingFactory; string queuePath = host.GetQueuePath(queueDescription); MessageSender messageSender = await messagingFactory.CreateMessageSenderAsync(queuePath); var sendTransport = new ServiceBusSendTransport(messageSender); return(sendTransport); }
public async Task <ISendEndpoint> GetSendEndpoint(Uri address) { IServiceBusHost host = _hosts.FirstOrDefault(x => x.Settings.ServiceUri.Host.Equals(address.Host, StringComparison.OrdinalIgnoreCase)); if (host == null) { throw new EndpointNotFoundException("The endpoint address specified an unknown host: " + address); } TopicDescription topicDescription = await(await host.RootNamespaceManager.ConfigureAwait(false)).CreateTopicSafeAsync(address.GetTopicDescription()).ConfigureAwait(false); MessagingFactory messagingFactory = await host.MessagingFactory.ConfigureAwait(false); MessageSender messageSender = await messagingFactory.CreateMessageSenderAsync(topicDescription.Path).ConfigureAwait(false); var sendTransport = new ServiceBusSendTransport(messageSender); sendTransport.ConnectSendObserver(_sendObservable); return(new SendEndpoint(sendTransport, _serializer, address, _sourceAddress)); }
public async Task Should_be_configured_and_working() { var settings = new TestAzureServiceBusAccountSettings(); var provider = new SharedAccessKeyTokenProvider(settings); TokenProvider tokenProvider = provider.GetTokenProvider(); Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", Configuration.ServiceNamespace, "MassTransit.AzureServiceBusTransport.Tests"); var namespaceManager = new NamespaceManager(serviceUri, tokenProvider); CreateQueue(namespaceManager, serviceUri, "TestClient"); CreateHostQueue(tokenProvider); var mfs = new MessagingFactorySettings { TokenProvider = tokenProvider, OperationTimeout = TimeSpan.FromSeconds(30), TransportType = TransportType.Amqp, AmqpTransportSettings = new AmqpTransportSettings { BatchFlushInterval = TimeSpan.FromMilliseconds(50), }, }; MessagingFactory factory = MessagingFactory.Create( ServiceBusEnvironment.CreateServiceUri("sb", Configuration.ServiceNamespace, Environment.MachineName), mfs); MessageReceiver receiver = await factory.CreateMessageReceiverAsync("Control"); receiver.PrefetchCount = 100; var done = TaskUtil.GetTask <bool>(); int count = 0; const int limit = 1000; receiver.OnMessageAsync(async message => { await message.CompleteAsync(); int received = Interlocked.Increment(ref count); if (received == limit) { done.TrySetResult(true); } }, new OnMessageOptions { AutoComplete = false, MaxConcurrentCalls = 100, AutoRenewTimeout = TimeSpan.FromSeconds(60), }); MessageSender client = await factory.CreateMessageSenderAsync("Control"); Stopwatch stopwatch = Stopwatch.StartNew(); Task[] tasks = new Task[limit]; for (int i = 0; i < limit; i++) { tasks[i] = SendAMessage(client); } await done.Task; stopwatch.Stop(); await Task.WhenAll(tasks); await receiver.CloseAsync(); Console.WriteLine("Performance: {0:F2}/s", limit * 1000 / stopwatch.ElapsedMilliseconds); }
static async Task SendBookingRequests(MessagingFactory senderMessagingFactory) { // and now we'll send some booking requests dynamic bookingRequests = new dynamic[] { new { flight = new { bookingClass = "C", legs = new[] { new { flightNo = "XB937", from = "DUS", to = "LHR", date = "2017-08-01" }, new { flightNo = "XB49", from = "LHR", to = "SEA", date = "2017-08-01" }, new { flightNo = "XB48", from = "SEA", to = "LHR", date = "2017-08-10" }, new { flightNo = "XB940", from = "LHR", to = "DUS", date = "2017-08-11" } } }, hotel = new { name = "Hopeman", city = "Kirkland", state = "WA", checkin = "2017-08-01", checkout = "2017-08-10" }, car = new { vendor = "Hervis", airport = "SEA", from = "2017-08-01T17:00", until = "2017-08-10:17:00" } }, new { flight = new { bookingClass = "C", legs = new[] { new { flightNo = "XL75", from = "DUS", to = "FRA", date = "2017-08-01" }, new { flightNo = "XL490", from = "FRA", to = "SEA", date = "2017-08-01" }, new { flightNo = "XL491", from = "SEA", to = "FRA", date = "2017-08-10" }, new { flightNo = "XL78", from = "FRA", to = "DUS", date = "2017-08-11" } } }, hotel = new { name = "Eastin", city = "Bellevue", state = "WA", checkin = "2017-08-01", checkout = "2017-08-10" }, car = new { vendor = "Avional", airport = "SEA", from = "2017-08-01T17:00", until = "2017-08-10:17:00" } }, new { hotel = new { name = "Eastin", city = "Bellevue", state = "WA", checkin = "2017-08-01", checkout = "2017-08-10" } }, new { flight = new { bookingClass = "Y", legs = new[] { new { flightNo = "XL75", from = "DUS", to = "FRA", date = "2017-08-01" }, new { flightNo = "XL78", from = "FRA", to = "DUS", date = "2017-08-11" } } } }, new { car = new { vendor = "Hext", airport = "DUS", from = "2017-08-01T17:00", until = "2017-08-10:17:00" } } }; Console.WriteLine("Sending requests"); var sender = await senderMessagingFactory.CreateMessageSenderAsync(SagaInputQueueName); for (int j = 0; j < 5; j++) { for (int i = 0; i < bookingRequests.Length; i++) { var message = new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(bookingRequests[i])))) { ContentType = "application/json", Label = "TravelBooking", TimeToLive = TimeSpan.FromMinutes(15) }; await sender.SendAsync(message); Interlocked.Increment(ref pendingTransactions); } } }
static async Task SendBookingRequests(MessagingFactory senderMessagingFactory) { // and now we'll send some booking requests dynamic bookingRequests = new dynamic[] { new { flight = new { bookingClass = "C", legs = new[] { new {flightNo = "XB937", from = "DUS", to = "LHR", date = "2017-08-01"}, new {flightNo = "XB49", from = "LHR", to = "SEA", date = "2017-08-01"}, new {flightNo = "XB48", from = "SEA", to = "LHR", date = "2017-08-10"}, new {flightNo = "XB940", from = "LHR", to = "DUS", date = "2017-08-11"} } }, hotel = new {name = "Hopeman", city = "Kirkland", state = "WA", checkin = "2017-08-01", checkout = "2017-08-10"}, car = new {vendor = "Hervis", airport = "SEA", from = "2017-08-01T17:00", until = "2017-08-10:17:00"} }, new { flight = new { bookingClass = "C", legs = new[] { new {flightNo = "XL75", from = "DUS", to = "FRA", date = "2017-08-01"}, new {flightNo = "XL490", from = "FRA", to = "SEA", date = "2017-08-01"}, new {flightNo = "XL491", from = "SEA", to = "FRA", date = "2017-08-10"}, new {flightNo = "XL78", from = "FRA", to = "DUS", date = "2017-08-11"} } }, hotel = new {name = "Eastin", city = "Bellevue", state = "WA", checkin = "2017-08-01", checkout = "2017-08-10"}, car = new {vendor = "Avional", airport = "SEA", from = "2017-08-01T17:00", until = "2017-08-10:17:00"} }, new { hotel = new {name = "Eastin", city = "Bellevue", state = "WA", checkin = "2017-08-01", checkout = "2017-08-10"} }, new { flight = new { bookingClass = "Y", legs = new[] { new {flightNo = "XL75", from = "DUS", to = "FRA", date = "2017-08-01"}, new {flightNo = "XL78", from = "FRA", to = "DUS", date = "2017-08-11"} } } }, new { car = new {vendor = "Hext", airport = "DUS", from = "2017-08-01T17:00", until = "2017-08-10:17:00"} } }; Console.WriteLine("Sending requests"); var sender = await senderMessagingFactory.CreateMessageSenderAsync(SagaInputQueueName); for (int j = 0; j < 5; j++) { for (int i = 0; i < bookingRequests.Length; i++) { var message = new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(bookingRequests[i])))) { ContentType = "application/json", Label = "TravelBooking", TimeToLive = TimeSpan.FromMinutes(15) }; await sender.SendAsync(message); Interlocked.Increment(ref pendingTransactions); } } }
public async Task <IMessageSender> CreateMessageSender(string entitypath) { return(new MessageSenderAdapter(await factory.CreateMessageSenderAsync(entitypath).ConfigureAwait(false))); }