Skip to content

Feature Message Queue Integration #2264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

personball
Copy link

@personball personball commented Jun 28, 2017

In this PR, I submit a solution about Message Queue Integration.

Change List:

  1. In project Abp, Add namespace Abp.MqMessages with interface IMqMessagePublisher and class NullMqMessagePublisher
  2. Add Projects:
    Abp.MqMessages
    Abp.MqMessages.Rebus
    Abp.MqMessages.RebusRabbitMqPublisher
    Abp.MqMessages.RebusRabbitMqConsumer
  3. Add mq-messages-sample in test folder with 3 sample projects.

Usage:

1. Message Definition in Sample.MqMessages

namespace Sample.MqMessages
{
    /// <summary>
    /// Custom MqMessage Definition. No depends on any framework,this class library can be shared as nuget pkg.
    /// </summary>
    public class TestMqMessage
    {
        public string Name { get; set; }
        public string Value { get; set; }
        public DateTime Time { get; set; }
    }
}

2. PublisherModule

2.1 Configuration

namespace Sample
{
    [DependsOn(typeof(RebusRabbitMqPublisherModule))]
    public class SampleRebusRabbitMqPublisherModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.Modules.UseRebusRabbitMqPublisher()
                .UseLogging(c => c.NLog())
                .ConnectionTo("amqp://dev:dev@rabbitmq.local.cn/dev_host");

            Configuration.BackgroundJobs.IsJobExecutionEnabled = true;
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }

        public override void PostInitialize()
        {
            Abp.Dependency.IocManager.Instance.IocContainer.AddFacility<LoggingFacility>(f => f.UseNLog().WithConfig("nlog.config"));

            var workManager = IocManager.Resolve<IBackgroundWorkerManager>();
            workManager.Add(IocManager.Resolve<TestWorker>());// to send TestMqMessage every 3 seconds
        }
    }
}

2.2 In TestWork

namespace Sample.BackgroundWorks
{
    public class TestWorker : PeriodicBackgroundWorkerBase, ISingletonDependency
    {
        private readonly IMqMessagePublisher _publisher;

        public TestWorker(AbpTimer timer, IMqMessagePublisher publisher)
            : base(timer)
        {
            _publisher = publisher;
            Timer.Period = 3000;//3 seconds
            Timer.RunOnStart = true;
        }

        protected override void DoWork()
        {
            Logger.Info($"TestWork Done! Time:{DateTime.Now}");
            _publisher.Publish(new TestMqMessage
            {
                Name = "TestWork",
                Value = "BlaBlaBlaBlaBlaBla",
                Time = DateTime.Now
            });
        }
    }
}

3. ConsumerModule

3.1 Configuation

namespace Sample
{
    [DependsOn(typeof(RebusRabbitMqConsumerModule))]
    public class SampleRebusRabbitMqConsumerModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.Modules.UseRebusRabbitMqConsumer()
                .UseLogging(c => c.NLog())
                .ConnectTo("amqp://dev:dev@rabbitmq.local.cn/dev_host")
                .UseQueue(Assembly.GetExecutingAssembly().GetName().Name)
                .RegisterHandlerInAssemblys(Assembly.GetExecutingAssembly());//register assembly whitch has rebus handlers
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void PostInitialize()
        {
            Abp.Dependency.IocManager.Instance.IocContainer.AddFacility<LoggingFacility>(f => f.UseNLog().WithConfig("nlog.config"));
        }
    }
}

3.2 Rebus Handler in Consumer

namespace Sample.Handlers
{
    public class TestHandler : IHandleMessages<TestMqMessage>
    {
        public ILogger Logger { get; set; }
        public IMqMessagePublisher Publisher { get; set; }
        public TestHandler()
        {
            Publisher = NullMqMessagePublisher.Instance;
        }

        public async Task Handle(TestMqMessage message)
        {
            var msg = $"{Logger.GetType()}:{message.Name},{message.Value},{message.Time}";
            Logger.Debug(msg);
            await Publisher.PublishAsync(msg);//send it again!
        }
    }
}

If you want to run this sample, you need an instance of rabbitmq and set ConnectionString like amqp://dev:dev@rabbitmq.local.cn/dev_host.

@personball personball changed the title Feature mq messages Feature Message Queue Integration Jun 28, 2017
@hikalkan hikalkan self-requested a review June 28, 2017 14:43
@hikalkan hikalkan added this to the v2.2 milestone Jun 28, 2017
@hikalkan
Copy link
Member

Thank you for your contribution. I will try to review this in next version.

@hikalkan hikalkan modified the milestones: v2.3, v2.2 Jul 17, 2017
@hikalkan hikalkan modified the milestones: v2.3, v2.4 Jul 30, 2017
@hikalkan hikalkan modified the milestones: v2.4, v2.5 Aug 12, 2017
@hikalkan hikalkan modified the milestones: v3.1, Backlog Oct 3, 2017
@AlexSenchenko
Copy link

Hi @personball
Are you using it already (Win/linx)? Is it mature enough?

@personball
Copy link
Author

@AlexSenchenko Yes, Windows work fine, but linux not tested yet.

For some dependency (Rebus) not resolved in dotnet standard 2.0. I can't say it works fine on linux.

@lataing
Copy link

lataing commented Jan 27, 2018

Why stop updating this feature? Is the technical problem? Personally, I think this is an important function.

@ismcagdas
Copy link
Member

@hikalkan what do you think ? Can we add it to v3.5 ?

@lataing
Copy link

lataing commented Jan 27, 2018

Maybe that's a good choice.
https://github.com/tangxuehua/equeue
a distributed mq written by c#.
He does not need to install third party software.

@natiki
Copy link
Contributor

natiki commented Jan 29, 2018

@lataing I would definitely like to see some more documentation before considering equeue.

@personball
Copy link
Author

@lataing As now, you can use this feature through my repo:https://github.com/personball/abplus. But it's not tracking latest version of ABP yet, you can fork the repo and work it out with no limitations.

@hitaspdotnet
Copy link

Come back on this feature and update please 😿 +1

@natiki
Copy link
Contributor

natiki commented Jul 2, 2018

Its worth reading https://github.com/dotnet-architecture/eShopOnContainers if you are doing anything micro services related. From that you should be able to use ABP as substance for each micro service.

@hikalkan hikalkan removed their request for review October 4, 2018 13:37
@bbakermmc
Copy link
Contributor

Seems like should be closed out

@hikalkan hikalkan closed this Mar 10, 2019
@hikalkan hikalkan removed this from the Backlog milestone Mar 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants