The kbmMW Scheduler schedules jobs. The jobs may be one time or recurrent jobs.
Some jobs may not be critical related to the exactness of their executing time on the mSec (like generating a report, doing a backup, importing some data and other such housekeeping and maintenance), while others may require as accurate as possible execution, for example sampling data, screen grabbing or stuff like that.
The scheduler can do both.
When defining a recurrent job, you typically also tells it with which interval it should run. The scheduler contains multiple fluent expressions to define the interval, like EveryMinute, EveryHour, EveryDay, EverySecond, EveryMSecond etc. which all takes floating point values.
Hence you can easily schedule (and activate) a job to run every 1.5 hour, by writing this:
kbmMW default assumes that jobs scheduled with an interval less than 2 seconds should be considered “precise” jobs, while others should be considered “relaxed” jobs.
A “Relaxed” job is a job, that is being assigned a thread from a thread pool, when it’s about to execute. If there are no threads available, the job is put on hold until a thread is free.
Thus Relaxed jobs often share one or a few threads to run, and thus are very gentle to the system resources. But the downside is that they may not execute completely on time. Their execution may be delayed a few mSecs to even minutes or hours, depending on the available number of threads in the relaxed thread pool, and the load of the scheduled relaxed jobs. Typically any delays will be minimal, but there is no guarantee.
A “Precise” job is a job that from time of defining the job, will have its own thread assigned to it, and thus will run independently of all other jobs.
As mentioned kbmMW automatically decides, upon scheduling time, which mode the job is to take, relaxed or precise. But you can override this manually by specifically stating either Relaxed or Precise as part of the fluent expression. E.g.
// Schedule a job in its own thread, running every 1.5 hour. Scheduler.Schedule(somejob).EveryHour(1.5).Precise.Activate; // Schedule a job to be run every 100 msecs, from a shared thread from the relaxed threadpool. Scheduler.Schedule(somejob).EveryMSecond(100).Relaxed.Activate;
In a previous Scheduler Tidbit blog post, I mentioned the new CronTab scheduling style. Since UNIX cron only has an interval resolution of 1 minute, scheduling such a job using kbmMW’s scheduler also will predefine the use of a one minute interval, and thus all cron jobs will default be scheduled as relaxed jobs.
However you can choose a different interval resolution by using any of the Every…. fluent expressions before the Cron expression. Eg.
// This will schedule a job to be run every minute from 5:00 to 5:59 every day. // The job will default be considered a relaxed scheduled job. Scheduler.Schedule(somejob).Cron('* 5 * * *').Activate;
// This will schedule a job to be run every second from 5:00 to 5:59 every day. // The job will default be considered a precise scheduled job. Scheduler.Schedule(somejob).EverySecond(1).Cron('* 5 * * *').Activate;
If you state the Every… expression after the Cron expression, it will add to the default 1 minute cron schedule. Eg.
// This will schedule a job to be run every 1.5 minute from 5:00 to 5:59 every day. Scheduler.Schedule(somejob).Cron('* 5 * * *').EverySecond(30).Activate;
What happens if a scheduled repeating job is taking longer time to finish than expected, thus missing next scheduled interval?
Depending on the amount of the delay and criterias for the schedule, the next execution of the job may either be delayed or completely cancelled, if the window of running the job has passed.
The kbmMW Scheduler contains some nice statistics to follow, which can be used to track if that happens, how often and provides some figures that tells about the time of executing.
The statistics can be obtained directly from the scheduled event instance you receive as result when you use Scheduler.Schedule…. But you can also iterate over all scheduled events. The following example iterates through the scheduled events, triggered by a TTimer, to be able to update a TMemo with statistics,.
procedure TForm1.Timer1Timer(Sender: TObject); var i:integer; lst:TList<IkbmMWScheduledEvent>; e:IkbmMWScheduledEvent; begin if not chbStats.Checked then exit; lst:=Scheduler.Events.Events.BeginRead; try mStats.Clear; for i:=0 to lst.Count-1 do begin e:=lst.Items[i]; mStats.Lines.Add(inttostr(i)+') ID='+e.ID+' Name='+e.Name+' Runs='+inttostr(e.Runs)+' Stalls='+inttostr(e.Stalls)+' TotalStalls='+inttostr(e.TotalStalls)+' LastRunTaken='+inttostr(e.LastRunTaken)+' TotalTimeTaken='+inttostr(e.TotalTimeTaken)); end; finally Scheduler.Events.Events.EndRead; end;
The scheduled events have a number of properties to access for statistics:
- Runs indicate how many times the event has run in total.
- Stalls indicate how many times the event should have run, but was unable to due to either still running or the thread pool exhausted at the time, since last time a successful run happened.
- TotalStalls counts total number of stalls.
- LastRunTaken shows how many mSecs last run lasted.
When TotalStalls increase fairly regularly it indicates that either too many events are defined compared to size of the relaxed thread pool (increase RelaxedPoolSize which is default 20), or some events are taking longer time than expected to run and thus can’t be timely rescheduled.
Next version of kbmMW will continue to improve on the already very powerful job scheduler built into the framework.
Now UNIX CronTab like scheduling can be made. Examples:
// At the 8 and 9 minutes past the hour Scheduler.Schedule(OnTestEvent).Cron('8,9 * * * * *').NamedAs('cron1').Activate; // At the exact minute of the hour when this event is scheduled Scheduler.Schedule(OnTestEvent).Cron('? * * * * *').NamedAs('cron2').Activate; // Every 5 minutes Scheduler.Schedule(OnTestEvent).Cron('*/5 * * * * *').NamedAs('cron3').Activate; // Every minute at Monday between 8:20 and 8:30 Scheduler.Schedule(OnTestEvent).Cron('20-30 8 * * Mon *').NamedAs('cron4').Activate; // Every second minute at Monday between 8:20 and 8:30 Scheduler.Schedule(OnTestEvent).Cron('20-30/2 8 * * 1 *').NamedAs('cron5').Activate; // Every second minute at Monday and Tuesday between 8:20 and 8:30 Scheduler.Schedule(OnTestEvent).Cron('20-30/2 8 * * 1,2 *').NamedAs('cron6').Activate; // Every second minute at Monday and Tuesday, in March between 8:20 and 8:30 Scheduler.Schedule(OnTestEvent).Cron('20-30/2 8 * mar 1,2 *').NamedAs('cron7').Activate;
kbmMW supports an extended CronTab format which includes these features.
- Optional year
- List of values
- Optional textual day of week
- Optional textual month name
- Start time indicator
Syntax for cron: minute hour day month dayofweek year
Year is optional.
Thus 5 or 6 space separated values must be given.
Each of the values can be:
* indicating any value.
? indicating value at schedule time
1,2,3.. list of values
1,4-5,7… list of values
1-5 range of values
*/2 interval… every 2
3-15/3 interval… every 3 starting at 3, ending at 15
Interval values can be used for minute, hour, day, month and year values.
Month can be given as a value 1..12 or text jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec.
DayOfWeek can be given as value 1 (monday)..7 (sunday) or mon, tue, wed, thu, fri, sat, sun.
All the other fluent expression supported by kbmMW’s scheduler, like InitialDelay, Starting, Ending, Occurance etc. can be added after the Cron statement.
In addition to Cron, a number of fluent methods like AtYears, AtMonths, AtDays, AtHours, AtMinutes, AtSeconds has been added to allow setting up Cron like schedules without using the Cron string format.
kbmMW contains a pretty powerful job scheduler which in next release of kbmMW becomes even more powerful.
It can execute things in a regular interval under various conditions.
But it can also be used for running code asynchronously without having manually to create threaded code.
Scheduler.RunNow( procedure begin somelongrunningmethod; end);
The procedure will be executed immediately in a different thread, without blocking the executing of the current thread.
But sometimes you want the result of the asynchronous executing to be output to the GUI. And kbmMW’s scheduler now contains easy access to doing that.
var res:sometype; begin Scheduler.Run( procedure begin res:=somelongrunningfunction; end) .SynchronizedAfterRun( procedure begin Memo1.Lines.Add(res); end) .Activate(true);
In this case, somelongrunningfunction is executed in a seperate thread, and its return value is placed in res. When the result is back, the code in SynchronizedAfterRun is executed, this time synchronized with the main thread, which makes it GUI safe.
Super easy way to execute async with support for updating GUI afterwards.
We are happy to announce v5.01.00 of our popular middleware for Delphi
Yet again kbmMW continues to set the bar for what an n-tier product must
be capable of in the real world!
We hope you will take the time to test this Beta release in your setup
and report back regression errors to us by email at
Keywords for this release:
– RAD Studio 10.2 Tokyo support including Linux support (in beta).
– Huge number of new features and improvements!
– New Smart services and clients for very easy
publication of functionality and use from clients
and REST aware systems without any boilerplate
– New ORM OPF (Object Relational Model Object Persistence Framework)
to easy storage and retrieval of objects from/to databases.
– New high quality random functions.
– New high quality pronouncable password generators.
– New support for YAML, BSON, Messagepack in addition to
JSON and XML.
– New Object Notation framework which JSON, YAML, BSON and
Messagepack is directly based on, making very easy
conversion between these formats and also XML which now
also supports the object notation framework.
– Lots of new object marshalling improvements, including
support for marshalling native Delphi objects to and from
YAML, BSON and Messagepack in addition to JSON and XML.
– New LogFormatter support making it possible to customize
actual logoutput format.
– CORS support in REST/HTML services.
– High performance HTTPSys transport for Windows.
– Focus on central performance improvements.
– Pre XE2 compilers no longer officially supported.
– Bug fixes
Look at end of post for detailed list of additions, changes and fixes.
Professional and Enterprise Edition is available for all with a current
If your SAU has run out, please visit our shop to extend it with another
CodeGear Edition is available for free, but only supports a specific
Delphi/Win32 SKU, contains a limited feature set and do not include source.
Please visit https://portal.components4developers.com to download.
kbmMW is the premiere n-tier product for Delphi, C++Builder and FPC
on .Net, Win32, Win64, Linux, Java, PHP, Android, IOS, embedded devices,
websites, mainframes and more.
Please visit http://www.components4developers.com for more information about kbmMW.
Components4Developers is a company established in 1999 with the purpose
of providing high quality development tools for developers and
enterprises. The primary focus is on SOA, EAI and systems integration
via our flagship product kbmMW.
kbmMW is a portable, highly scalable, high end application server and
enterprise architecture integration (EAI) development framework for
Win32, ..Net and Linux with clients residing on Win32, .Net, Linux,
Unix, Mainframes, Minis, Embedded and many other places.
It is currently used as the backbone in hundreds of central systems, in
hospitals, courts, private, industries, offshore industry, finance,
telecom, governements, schools, laboratories, rentals, culture
institutions, FDA approved medical devices, military and more.
5.00.00 Beta Feb 19 2017
Important notes (changes that may break existing code)
* Officially now only supporting XE2 and forward. Support for pre XE2
may not be complete.
– Added support for RAD Studio 10.2 Tokyo, including Linux support
(currently in beta)
Include new sample simpleinvocation LinuxServer demo.
– Officially now only supporting XE2 and forward. Support for pre XE2
may not be complete.
– Added RemoteLocationsPrimaryPart to
TkbmMWAuthorizationTransportConstraint (default false).
If set to true, will only compare primary part of
Primary part is defined by everything up til the last ‘:’. In most cases
that will mean the IP address without the port number.
– Added IkbmMWLogFormatter interface to kbmMWLog.
– Added TkbmMWStandardLogFormatter to kbmMWLog. It contains vastly
improved features for
controlling the look and layout of the log output. Its the default
used log formatter.
– Added TkbmMWSimpleLogFormatter to kbmMWLog. It outputs a log with only
type (info/warning/error/fatal etc) and log string.
– Added LogFormatter property to IkbmMWLogManager. It can be set to a
custom log formatter.
– Added mwldtRaw log data type and methods LogRaw to IkbmMWLog. It
allows for logging
bytes or strings without interpretation/reformatting.
– Added global SystemLog instance which will always output to system log
It is used as a fallback in case the regular log system excepts.
– Added support for marshalling/unmarshalling named enum values via the
Its useful when an enum value cant be used as an identifier or its
value should be different
from its presented name.
– Added ExceptOnUnknownType to TkbmMWCustomRTTIMarshal. It controls if
to throw an exception
if an unknown type is accessed or not (default true).
– Added support for marshalling/unmarshalling TDictionary<K,V> types.
– Added support for correctly instantiating classes in unmarshalling
that contains a capacity
– Added Run methods to TkbmMWScheduler. It allows for one time run and
forget scheduled async
code. They are used in the same way as the Schedule methods, but sets
Occurs to mwsoRun.
The job will be automatically unscheduled after the run.
– Added overloaded DelayInitial(const AInitialDelaySecs:double) to
It allows for directly giving an initial delay in seconds. It supports
– Added Clear to TkbmMWScheduledEvents (scheduler.Events) to allow for
clearing all scheduled
events. Running events will be allowed to finish.
– Added GetNameSpaceByURI, DefaultNameSpace, DefaultNameSpaceURI to
– Added SearchRelNodeByID, SearchNodeByID, SearchRelNodeByGivenName,
– Added ExpandNameSpace, GivenName, ChildrenByGivenName, ChildByGivenName,
CheckChildByGivenName to TkbmMWDOMXMLNode.
– Added CheckNodeByGivenName, NodeByGivenName to TkbmMWXMLNodeList.
– Added missing ADSDBF support to kbmMWReg.
mwrtoOptionalDelete,mwrtoKeyNotNullDelete and table modifier flags
OIM/OMI (optional insert/modify), OID/ODI (optional insert/delete),
OMD/ODM (optional modify/delete), OI (optional insert),
OD (optional delete), OM (optional modify),
KIM/KMI (insert/modify on key not null), KID/KDI (insert/delete on key
KMD/KDM (modify(delete on key not null), KI (insert on key not null),
KM (modify on key not null), KD (delete on key no null)
They allow for more finegrained definition on when
insert/update/delete should happen.
If one of the optional (OIM,OMD,OID,OI,OD,OM) is given, no exception
will be raised for
the table when an operation did not succeed, and resolving will continue.
If one of the key (KIM,KMD,KID,KI,KM,KD) is given, record resolve
operation will be skipped
for the specific record if key is null.
– Added ReadBOM overloaded methods with default TEncoding option to
– Added overloaded kbmMWTextToString, kbmMWExpectText,
kbmMWGetComputerName functions to kbmMWGlobal.pas.
– Added optional ACallback anonymous function to SendAsyncRequest and
kbmMWClient.pas (WIB only).
It allows for providing an anonymous function that should be called
when an async response
is returned. The function must follow this type:
– Added LOB blob/clob support (user fix) to DOA adapter.
– Vastly improved XSD importer.
– Added generic Object Notation framework for building virtual object
trees consisting of
TkbmMWONNative, TkbmMWONArray and TkbmMWONObject which all are based
– Added YAML parser/generator based on object notation framework.
– Added Messagepack parser/generator based on object notation framework.
– Added BSON parser/generator based on object notation framework.
– Added support for converting XML to and from object notation format via
LoadFromObjectNotation and SaveToObjectNotation functions.
– Added new TkbmMWCustomSmartService and TkbmMWCustomHTTPSmartService
can be selected in the service wizard to create smart services. Smart
supports tagging any function to be exported to a smart client or a
– Updated transports to support marshalling objects via the transport.
– Added several near lock free features to kbmMWGlobal, including
more interlocked functions, lock free array GC fixes, optional
support for TkbmMWMREWLock can switch to TMonitor for reader/writer
– Optimized many algorithms like TkbmMWStringBuilder, TkbmMWGenerateGUID,
TkbmMWGenerateShortGUID and many more to improve performance overall.
– Added many new general purpose functions to kbmMWGlobal.
– Added direct support for salt in hash functions deriving from
– Added support for anonymous functions in async kbmMWClient requests (WIB)
– Added new kbmMWRTTI unit with loads of RTTI functionality.
– Added YAML stream format for kbmMemTable and descendants.
– Added Messagepack stream format for kbmMemTable and descendants.
– Added BSON stream format for kbmMemTable and descendants.
– Added object marshalling to and from YAML.
– Added object marshalling to and from Messagepack.
– Added object marshalling to and from BSON.
– Added ServicePath property to TkbmMWClientIdentity.
– Added MaxLogins to TkbmMWAuthorizationManager which is default 100 to
potential login spam.
– Added support for HTTPSys based transport for Windows. Its specially
for high performance RESTful applications.
– Added AutoRegisterServices method to TkbmMWServer which can be used
locating and registering any service that has the kbmMW_Service
It makes use of the old RegisterService and RegisterServiceByName
– Added LocalSinceEpochMS, UTCSinceEpochMS, Temporenc to TkbmMWDateTime.
is a very compact binary encoding of a datetime value.
– Added FastSetUTC method to TkbmMWDateTime to threadsafely very fast
the UTC value of an already defined TkbmMWDateTime record. The record
MUST be initialized before using FastSetUTC, for example by
– Added ValueToStream, ValueFromStream, ValueToBytes and ValueFromBytes in
TkbmMWCustomRTTIMarshal. Allows for easy marshalling objects to and
from bytes and streams.
– Added ExceptOnUnknownType boolean property to TkbmMWCustomRTTIMarshal.
Controls if an exception should be thrown in case a type cant be
unmarshalled, or it should be silently skipped.
– Added Devart MyDAC support (DMYDAC).
– Added CORS support (Cross Origin-Resource Sharing) in
descendants. (OnCORS event).
– Added PerformOPTIONS and PerformPATCH support to TkbmMWCestomHTTPService.
(OnOptions and OnPatch in TkbmMWEventHTTPService).
– Added REST transport stream format which must be used if non HTTPSys
is used for smart services.
– Added TkbmMWSmartClient which allows simple and easy access to server
side smart functions.
Check simpleinvocation sample.
– Added high quality random functions in new kbmMWRandom unit.
TkbmMWRandomDelphi (32 bit non high quality ramdom!),
TkbmMWRandomSplitMix (64 bit)
TkbmMWRandomXoroshiro128Plus (64 bit), TkbmMWRandomXoroshiro1024 (64 bit),
TkbmMWRandomPCGUInt32 (32 bit), TkbmMWRandomMersenneTwisterUInt32 (32
TkbmMWRandomMersenneTwisterUInt64 (64 bit)
– Added high quality pronouncable password generators in new unit
– Added support for ExchangeType in TkbmMWAMQPClient.
– Added new GC and CloseAndGC methods in TkbmMWAMQPChannel to force
getting rid of
defunct/cached but unused AMQP channels.
– Added IPVersion property to TkbmMWCustomTCPIPIndyClientTransport.
– Split out all lockfree algorithms from kbmMWGlobal to kbmMWLockFree unit
to make kbmMWGlobal smaller, partly due to internal compiler errors.
– Fixed to default not inlining kbmMWMREWLock methods on 32 bit CPUs due to
unstable compiler which can result in F2084/C2491 error combinations.
– Moved all lockfree code to new kbmMWLockFree.pas unit.
– Added TkbmMWLockfreeStack.
– Changed so XE6/XE7 SKU’s do not compile in new kbmMWLockFree contents
due to compiler errors.
– Added kbmMW ORM OPF (Object Relational Modelling Object Persistence
in unit kbmMWORM.pas. Compiles with XE5 and higher. Check new ORM demos.
Currently in beta.
– Added TkbmMWBinaryPasswordGen to kbmMWPassword.pas. Generates random
of any length, suitable for cryptographic use.
– Added functions Year(ADateTime:TDateTime=Math.NaN):word;
– Improved Null handling in kbmMWNullable and TkbmMWDateTime.
– Improved Modified support in kbmMWNullable and TkbmMWDateTime.
– Added functions function OutputToFile(const
function OutputToStrings(const AStrings:TStrings; const
ASynchronized:boolean = true):IkbmMWStringsLogManager;
function OutputToStringsAndFile(const AStrings:TStrings; const
ASynchronized:boolean = true; const
to IkbmMWLog to make it easier to redirect output to some standard
– Added mwddLog to kbmMWGlobal, as a new output destination for kbmMW
Will output to the default logger.
– Fixed and improved TkbmMWCircularBuffer.
– Added kbmMWSmartUtils.pas containing Use class with easy to use
methods to convert arguments and results.
– Removed (mwloProcessThreadInfo,mwloThreadName from TkbmMWLogOption
since its now controlled
by the log formatter.
– Changed Activate method of IkbmMWScheduledEvent to have true as
default argument AValue.
– Changed SetAsDuration and GetAsDuration to use TkbmMWDuration instead
– Added support for checking if path is accessible in
– Changed JSON parser/generator to be based on the object notation
– Improved scalability of FastCGI services to better spread load accross
– Fixed A/V when attempting to use audit file.
– Fixed missing clearing of TkbmMWDuration when parsing duration
strings, resulting in
an incorrect duration value (instead of null) on invalid duration strings.
– Fixed TDuration.Passed(const ADuration:TkbmMWDuration) which didnt
return correct value.
– Fixed deactivating precise scheduled events.
– Fixed events scheduled to start on day, month, week or year boundaries.
– Fixed issues to initial delay in scheduled events.
– Fixed parsing tags crossing line boundaries in XML parser.
– Fixed various namespace issues in XML parser.
– Fixed WIB compilation issue for XE.
– Fixed TkbmMWStreamStore offset bug when writing first segment.
– Fixed potential shutdown issue in Indy10 transports due to Indy10 bug.
– Fixed kbmMWDependency non accumulating memory leaks.
– Fixed NexusDB transport compilation.
– Fixed Autoinc field detection in EDB (ElevateDB) adapter.
We are happy to announce the latest and greatest release of our memory table. Whats new in 7.75.00 Apr 22 2017 -------------------------------- - Fixed bug in TkbmSQLTables.Delete. - Fixed support for AutoUpdateFieldVariables, and ensured that CreateTable will auto remove persistant fields. - Added RecordID:TkbmNativeInt and UniqueRecordID:TkbmNativeInt to TkbmCustomMemTable. - Fixed loading ftByte field data in binary streamformatter. - Added GetRecordFieldModified and SetRecordFieldModified which manages new record field level modified flag. - Added support for parsing anonymous parameters in SQL parser. - Added support for Delphi 10.2 Tokyo including full Linux support. - Added function GetAllFields:TkbmFieldArray to TkbmCustomDeltaHandler. Standard Edition is released with source to holders of an active kbmMemTable Service and Update subscription (SAU). Professional Edition is released with source and additional performance enhancement features to holders of an active kbmMW Pro/Ent Service and Update subscription (SAU). A free CodeGear Edition can be found bundled with kbmMW CodeGear Edition for specific Delphi versions. kbmMemTable supports the following development environments: Delphi 2009 Delphi 2010 RAD Studio Delphi/C++ XE2 RAD Studio Delphi/C++ XE3 RAD Studio Delphi/C++ XE4 RAD Studio Delphi/C++ XE5 RAD Studio Delphi/C++ XE6 RAD Studio Delphi/C++ XE7 RAD Studio Delphi/C++ XE8 RAD Studio Delphi/C++ 10 Seattle RAD Studio Delphi/C++ 10.1 Berlin RAD Studio Delphi/C++ 10.2 Tokyo Lazarus 1.2.4 with FPC 2.6.4 kbmMemTable is the premier high performance, high functionality in memory dataset for Delphi and C++Builder with kbmMemTable Professional topping the scales as being the worlds fastest! If you have an up to date Service and Update (SAU) subscription, then you can immediately visit https://portal.components4developers.com to download the latest kbmMemTable release. If not, please visit our shop at http://www.components4developers.com and extend your SAU with another 12 months.