Base solution for your next web application
Open Closed

Background job console app #12443


User avatar
0
uenlkr4e created

Hi, In our app we will be using RabbitMQ (switched from default abp queue manager) as our queue manager and decided to write a separate console app to consume the background jobs. Is there a sample app that would give us insight on this? We plan on using masstransit messaging framework also.

Would it be possible to call the execute methods from our console app? There seems to be many dependencies to manage and I cant find a proper way to make them all work.

A starter app would really help here or some pointers. Thank you

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

1 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @uenlkr4e

    The console app is initialized using AbpBootstrapper to allow full dependency injection and Abp features. We register the consumer and the bus inside the ConsoleAppModule, following Abp’s module system.

    using System;
    using Abp.Dependency;
    using Abp.Modules;
    using Abp.Reflection.Extensions;
    using Castle.MicroKernel.Registration;
    using MassTransit;
    using YourApp.ConsoleApp;
    using YourApp.Core;
    
    namespace YourApp.ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var bootstrapper = AbpBootstrapper.Create<ConsoleAppModule>())
                {
                    bootstrapper.Initialize();
    
                    var busControl = IocManager.Instance.Resolve<IBusControl>();
                    busControl.Start(); // Start MassTransit
    
                    Console.WriteLine("Consumer is running. Press any key to exit...");
                    Console.ReadKey();
    
                    busControl.Stop();
                }
            }
        }
    }
    
    using Abp.Modules;
    using Abp.Reflection.Extensions;
    using Castle.MicroKernel.Registration;
    using MassTransit;
    
    namespace YourApp.ConsoleApp
    {
        [DependsOn(typeof(YourAppCoreModule))] // Replace with your actual core module
        public class ConsoleAppModule : AbpModule
        {
            public override void Initialize()
            {
                IocManager.RegisterAssemblyByConvention(typeof(ConsoleAppModule).GetAssembly());
                ConfigureMassTransit();
            }
    
            private void ConfigureMassTransit()
            {
                var container = IocManager.IocContainer;
    
                container.Register(
                    Component.For<YourJobConsumer>().LifestyleTransient()
                );
    
                container.Register(
                    Component.For<IBusControl>()
                             .UsingFactoryMethod(() =>
                             {
                                 return Bus.Factory.CreateUsingRabbitMq(cfg =>
                                 {
                                     cfg.Host("localhost", "/", h =>
                                     {
                                         h.Username("guest");
                                         h.Password("guest");
                                     });
    
                                     cfg.ReceiveEndpoint("your-queue-name", e =>
                                     {
                                         e.Consumer<YourJobConsumer>(() =>
                                             IocManager.Instance.Resolve<YourJobConsumer>());
                                     });
                                 });
                             })
                             .LifestyleSingleton()
                );
            }
        }
    }
    

    Consumer Class

    using MassTransit;
    using System.Threading.Tasks;
    using Abp.Domain.Uow;
    
    public class YourJobConsumer : IConsumer<YourJobMessage>
    {
        private readonly IYourService _yourService;
    
        public YourJobConsumer(IYourService yourService)
        {
            _yourService = yourService;
        }
    
        [UnitOfWork]
        public async Task Consume(ConsumeContext<YourJobMessage> context)
        {
            await _yourService.DoWorkAsync(context.Message);
        }
    }
    

    Using this sample setup, you can create a standalone console application that listens to and processes messages from RabbitMQ.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)