我使用以下示例作为创建部署MassTransit的引用点。 我的目标是模拟 恢复(适用于此) 缩放(也有效) 取消(无法使其工作并需要帮助) 继续回应/进展(工作进展 - 可能是我的下一个任务)
(更多描述因为我被搁置感谢指导我们) 这些是需要的nuget包
MassTransit MassTransit.RabbitMQ RabbitMQ.Client TopShelf
所以这是我的制作人。它只需要一个字符串来触发作业并从该作业中获得响应。
internal class Program { private static void Main() { IBusControl busControl = CreateBus(); busControl.Start(); try { for (; ; ) { Console.Write("Message to process (quit exits): "); string customerId = Console.ReadLine(); if (customerId == "quit") break;var serviceAddress = new Uri("rabbitmq://localhost/request_service"); IRequestClient<ISimpleRequest, ISimpleResponse> c2 = busControl.CreateRequestClient<ISimpleRequest, ISimpleResponse>(serviceAddress, TimeSpan.FromDays(10)); Task.Run(async () => { ISimpleResponse response = await c2.Request(new SimpleRequest(customerId)); Console.WriteLine("Processing of message {0} is successful", response.CusomerName); }); } } catch (Exception ex) {Console.WriteLine("Exception!!! OMG!!! {0}", ex);} finally { busControl.Stop(); } } private static IBusControl CreateBus() { return Bus.Factory.CreateUsingRabbitMq(x => x.Host(new Uri("rabbitmq://localhost"), h => { h.Username("guest"); h.Password("guest"); })); } }
Below is the code for consumer. The consumer just picks up a message and have a Thread of 30 sec to simulate long running jobs.
class Program { static int Main(string[] args) { return (int)HostFactory.Run(x => x.Service()); } }public class InitiateServiceBus : ServiceControl { IBusControl _busControl; public bool Start(HostControl hostControl) { _busControl = Bus.Factory.CreateUsingRabbitMq(x => { IRabbitMqHost host = x.Host(new Uri("rabbitmq://localhost"), h => { h.Username("guest"); h.Password("guest"); }); x.ReceiveEndpoint(host, "request_service", e => { e.Consumer<RequestConsumer>(); }); }); _busControl.Start(); return true; } public bool Stop(HostControl hostControl) { _busControl.Stop(); return true; } } public class RequestConsumer : IConsumer<ISimpleRequest> { private readonly ILog _log = Logger.Get<RequestConsumer>(); public async Task Consume(ConsumeContext<ISimpleRequest> context) { _log.InfoFormat("I am consumer {0}", context.Message.CustomerId); Thread.Sleep(TimeSpan.FromSeconds(30)); context.Respond(new SimpleResponse { CusomerName = "Processing Finished " }); } private class SimpleResponse :ISimpleResponse { public string CusomerName { get; set; } } }
And these are the messages i send public interface ISimpleRequest { DateTime Timestamp { get; } string CustomerId { get; } }
public interface ISimpleResponse { string CusomerName { get; } } public class SimpleRequest : ISimpleRequest { private readonly string _customerId; private readonly DateTime _timestamp; public SimpleRequest(string customerId) { _customerId = customerId; _timestamp = DateTime.UtcNow; } public DateTime Timestamp { get { return _timestamp; } } public string CustomerId { get { return _customerId; } } }
任何提示都会非常有用。
答案 0 :(得分:1)
我找到了一个解决方案,所以如果我使用这个
e.Consumer(()=&gt; {return reqConume; //这是一个单身人士 });
和单身而不是
e.Consumer();
在消费者中,我有相同的消费者实例,我可以直接监控消费者。发送取消事件也是如此。对不起麻烦的家伙
答案 1 :(得分:0)
您应该使用任务取消来实现此目的。根据MSDN
System.Threading.Tasks.Task和 System.Threading.Tasks.Task类支持取消 通过在.NET Framework中使用取消令牌。更多 信息,请参阅托管线程中的取消。
请查看以下链接了解更多详情 https://msdn.microsoft.com/en-us/library/dd997396(v=vs.110).aspx