public void Consumer_Should_Auto_Get_Message_And_Invoke_Callback() { using (var scope = _serviceProvider.CreateScope()) { var consumer = scope.ServiceProvider.GetRequiredService <IRabbitMqConsumer>(); var connectionName = "default"; var queueDefinition = new QueueDefinition { QueueName = null, AutoDelete = false, Durable = false, Exclusive = false }; var disposableHandle = consumer.Subscribe <UserRegisterEventData>(UserRegisterProcessor); consumer.Initialize(queueDefinition, connectionName); Task UserRegisterProcessor(UserRegisterEventData user) { user.UserName.ShouldBe("sailucheng"); user.RegisterTime.ShouldBeGreaterThan(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); return(Task.CompletedTask); } } }
protected override void ProcessRecord() { ProcessImpl( filter => Api.QueueDefinitions.GetQueueDefinitions(filter: filter).Value, id => Api.QueueDefinitions.GetById(id), dto => QueueDefinition.FromDto(dto)); }
/// <summary> /// Validate the object. /// </summary> /// <exception cref="ValidationException"> /// Thrown if validation fails /// </exception> public virtual void Validate() { if (QueueDefinition != null) { QueueDefinition.Validate(); } if (ReviewerUser != null) { ReviewerUser.Validate(); } if (Reference != null) { if (Reference.Length > 128) { throw new ValidationException(ValidationRules.MaxLength, "Reference", 128); } if (Reference.Length < 0) { throw new ValidationException(ValidationRules.MinLength, "Reference", 0); } } if (Robot != null) { Robot.Validate(); } }
protected override void ProcessRecord() { ProcessImpl( (filter, top, skip) => Api.QueueDefinitions.GetQueueDefinitions(filter: filter, top: top, skip: skip, count: false), id => Api.QueueDefinitions.GetById(id), dto => QueueDefinition.FromDto(dto)); }
public void When_using_a_routing_key_with_a_wildcard() { int recieved = 0; var queue = new QueueDefinition { Name = "Queue" + Guid.NewGuid().ToString(), AutoDelete = true, RoutingKeys = new[] { "prefix.*" } }; _connector.ListenTo<int>( _exchangeDefinition, queue, new LambdaMessageHandler<int>((props, message) => { recieved = message; _reset.Set(); return true; })); _connector.SendTo(_exchangeDefinition, "prefix.key", props => { }, 123); _reset.WaitOne(TimeSpan.FromSeconds(5)); recieved.ShouldBe(123); }
public void When_listening_to_a_queue_and_serialization_fails() { var queue = new QueueDefinition { Name = QueueName, AutoDelete = true }; Exception exception = null; Dto recieved = null; var handler = new LambdaMessageHandler<Dto>( (props, json) => { recieved = json; _reset.Set(); return true; }, ex => { exception = ex; }); _connector.ListenTo(queue, handler); SendToQueue(123); _reset.WaitOne(TimeSpan.FromSeconds(5)); recieved.ShouldBe(null); exception.ShouldBeOfType<JsonSerializationException>(); }
static void Main(string[] args) { //configuration var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.dev.json").Build(); var services = new ServiceCollection(); services.AddSingleton(configuration); //logging services.AddLogging(cfg => cfg.AddConsole()); //rabbitmq services.AddRabbitMq(configuration.GetSection("rabbitmq")); var serviceProvider = services.BuildServiceProvider(); using (var scope = serviceProvider.CreateScope()) { var exchange = new ExchangeDefinition { ExchangeName = "logs", Type = ExchangeType.Topic }; var queue = new QueueDefinition { QueueName = "color.warning", AutoDelete = false, Exclusive = false, Durable = false }; var publisher = scope.ServiceProvider.GetRequiredService <IRabbitMqPublisher>(); //publisher publisher.Initialize(queue: queue, exchange: exchange, routingKey: "#.warning"); publisher.Publish(new UserRegisterEventData { UserName = "******", RegisterTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }); var consumer = scope.ServiceProvider.GetRequiredService <IRabbitMqConsumer>(); consumer.Initialize(queue); var disposableHandle = consumer.Subscribe <UserRegisterEventData>(usr => { Console.Write($"{usr.UserName}\t{DateTimeOffset.FromUnixTimeMilliseconds(usr.RegisterTime).ToString()}"); Console.WriteLine(); return(Task.CompletedTask); }); Console.WriteLine("Press any key to exit.."); Console.ReadLine(); //unsubscribe disposableHandle.Dispose(); Console.WriteLine("exit.."); } }
public async Task <QueueDefinition> CreateIfNotExist(QueueDefinition queueDefinition, CancellationToken token) { if ((await Exist(queueDefinition.QueueName, token))) { return(queueDefinition); } return(await Create(queueDefinition, token)); }
public async Task <QueueDefinition> Create(QueueDefinition queueDefinition, CancellationToken token) { queueDefinition.VerifyNotNull(nameof(queueDefinition)); QueueDescription createdDescription = await _managementClient.CreateQueueAsync(queueDefinition.ConvertTo(), token); _logging.LogTrace($"{nameof(Create)}: QueueName={queueDefinition.QueueName}"); return(createdDescription.ConvertTo()); }
public Task <QueueDefinition> CreateQueue(IWorkContext context, QueueDefinition queueDefinition) { queueDefinition.Verify(nameof(queueDefinition)).IsNotNull(); if (_queue.ContainsKey(queueDefinition.QueueName !)) { throw new InvalidOperationException(); } _queue.Add(queueDefinition.QueueName !, queueDefinition); return(Task.FromResult(queueDefinition)); }
protected override void ProcessRecord() { var queue = HandleHttpOperationException(() => Api.QueueDefinitions.Post(new QueueDefinitionDto { Name = Name, AcceptAutomaticallyRetry = AcceptAutomaticallyRetry.ToBool(), Description = Description, EnforceUniqueReference = EnforceUniqueReference.ToBool(), MaxNumberOfRetries = MaxNumberOfRetries })); WriteObject(QueueDefinition.FromDto(queue)); }
public void QueueDefinitionAddEditGetRemove() { using (var runspace = PowershellFactory.CreateAuthenticatedSession(TestContext)) { QueueDefinition queue = null; var name = TestRandom.RandomString(); var description = TestRandom.RandomString(); using (var cmdlet = PowershellFactory.CreateCmdlet(runspace)) { cmdlet.AddCommand(UiPathStrings.AddUiPathQueueDefinition) .AddParameter(UiPathStrings.Name, name) .AddParameter(UiPathStrings.Description, description) .AddParameter(UiPathStrings.AcceptAutomaticallyRetry); var queues = Invoke <QueueDefinition>(cmdlet); Validators.ValidateQueueDefinitionResponse(queues, null, name, description, true, false, 0); queue = queues[0]; } // Positional Get using (var cmdlet = PowershellFactory.CreateCmdlet(runspace)) { description = TestRandom.RandomString(); cmdlet.AddCommand(UiPathStrings.EditUiPathQueueDefinition) .AddArgument(queue) .AddParameter(UiPathStrings.Description, description); Invoke <QueueDefinition>(cmdlet); } using (var cmdlet = PowershellFactory.CreateCmdlet(runspace)) { cmdlet.AddCommand(UiPathStrings.GetUiPathQueueDefinition) .AddParameter(UiPathStrings.Id, queue.Id); var queues = Invoke <QueueDefinition>(cmdlet); Validators.ValidateQueueDefinitionResponse(queues, queue.Id, name, description, true, false, 0); } using (var cmdlet = PowershellFactory.CreateCmdlet(runspace)) { cmdlet.AddCommand(UiPathStrings.RemoveUiPathQueueDefinition) .AddParameter(UiPathStrings.QueueDefinition, queue); Invoke(cmdlet); } } }
public async Task GivenQueuePAth_WhenCreated_ShouldNotThrow(string nodeId) { IReadOnlyList <QueueDefinition> subjects = await _queueManagement.Search(_workContext); foreach (var item in subjects) { await _queueManagement.DeleteQueue(_workContext, item.QueueName !); } QueueDefinition testDefinition = new QueueDefinition { QueueName = nodeId, }; bool exist = await _queueManagement.QueueExists(_workContext, testDefinition.QueueName); if (exist) { await _queueManagement.DeleteQueue(_workContext, testDefinition.QueueName); } // Act bool state = await new StateManagerBuilder() .Add(new CreateQueueState(_queueManagement, testDefinition)) .Build() .Set(_workContext); state.Should().BeTrue(); subjects = await _queueManagement.Search(_workContext); subjects.Should().NotBeNull(); subjects.Count.Should().Be(1); subjects[0].QueueName.Should().BeEquivalentTo(testDefinition.QueueName); state = await new StateManagerBuilder() .Add(new RemoveQueueState(_queueManagement, testDefinition.QueueName)) .Build() .Set(_workContext); state.Should().BeTrue(); subjects = await _queueManagement.Search(_workContext); subjects.Should().NotBeNull(); subjects.Count.Should().Be(0); }
public InMemoryConnectorTests() { _reset = new AutoResetEvent(false); _connector = new InMemoryConnector(); _exchangeDefinition = new ExchangeDefinition("Exchange" + Guid.NewGuid().ToString(), ExchangeType.Direct) { AutoDelete = true }; _queueDefinition = new QueueDefinition { Name = "Queue" + Guid.NewGuid().ToString(), AutoDelete = true, RoutingKeys = new[] { "some.key" } }; }
public static QueueDescription ConvertTo(this QueueDefinition subject) { subject.VerifyNotNull(nameof(subject)); subject.QueueName !.VerifyNotEmpty(nameof(subject.QueueName)); return(new QueueDescription(subject.QueueName) { LockDuration = subject.LockDuration, RequiresDuplicateDetection = subject.RequiresDuplicateDetection, DuplicateDetectionHistoryTimeWindow = subject.DuplicateDetectionHistoryTimeWindow, RequiresSession = subject.RequiresSession, DefaultMessageTimeToLive = subject.DefaultMessageTimeToLive, AutoDeleteOnIdle = subject.AutoDeleteOnIdle, EnableDeadLetteringOnMessageExpiration = subject.EnableDeadLetteringOnMessageExpiration, MaxDeliveryCount = subject.MaxDeliveryCount, EnablePartitioning = subject.EnablePartitioning, }); }
public async Task GivenServiceQueue_WhenGetAndExist_ReturnData() { // Verify queue does not exist, if so delete it. bool exist = await _queueManagement.QueueExists(_workContext, _queueDefinition.QueueName !); if (exist) { await _queueManagement.DeleteQueue(_workContext, _queueDefinition.QueueName !); } // Create queue QueueDefinition subject = await _queueManagement.CreateQueue(_workContext, _queueDefinition); subject.Should().NotBeNull(); (_queueDefinition == subject).Should().BeTrue(); subject = await _queueManagement.GetQueue(_workContext, _queueDefinition.QueueName !); subject.Should().NotBeNull(); (_queueDefinition == subject).Should().BeTrue(); }
public async Task GivenServiceExistQueue_WhenSearchedWildcard_ShouldFind() { // Verify queue does not exist, if so delete it. bool exist = await _queueManagement.QueueExists(_workContext, _queueDefinition.QueueName !); if (!exist) { QueueDefinition createSubject = await _queueManagement.CreateQueue(_workContext, _queueDefinition); createSubject.Should().NotBeNull(); (_queueDefinition == createSubject).Should().BeTrue(); } IReadOnlyList <QueueDefinition> subjects = await _queueManagement.Search(_workContext, "unit1*"); subjects.Should().NotBeNull(); subjects.Count.Should().Be(1); subjects[0].QueueName.Should().BeEquivalentTo(_queueDefinition.QueueName); await _queueManagement.DeleteQueue(_workContext, _queueDefinition.QueueName !); }
public async Task GivenServiceQueue_WhenCreatedTwice_ShouldThrow() { // Verify queue does not exist, if so delete it. bool exist = await _queueManagement.QueueExists(_workContext, _queueDefinition.QueueName !); if (exist) { await _queueManagement.DeleteQueue(_workContext, _queueDefinition.QueueName !); } // Create queue QueueDefinition subject = await _queueManagement.CreateQueue(_workContext, _queueDefinition); subject.Should().NotBeNull(); (_queueDefinition == subject).Should().BeTrue(); Func <Task> act = async() => await _queueManagement.CreateQueue(_workContext, _queueDefinition); await act.Should().ThrowAsync <MessagingEntityAlreadyExistsException>(); await _queueManagement.DeleteQueue(_workContext, _queueDefinition.QueueName !); }
public void Verify_can_create_queue() { var container = GetContainerBuilder().BuildServiceProvider(); var result = container.GetService <IBrokerObjectFactory>() .Object <Queue>() .Create(x => { x.Queue("TestQueue31"); x.Configure(c => { c.IsDurable(); c.AutoDeleteWhenNotInUse(); c.HasArguments(arg => { arg.SetQueueExpiration(1000); arg.SetPerQueuedMessageExpiration(2000); }); }); x.Targeting(t => { t.VirtualHost("HareDu"); t.Node("Node1"); }); }) .GetResult(); result.HasFaulted.ShouldBeFalse(); result.DebugInfo.ShouldNotBeNull(); QueueDefinition definition = result.DebugInfo.Request.ToObject <QueueDefinition>(); definition.Arguments["x-expires"].ToString().ShouldBe("1000"); definition.Arguments["x-message-ttl"].ToString().ShouldBe("2000"); // definition.Arguments["x-expires"].ShouldBe(1000); // definition.Arguments["x-message-ttl"].ShouldBe(2000); definition.Durable.ShouldBeTrue(); definition.AutoDelete.ShouldBeTrue(); definition.Node.ShouldBe("Node1"); }
public async Task <Result> Create(Action <QueueCreateAction> action, CancellationToken cancellationToken = default) { cancellationToken.RequestCanceled(); var impl = new QueueCreateActionImpl(); action(impl); impl.Validate(); QueueDefinition definition = impl.Definition.Value; Debug.Assert(definition != null); string url = $"api/queues/{impl.VirtualHost.Value.ToSanitizedName()}/{impl.QueueName.Value}"; if (impl.Errors.Value.Any()) { return(new FaultedResult(impl.Errors.Value, new DebugInfoImpl(url, definition.ToJsonString(Deserializer.Options)))); } return(await Put(url, definition, cancellationToken).ConfigureAwait(false)); }
public void When_listening_to_a_queue() { var queue = new QueueDefinition { Name = QueueName, AutoDelete = true }; int recieved = 0; _connector.ListenTo<int>( queue, new LambdaMessageHandler<int>((props, json) => { recieved = json; _reset.Set(); return true; })); SendToQueue(123); _reset.WaitOne(TimeSpan.FromSeconds(5)); recieved.ShouldBe(123); }
public void When_listening_to_an_exchange_with_a_custom_queue() { var exchange = new ExchangeDefinition(ExchangeName, ExchangeType.Direct) { AutoDelete = true }; var queue = new QueueDefinition { Name = QueueName, AutoDelete = true, }; int recieved = 0; var unsubscribe = _connector.ListenTo<int>( exchange, queue, new LambdaMessageHandler<int>((props, json) => { recieved = json; _reset.Set(); return true; })); SendToExchange(123); _reset.WaitOne(TimeSpan.FromSeconds(5)); recieved.ShouldBe(123); }
protected override void ProcessRecord() { ProcessImpl( () => (ParameterSetName == QueueDefinitionParameterSet) ? QueueDefinition.ToDto(QueueDefinition) : HandleHttpOperationException(() => Api.QueueDefinitions.GetById(Id)), (queueDto) => HandleHttpOperationException(() => Api.QueueDefinitions.PutById(queueDto.Id.Value, queueDto))); }
public Cluster Install(bool system, string clusterName, string username, string email, string password) { cluster = new Cluster(Context) { Name = clusterName, System = system, }; cluster.Save(); // Create machine roles and machines // -- controller role var mrcont = new MachineRole(cluster) { Name = Constants.ControllerMachineRoleName, System = system, MachineRoleType = MachineRoleType.StandAlone, }; mrcont.Save(); var sv = new ServerVersion(mrcont) { Name = Constants.ServerVersionName, System = system, }; sv.Save(); var mcont = new Machine(mrcont) { Name = Constants.ControllerMachineName, }; mcont.Save(); var sicont = new ServerInstance(mcont) { Name = Constants.ServerInstanceName, ServerVersion = sv, }; sicont.Save(); // -- node role var mrnode = new MachineRole(cluster) { Name = Constants.NodeMachineRoleName, MachineRoleType = MachineRoleType.MirroredSet, }; mrnode.Save(); var nodesv = new ServerVersion(mrnode) { Name = Constants.ServerVersionName, }; nodesv.Save(); // -- Create a node /* * Machine mnode = new Machine(Context, mrnode); * mnode.Name = Constants.NodeMachineName; * mnode.Save(); * * si = new ServerInstance(Context, mnode); * si.Name = Constants.ServerInstanceName; * si.ServerVersionReference.Value = sv; * si.Save();*/ // Create the shared domain for cluster level databases and users var domain = new Domain(cluster) { Name = Constants.SharedDomainName, Email = email, System = system, }; domain.Save(); // Create administrator group and user GenerateAdminGroup(system); GenerateAdmin(system, username, email, password); // Create the shared feredation var federation = new Federation(domain) { Name = Constants.SharedFederationName, Email = email, System = system, ControllerMachine = mcont, SchemaSourceServerInstance = sicont, }; federation.Save(); // Temp database definition var tempdd = new DatabaseDefinition(federation) { Name = Constants.TempDbName, System = system, LayoutType = DatabaseLayoutType.Monolithic, DatabaseInstanceNamePattern = Constants.TempDbInstanceNamePattern, DatabaseNamePattern = Constants.TempDbNamePattern, SliceCount = 1, PartitionCount = 1, }; tempdd.Save(); var tempddi = new DatabaseDefinitionInstaller(tempdd); tempddi.GenerateDefaultChildren(nodesv, Constants.TempDbName); // Create cluster level jobs and queues // -- admin queue definition QueueDefinition qd = new QueueDefinition(cluster) { Name = Constants.MaintenanceQueueDefinitionName, System = system, }; qd.Save(); QueueInstance qi = new QueueInstance(mcont) { Name = Constants.MaintenanceQueueName, RunningState = Registry.RunningState.Running, }; qi.QueueDefinitionReference.Value = qd; qi.Save(); // -- long queue definition qd = new QueueDefinition(cluster) { Name = Constants.LongQueueDefinitionName }; qd.Save(); qi = new QueueInstance(mcont) { Name = Constants.LongQueueName, RunningState = Registry.RunningState.Running, }; qi.QueueDefinitionReference.Value = qd; qi.Save(); // -- quick queue definition qd = new QueueDefinition(cluster) { Name = Constants.QuickQueueDefinitionName, }; qd.Save(); qi = new QueueInstance(mcont) { Name = Constants.QuickQueueName, RunningState = Registry.RunningState.Running, }; qi.QueueDefinitionReference.Value = qd; qi.Save(); // -- database mirror job var jd = new JobDefinition(federation) { Name = typeof(Jhu.Graywulf.Jobs.MirrorDatabase.MirrorDatabaseJob).Name, System = system, WorkflowTypeName = typeof(Jhu.Graywulf.Jobs.MirrorDatabase.MirrorDatabaseJob).AssemblyQualifiedName, }; jd.DiscoverWorkflowParameters(); jd.Save(); // -- test job jd = new JobDefinition(federation) { Name = typeof(Jhu.Graywulf.Jobs.Test.TestJob).Name, System = system, WorkflowTypeName = typeof(Jhu.Graywulf.Jobs.Test.TestJob).AssemblyQualifiedName, }; jd.DiscoverWorkflowParameters(); jd.Save(); return(cluster); }
public void When_listening_to_a_queue_and_the_message_is_not_acknowleged() { var queue = new QueueDefinition { Name = QueueName, AutoDelete = true }; var recieved = 0; _connector.ListenTo<int>( queue, new LambdaMessageHandler<int>((props, message) => { recieved++; return recieved > 1; })); SendToQueue(123); _reset.WaitOne(TimeSpan.FromSeconds(5)); recieved.ShouldBe(2); }
public Task <QueueDefinition> UpdateQueue(IWorkContext context, QueueDefinition queueDefinition) { throw new NotImplementedException(); }
protected Action QueueResponder() { var queue = new QueueDefinition { Name = QueueName, AutoDelete = true }; return _connector.ListenTo<int>(queue, new LambdaMessageHandler<int>((props, message) => { var result = message.ToString().Length; _connector.SendTo( new QueueDefinition { Name = props.ReplyTo }, p => { p.CorrelationId = props.CorrelationId; }, result); return true; })); }
public void When_querying_a_queue() { var queue = new QueueDefinition { Name = QueueName, AutoDelete = true }; var unsubscribe = QueueResponder(); int recieved = 0; var message = 1234; _connector .Query<int>(queue, props => { }, message) .ContinueWith(response => { recieved = response.Result.Message; _reset.Set(); }) .Wait(); unsubscribe(); recieved.ShouldBe(4); }
public void When_sending_to_an_exchange_with_a_routing_key() { var exchange = new ExchangeDefinition(ExchangeName, ExchangeType.Direct) { AutoDelete = true }; var queue = new QueueDefinition { Name = QueueName, AutoDelete = true, RoutingKeys = new[] { "some.key" } }; int recieved = 0; var unsubscribe = _connector.ListenTo<int>( exchange, queue, new LambdaMessageHandler<int>((props, json) => { recieved = json; _reset.Set(); return true; })); _connector.SendTo(exchange, "some.key", props => { }, 123); _reset.WaitOne(TimeSpan.FromSeconds(5)); recieved.ShouldBe(123); }