mirror of https://github.com/abpframework/abp.git
14 changed files with 166 additions and 15 deletions
@ -0,0 +1,8 @@ |
|||
namespace Volo.Abp.BackgroundJobs.RabbitMQ |
|||
{ |
|||
public interface IJobListener |
|||
{ |
|||
void Start(); |
|||
void Stop(); |
|||
} |
|||
} |
|||
@ -0,0 +1,62 @@ |
|||
using System; |
|||
using RabbitMQ.Client; |
|||
using RabbitMQ.Client.Events; |
|||
using Volo.Abp.RabbitMQ; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.RabbitMQ |
|||
{ |
|||
public class JobListener<TJob> : IJobListener |
|||
{ |
|||
protected string JobName { get; } |
|||
protected IBackgroundJobExecuter JobExecuter { get; } |
|||
protected IChannelPool ChannelPool { get; } |
|||
protected IRabbitMqSerializer Serializer { get; } |
|||
protected Type ArgsType { get; } |
|||
|
|||
protected IChannelAccessor ChannelAccessor { get; private set; } |
|||
protected EventingBasicConsumer Consumer { get; private set; } |
|||
|
|||
public JobListener( |
|||
IChannelPool channelPool, |
|||
IBackgroundJobExecuter jobExecuter, |
|||
IRabbitMqSerializer serializer) |
|||
{ |
|||
ChannelPool = channelPool; |
|||
JobExecuter = jobExecuter; |
|||
Serializer = serializer; |
|||
ArgsType = BackgroundJobArgsHelper.GetJobArgsType(typeof(TJob)); |
|||
JobName = BackgroundJobNameAttribute.GetName(ArgsType); |
|||
} |
|||
|
|||
public void Start() |
|||
{ |
|||
var queueName = "BackgroundJobs." + JobName; //TODO: Make prefix optional
|
|||
|
|||
ChannelAccessor = ChannelPool.Acquire(queueName); |
|||
|
|||
//TODO: How to ensure that queue is created!
|
|||
|
|||
Consumer = new EventingBasicConsumer(ChannelAccessor.Channel); |
|||
Consumer.Received += MessageReceived; |
|||
|
|||
//TODO: What BasicConsume returns?
|
|||
ChannelAccessor.Channel.BasicConsume( |
|||
queue: queueName, |
|||
autoAck: false, |
|||
consumer: Consumer |
|||
); |
|||
} |
|||
|
|||
private void MessageReceived(object sender, BasicDeliverEventArgs e) |
|||
{ |
|||
var context = new JobExecutionContext(typeof(TJob), Serializer.Deserialize(e.Body, ArgsType)); |
|||
JobExecuter.Execute(context); |
|||
//TODO: How to ACK on success or Reject on failure?
|
|||
} |
|||
|
|||
public void Stop() |
|||
{ |
|||
ChannelAccessor.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,8 +1,9 @@ |
|||
using RabbitMQ.Client; |
|||
using System; |
|||
using RabbitMQ.Client; |
|||
|
|||
namespace Volo.Abp.RabbitMQ |
|||
{ |
|||
public interface IConnectionPool |
|||
public interface IConnectionPool : IDisposable |
|||
{ |
|||
IConnection Get(string connectionName = null); |
|||
} |
|||
|
|||
@ -0,0 +1,9 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.RabbitMQ |
|||
{ |
|||
public class QueueDictionary : Dictionary<string, QueueOptions> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
using System.Collections.Generic; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.RabbitMQ |
|||
{ |
|||
public class QueueOptions |
|||
{ |
|||
[NotNull] |
|||
public string Name { get; } |
|||
|
|||
public bool Durable { get; set; } |
|||
|
|||
public bool Exclusive { get; set; } |
|||
|
|||
public bool AutoDelete { get; set; } |
|||
|
|||
public IDictionary<string, object> Arguments { get; } |
|||
|
|||
public QueueOptions([NotNull] string name, bool durable = true, bool exclusive = false, bool autoDelete = false) |
|||
{ |
|||
Name = name; |
|||
Durable = durable; |
|||
Exclusive = exclusive; |
|||
AutoDelete = autoDelete; |
|||
Arguments = new Dictionary<string, object>(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue