Sample Project On NService Bus scheduler - nservicebus

I have one requirement in my current project, where, I need to implement NService bus scheduler. As I am new to Nservice bus scheduler implementation, please raise your hand, if any one is having knowledge about this, please share your thoughts on any useful link also will be helpful.
Thanks in advance
Vimal

The NserviceBus Scheduler is quite easy to work with.
As an example, here's a snippet:
// 'Schedule' is an instance class that can be resolved from the container.
// To send a message every 5 minutes
schedule.Every(TimeSpan.FromMinutes(5), () => bus.Send(new CallLegacySystem()));
// Name a schedule task and invoke it every 5 minutes
schedule.Every(TimeSpan.FromMinutes(5), "MyCustomTask", SomeCustomMethod);
Take note though that this scheduler is message based, not time based. Meaning that when a task is scheduled to be executed at time X, it is not executed at time X, but rather queued at that time. See:
Since the scheduler uses the queuing mechanism, it does have some side effects on the timelines of scheduled tasks. When a task is scheduled to be run at a given time it is not "executed at that time", it is instead "queued at that time" to be executed. In most cases this distinction will have no noticeable effect on the behavior of the scheduling API. However in high load systems the fact that a scheduled task is added to the back of the queue can result in a noticeable delay between the "time the task has been request to be run" and the "time the task is actually executed".
If you need more help, the documentation can be found here and is quite thorough.

Related

android studio call a functoin with alarm manager

I'm making an app in kotlin that query an API once a day with alarm manager.
Now I thought of a problem: what to do if there is no internet connection when the alarm manager is executed?
My best idea is to query the API and if there is no response I would set another alarm manager to half an hour later.
the problem is that the alarm manager needs a pending intent and I don't know how to call another function in the activity with a pending intent.
I can just call the same activity with another intent, but I'm afraid that if there won't be internet connection several times in a row something bad will happen because there will be to many intents.
So my question is if the bad thing will actually happen because of all the intents
or maybe you have a better solution than mine...
You should use WorkManager for this purpose. It is designed to support these kinds of tasks. You can schedule periodic execution and tell WorkManager that your task needs Internet connectivity. WorkManager ensures that the task only runs when there is connectivity. Tasks scheduled with WorkManager are persisted across device restarts so you don't have to worry about rescheduling them after a device reboot.
See https://developer.android.com/topic/libraries/architecture/workmanager

Schedule a background task (Cron Job) for a specific date and time

I am creating an ASP NET Core app, and I have certain entities, for example, a dispute for an order. After the dispute has been opened, the Creation Date property is set for it. I want to make sure that if the buyer does not answer in his dispute within 7 days, the dispute is closed automatically.
I understand that it can be done through CronJob with a certain interval, but I would like to avoid an excessive number of calls to check the date in all disputes.
Is there a proper way to schedule task calls at a specific time? I need it workable even after app restarting.
Is there a proper way to schedule task calls at a specific time?
To execute a background task/job at a user specified time, you can try to use some message queue services, such Azure Queue Storage queues that enable us to specify how long the message should be invisible to Dequeue and Peek operations by setting visibilityTimeout.
And you can implement a queue triggered background task to retrieve message(s) from the queue and close the dispute in code logic.

need some kind of job scheduler or delayed message queue in a java world

I'm needing to execute a process in the future, let's say 20min, based on some event happening, but I may need to cancel that scheduled process depending on different factors. Or , i may need to restart the timer on the job, depending on another event....etc. You get the idea. All different permutations of this. Does anyone know of a good technology for this need? Maybe quartz(does quartz suck? does it do all these things?), maybe activemq, maybe some other job scheduling technology?
Thanks!
-Ron
ActiveMQ's scheduler is a good fit for this. The pattern can go something like:
Kick off a process (get some identifier)
Send a message to the ActiveMQ scheduler to fire in x time period
Message Consumer receives the timer message, pulls the identifier to check on the status
If process is done.. continue and finish up
If process needs more wait time, send another timer message to ActiveMQ
Everything is asynchronous, and code required is very minimal. The big advantage of using ActiveMQ is you can have multiple consumers listening for the scheduled message to provide for high availability.

Understanding Eventual Consistency, BacklogItem and Tasks example from Vaughn Vernon

I'm struggling to understand how to implement Eventual Consistency with the exposed example of BacklogItems and Tasks from Vaughn Vernon. The statement I've understood so far is (considering the case where he splits BacklogItem and Task into separate aggregate roots):
A BacklogItem can contain one or more tasks. When all remaining hours from a the tasks of a BacklogItem are 0, the status of the BacklogItem should change to "DONE"
I'm aware about the rule that says that you should not update two aggregate roots in the same transaction, and that you should accomplish that with eventual consistency.
Once a Domain Service updates the amount of hours of a Task, a TaskRemainingHoursUpdated event should be published to a DomainEventPublisher which lives in the same thread as the executing code. And here it is where I'm at a loss with the following questions:
I suppose that there should be a subscriber (also living in the same thread I guess) that should react to TaskRemainingHoursUpdated events. At which point in your Desktop/Web application you perform this subscription to the Bus? At the very initialization of your app? In the application code? Is there any reasoning to place domain subscriptors in a specific place?
Should that subscriptor (in the same thread) call a BacklogItem repository and perform the update? (But that would be a violation of the rule of not updating two aggregates in the same transaction since this would happen synchronously, right?).
If you want to achieve eventual consistency to fulfil the previously mentioned rule, do I really need a Message Broker like RabbitMQ even though both BacklogItem and Task live inside the same Bounded Context?
If I use this message broker, should I have a background thread or something that just consumes events from a RabbitMQ queue and then dispatches the event to update the product?
I'd appreciate if someone can shed some clear light over this since it is quite complex to picture in its completeness.
So to start with, you need to recognize that, if the BacklogItem is the authority for whether or not it is "Done", then it needs to have all of the information to compute that for itself.
So somewhere within the BacklogItem is data that is tracking which Tasks it knows about, and the known state of those tasks. In other words, the BacklogItem has a stale copy of information about the task.
That's the "eventually consistent" bit; we're trying to arrange the system so that the cached copy of the data in the BacklogItem boundary includes the new changes to the task state.
That in turn means we need to send a command to the BacklogItem advising it of the changes to the task.
From the point of view of the backlog item, we don't really care where the command comes from. We could, for example, make it a manual process "After you complete the task, click this button here to inform the backlog item".
But for the sanity of our users, we're more likely to arrange an event handler to be running: when you see the output from the task, forward it to the corresponding backlog item.
At which point in your Desktop/Web application you perform this subscription to the Bus? At the very initialization of your app?
That seems pretty reasonable.
Should that subscriptor (in the same thread) call a BacklogItem repository and perform the update? (But that would be a violation of the rule of not updating two aggregates in the same transaction since this would happen synchronously, right?).
Same thread and same transaction are not necessarily coincident. It can all be coordinated in the same thread; but it probably makes more sense to let the consequences happen in the background. At their core, events and commands are just messages - write the message, put it into an inbox, and let the next thread worry about processing.
If you want to achieve eventual consistency to fulfil the previously mentioned rule, do I really need a Message Broker like RabbitMQ even though both BacklogItem and Task live inside the same Bounded Context?
No; the mechanics of the plumbing matter not at all.

How does ActiveMQ AMQ_SCHEDULED_DELAY message works?

We want to use delay feature from activeMQ to delay particural event. How does AMQ_SCHEDULED_DELAY work internaly? In documentation is information about scheduler but no information what mechanism it utilize to delay message. For that reason we are not sure how delaying is going to affect activeMQ. Does activeMQ utilize pooling or async to achive delay.
I ask this question because people from my organization want to pick diffrent technology. I do not have any proof delay from activeMQ is any better.
Here is link to source code. I was thinking of looking up code but I'm not good in java. Can anyone help?
Default implementation of ActiveMQ does utilize the polling.
Active MQ internally keep polling for the scheduled (or delayed) messages by a background scheduler thread. This thread read the list of scheduled events (or messages) and fires the jobs, reschedule repeating jobs as needed before firing the job event.
The list of scheduled events is stored in a sorted order in internal storage of activemq. So during poll, it just read event which are scheduled for earliest processing. Since the messages are persisted during enquing, scheduling many not have visible performance impact during processing.
However before adopting, you can setup your benchmark, without worries much internal implementation detail, to see that your performance/SLA requirement are getting met.
For more details, you may refer to Javadoc of job scheduler API. For default implementation can you refers to the code.
Hope this helps.
In looking at the source code mentioned by #skadya, the term "polling" is not what I interpret. It appears to use the Java Object class' wait(long timeout) method to determine when to "wake up" the thread that runs the jobs.
So, I wouldn't call it polling. I would call it an asynchronous mechanism in which the delay / timeout is set such that the thread will wake up (e.g. to run the next scheduled job at the appropriate time) via the timeout set to a value that is appropriate for the next scheduled job's commencement.
Javadoc for Object.wait(long timeout)
Note that the implementation for Object.wait is a native (i.e. non-java) implementation provided by the JDK / JRE / JVM for a given platform. For what that's worth.
It is possible to do performance test with activemq web console. There is an option to send message with configurable delay and number of messages to send. It doesn't answer my question but it seems like best option to compare two approaches.