I'm looking for clear documentation and/or and example on how to setup a time-based trigger on a global window in Apache beam.
The purpose is to perform a count of the events since the last trigger fired, even when 0 events have been added since.
You can use timers and state, if you need to use global window and emit result even if there was no event since the last firing. I think it is not possible to do it with the built in triggers.
You can keep the count in a state and use a timer to emit the results periodically.
These two blog post explains the usage of timers and state:
Stateful processing with Apache Beam
Timely (and Stateful) Processing with Apache Beam
Hi I have been asked to maintain a Delphi 5 based program for someone, and the program is using a timer object to tick every 50 milli-seconds and upon each time-up it runs the block of single threaded code. I am just wondering, what would happen if the time taken to execute this block of code is longer than the timer tick interval, would this be bad? For example could it cause problems like access violation? How does Delphi handle this kind of situation by default? Thanks a lot.
The critical part of this question is :
... what would happen if the time taken to execute this block of code is longer than the timer tick interval, would this be bad?
It's not great, but it's not a show stopper and it certainly cannot cause access violations. Delphi's TTimer is implemented using the WinAPI SetTimer function.
You might naively think that if your timer's handler took longer than the interval to process that the timer would continue to pile up messages in the message queue and your program would effectively lock up with a flood of timer messages that had no hope of ever all being processed. Thankfully, this isn't exactly how timers work. The documentation can shed some light.
WM_TIMER message
The WM_TIMER message is a low-priority message. The GetMessage and PeekMessage functions post this message only when no other higher-priority messages are in the thread's message queue.
Now, there isn't really a concept of "high" and "low" priority messages in a windows application, and while this statement is a little ambiguous we can take the context to mean that WM_TIMER is a message that is not posted to the application's message queue but rather generated in response to a GetMessage or PeekMessage call when a timer has been set with SetTimer, when that timer's interval has elapsed, and when there are no other messages already in the queue.
So, while the timer interval may elapse during your handler's processing, any other messages that come in while this is happening will still enter the queue normally and will be processed once your handler completes. Only once the queue has been emptied again will another WM_TIMER message be generated.
The timer events, therefore, will execute either at the rate of the tick interval or as fast as your application can process them, whichever ends up being longest. If you do have timer messages coming in too quickly, however, and your timer handler's processing time is long then your application's responsiveness can suffer. It won't become unresponsive, but all other message processing will be restricted to being processed at the interval of your timer's event handler execution time. This can make your application feel sluggish.
Example
To demonstrate, create a new forms application and add a TTimer component with an interval set to 10. Then attach this handler :
procedure TForm1.Timer1Timer(Sender: TObject);
begin
sleep(200);
end;
While the program is running, try moving the window around. What we have done is to essentially quantize the application's message processing to a 200ms interval (the duration of the timer's event handler execution).
Ticks of the timer do not interrupt your code.
Timer ticks are delivered in the form of window messages. Window messages can only arrive when you check the message queue for new messages. That happens automatically when your timer event handler returns and your program resumes its event loop, but you can trigger it explicitly by calling Application.ProcessMessages. Don't call that, though; it seldom solves problems in the long run.
If you don't check the message queue in your timer-tick handler, then your handler will never start running a second time while it's still handling a previous tick.
Even if you do check the queue, all that happens is that the tick handler will be called recursively. After all, it's all running in a single thread. Recursive timer handling probably isn't what you want to happen, though, so I'll again counsel against checking for messages in a message handler.
Furthermore, timer messages can never "pile up" if your timer handler takes a long time to run. Timer messages are "fake" in that they don't actually get added to the message queue at regular intervals. Instead, the OS will synthesize a timer message at the time your program checks the queue for more messages. If there are no higher-priority messages in the queue, and the timer interval has elapsed, then the OS will return a wm_Timer message. If you don't check for more messages, then there will be no timer message in the queue. In particular, there will not be multiple timer messages in the queue.
Further reading:
Stack Overflow: How does the message queue work in Win32?
Dr. Dobbs: Inside the Windows Messaging System
I am running workflow on the AWS console. I saw there is a Event Type called "TimerFired" in the console. What does it do?
TimerFired event is generated when timer scheduled through StartTimer decision fires. It also causes a new decision being scheduled to give decider chance to handle the timer event.
Some of my observations on researching about flow framework are:
#Signal starts executing in the decider replay once the signal is received. #Signal method is executed in all the future replays of the same workflow. (Once signal is received, on each replay the decider executes #Signal). #Asynchronus methods should not be long running tasks, as decider will schedule Activity Tasks only after all #Asynch methods have completed execution.
Are my observations correct ? If yes: then what if in the same workflow I want a signal, which performs some task and then stop executing for future replays. Such as a pause signal: user might pause and resume a workflow multiple times.
Another problem is: How are the following types of cases handled by flow: A decider times out, and meanwhile two events come: Cancel workflow and Activity Completed. How does decider figure out that they are related and if cancellation is done, then do not responds to ActivityComplatedEvent.
It helps to not think about workflow behavior in terms of replay. Replay is just a mechanism for recovering workflow state. But when workflow logic is written it is not really visible besides the requirement of determinism and that workflow code is asynchronous and non blocking. So never think about replay when designing your workflow logic. Write it as it is a locally executing asynchronous program.
So when replay is not in a way the #Signal is just a callback method that executes once per received signal. So if you invoke some action from the #Signal method then it is going to execute once.
As for second question it depends on the order in which cancellation and activity completion are received. If cancellation is first then cancellation is delivered to a workflow first which might cause the cancellation of the activity. Cancellation is actually blocking waiting for activity to cancel. Completion of activity (which is the next event) unblocks the cancellation for it. If completion is second then activity completes and whatever follows it is cancelled by the next event. In majority of cases the result is exactly the same that activity completion is received but all logic after that is cancelled.
i want implement a windows services scheduled task.
I already created windows service. In a service i have implemented a timer.The timer is initialized at class interval. The timers interval is set in the start method of service and also it is enabled in the start method of the service. After timers elapsed event is fire i have done some actions.
My problem is that, i am in a dilemma. Lets say the action i have done in Elapsed event, lets say take one hour and the timers interval is set to half an hour. so there are chances that even if the previous call to elapsed event has not ended new call to elapsed event will occur.
my question will there be any conflict or is it ok or shall i use threads.
please give some advice
You can stop the timer at the begining of your long running method, then start it again at the end:
Timer.Change(Timeout.Infinite, Timeout.Infinite)
// do long task ...
Timer.Change(dueTime,period)