public void SubscribeWithHandler(Func <TransportMessage, CancellationToken, Task> handler) { if (_bus != null) { return; } _bus = Configure .With(_activator) .Routing(r => r.AddTransportMessageForwarder(async transportMessage => { if (!AllHeadersMatch(transportMessage.Headers)) { return(ForwardAction.None); } await handler(transportMessage, CancellationToken.None); return(ForwardAction.Ignore()); })) .Transport(t => { t.UseRabbitMq(Configuration.ConnectionString, Configuration.ClientId).InputQueueOptions(x => x.SetAutoDelete(Configuration.AutoDeleteQueue)); }) .Start(); _bus.Advanced.Topics.Subscribe(Configuration.TopicFullName); }
protected override Task ExecuteAsync(CancellationToken stoppingToken) { var handlerActivator = new DependencyInjectionHandlerActivator(_serviceProvider); var configurer = Configure.With(handlerActivator); configurer .Serialization(serializer => serializer.UseNewtonsoftJson(DefaultContentSerializer.CreateDefaultJsonSerializationSettings())) .Transport(transport => transport.UseAzureServiceBus(_configuration.GetConnectionString("AzureServiceBus"), "error")) .Routing(r => { r.AddTransportMessageForwarder(transportMessage => { var returnAddress = transportMessage.Headers[Headers.SourceQueue]; if (returnAddress.Contains("workflow-management-events")) { return(Task.FromResult(ForwardAction.Ignore())); } return(Task.FromResult(ForwardAction.ForwardTo(returnAddress))); }); }); var bus = configurer.Start(); return(Task.CompletedTask); }
public async Task CanForwardToMultipleRecipients() { var network = new InMemNetwork(); var activator = new BuiltinHandlerActivator(); Using(activator); var recipients = new[] { "recipient-A", "recipient-B" }.ToList(); recipients.ForEach(network.CreateQueue); Configure.With(activator) .Transport(t => t.UseInMemoryTransport(network, "forwarder")) .Routing(t => { t.AddTransportMessageForwarder(async transportMessage => ForwardAction.ForwardTo(recipients)); }) .Start(); await activator.Bus.SendLocal("HEJ MED DIG!!!"); var transportMessages = await Task.WhenAll(recipients.Select(async queue => { var message = await network.WaitForNextMessageFrom(queue); return(message); })); Assert.That(transportMessages.Length, Is.EqualTo(2)); }
static IBus GetFailingBus(BuiltinHandlerActivator activator, InMemNetwork network, string recipientQueueName, ErrorBehavior errorBehavior) { var deliveryAttempts = 0; var bus = Configure.With(activator) .Transport(t => t.UseInMemoryTransport(network, "forwarder")) .Routing(t => { t.AddTransportMessageForwarder(async transportMessage => { deliveryAttempts++; if (deliveryAttempts < 10) { throw new RebusApplicationException("fake an error"); } return(ForwardAction.ForwardTo(recipientQueueName)); }, errorBehavior); }) .Options(o => { o.SetNumberOfWorkers(1); o.SetMaxParallelism(1); }) .Start(); return(bus); }
public void SubscribeWithHandler(Func <TransportMessage, CancellationToken, Task> handler) { if (_bus != null) { return; } _bus = Configure .With(_activator) .Options(o => o.SetNumberOfWorkers(0)) .Routing(r => r.AddTransportMessageForwarder(async transportMessage => { if (!AllHeadersMatch(transportMessage.Headers)) { return(ForwardAction.None); } await handler(transportMessage, CancellationToken.None); return(ForwardAction.Ignore()); })) .Transport(t => { t.UseRabbitMq(Configuration.ConnectionString, $"Elsa{Guid.NewGuid().ToString("n").ToUpper()}").InputQueueOptions(o => o.SetAutoDelete(autoDelete: true)); }) .Start(); _bus.Advanced.Topics.Subscribe(Configuration.RoutingKey); }
public async Task CanIgnoreMessageCompletely(bool ignoreTheMessage) { var network = new InMemNetwork(); var activator = new BuiltinHandlerActivator(); Using(activator); var gotTheMessage = new ManualResetEvent(false); activator.Handle <string>(async str => { gotTheMessage.Set(); }); var recipients = new[] { "recipient-A", "recipient-B" }.ToList(); recipients.ForEach(network.CreateQueue); Configure.With(activator) .Transport(t => t.UseInMemoryTransport(network, "forwarder")) .Routing(t => { if (ignoreTheMessage) { t.AddTransportMessageForwarder(async transportMessage => ForwardAction.Ignore()); } }) .Start(); await activator.Bus.SendLocal("HEJ MED DIG!!!"); if (ignoreTheMessage) { Assert.That(gotTheMessage.WaitOne(TimeSpan.FromSeconds(0.5)), Is.False); } else { Assert.That(gotTheMessage.WaitOne(TimeSpan.FromSeconds(0.5)), Is.True); } }
/// <summary> /// Prepares invoker object in Command Pattern for the client /// </summary> /// <returns>Invoker object</returns> public static Driver GetDriver() { // Set up receiver ICar car = CarFactory.GetCar(DriverType.Buyer); car.Start(); // Create the commands IAction forwardAction = new ForwardAction(car); IAction reverseAction = new ReverseAction(car); IAction steerLeftAction = new SteerLeftAction(car); IAction steerRightAction = new SteerRightAction(car); // Create the invoker Driver driver = new Driver( forwardAction, reverseAction, steerLeftAction, steerRightAction ); // Return invoker to client return(driver); }
public ForwardActionOccurence(ForwardAction forward) { this.forward = forward; }
public Occurrence Memory(float delay, ForwardAction forward) { return(Memory(delay, false, new ForwardActionOccurence(forward))); }
public Occurrence Plan(float delay, ForwardAction forward) { return(Plan(delay, false, new ForwardActionOccurence(forward))); }
public Occurrence Schedule(float time, ForwardAction forward) { return(Schedule(time, false, new ForwardActionOccurence(forward))); }
public Occurrence Plan(float delay, bool repeatable, ForwardAction forward, BackwardAction backward) { return(Plan(delay, repeatable, new ActionOccurence(forward, backward))); }
public DelegateOccurrence(ForwardAction <T> forward, BackwardAction <T> backward) { this.forward = forward; this.backward = backward; }
public Occurrence Do(bool repeatable, ForwardAction forward, BackwardAction backward) { return(Do(repeatable, new DelegateOccurrence(forward, backward))); }
public async Task CanDistributeWork(int numberOfMessages) { var transportConfigurer = GetTransportConfigurer(); var workers = new[] { "worker1", "worker2", "worker3", "worker4" } .Select(TestConfig.GetName) .ToArray(); var distributorQueueName = TestConfig.GetName("distributor"); workers.ForEach(name => { StartWorker(name, transportConfigurer); }); var distributor = Configure.With(Using(new BuiltinHandlerActivator())) .Logging(l => l.Console(LogLevel.Info)) .Transport(t => { transportConfigurer(t, distributorQueueName); }) .Routing(r => { var nextDestinationIndex = 0; r.AddTransportMessageForwarder(async transportMessage => { var headers = transportMessage.Headers; if (headers.ContainsKey(SpecialHeaderKey)) { var index = Interlocked.Increment(ref nextDestinationIndex) % workers.Length; return(ForwardAction.ForwardTo(workers[index])); } return(ForwardAction.None); }); }) .Options(o => { o.LogPipeline(verbose: true); }) .Start(); await Task.WhenAll( Enumerable.Range(0, numberOfMessages) .Select(id => new Work { WorkId = id }) .Select(work => distributor.SendLocal(work, _forwardHeaders)) ); await _doneWork.WaitUntil(w => w.Count == numberOfMessages, timeoutSeconds : 20); var workByWorker = _doneWork.GroupBy(w => w.Worker).ToList(); Console.WriteLine(@"Done work: {0} ", string.Join(Environment.NewLine, workByWorker.Select(g => $" {g.Key}: {g.Count()}"))); Assert.That(workByWorker.Count, Is.EqualTo(workers.Length), "Expected that all workers got to do some work!"); }
public ForwardActionOccurence(ForwardAction forward) { this.forward = forward; }
public Occurrence Plan <T>(float delay, bool repeatable, ForwardAction <T> forward, BackwardAction <T> backward) { return(Plan(delay, repeatable, new DelegateOccurrence <T>(forward, backward))); }
public Occurrence Schedule(float time, bool repeatable, ForwardAction forward, BackwardAction backward) { return(Schedule(time, repeatable, new DelegateOccurrence(forward, backward))); }
public ActionOccurence(ForwardAction forward, BackwardAction backward) { this.forward = forward; this.backward = backward; }
public Occurrence Memory(float delay, bool repeatable, ForwardAction forward, BackwardAction backward) { return(Memory(delay, repeatable, new DelegateOccurrence(forward, backward))); }
public ActionOccurence(ForwardAction forward, BackwardAction backward) { this.forward = forward; this.backward = backward; }