The disposal route of timed task and treating apparatus
Technical field
Embodiments of the present invention relate to computer realm, and more specifically, embodiments of the present invention relate to disposal route and the treating apparatus of timed task.
Background technology
The embodiments of the present invention that be intended to for stating in claims this part provide background or context.Description is not herein because be included in just admit it is prior art in this part.
Timed task is to carry out in the time of agreement the work of having mapped out.In Linux, often use cron and served this work, wherein, adopt crontab to carry out timing task management.
Summary of the invention
Although adopt crontab can realize the management to timed task, but, while adopting crontab to manage timed task, in the performance history of a project, certain service of having nothing for it but needs regularly to carry out and writes a large amount of codes, causes thus the construction cycle of project to extend, the cost of development of project increases, and, adopting crontab, maintenance work is very heavy.
For this reason, be starved of a kind of disposal route and device of improved timed task, to address the aforementioned drawbacks.
In the present context, embodiments of the present invention expectation provides a kind of disposal route and treating apparatus of timed task.
In the first aspect of embodiment of the present invention, a kind of disposal route of timed task is provided, comprising: after service starts, be read as the timing task information of timed task configuration pending in described service; According to described timing task information, the proxy class of timed task described in dynamic creation; Register described proxy class to scheduler, and configure the trigger of described timed task; When the trigger condition of described trigger meets, under the scheduling of described scheduler, carry out described timed task.
In one embodiment of the invention, described trigger is associated with the implementation strategy of described timed task; Described method also comprises: by revising described timing task information, while upgrading the implementation strategy of described timed task, upgrade described trigger.
In another embodiment of the present invention, described method also comprises: when the described timed task of the interim execution of needs, by specified interface, receive calling described proxy class.
In yet another embodiment of the present invention, by revising described timing task information, the manner of execution of described timed task can dynamic change.
In another embodiment of the present invention, the manner of execution of described timed task is the method in Spring bean.
In the second aspect of embodiment of the present invention, a kind for the treatment of apparatus of timed task is provided, comprising: read module, after starting in service, is read as the timing task information of timed task configuration pending in described service; Creation module, for according to described timing task information, the proxy class of timed task described in dynamic creation; Registering modules, for registering described proxy class to scheduler, and configures the trigger of described timed task; Execution module, while meeting for the trigger condition when described trigger, under the scheduling of described scheduler, carries out described timed task.
According to the disposal route of the timed task of embodiment of the present invention and treating apparatus, can make project development personnel can reach the introducing of timed task treating apparatus by simple configuration.Compare with the technical scheme that available technology adopting crontab manages timed task, project development personnel are without writing a large amount of codes, thereby reduced significantly the development difficulty of project, shortened the construction cycle of project, and reduced the development cost of project.In addition, follow-up maintenance has also been simplified.
summary of the invention
The inventor finds, when available technology adopting crontab manages timed task, in the performance history of a project, certain service of having nothing for it but need to be carried out timed task and write a large amount of codes, thereby increased the cost of development of project, extended the construction cycle of project.And if managed timed task as a module, meet the establishment, modification of timed task, the demand such as call temporarily, in contrast to available technology adopting crontab concerning the management of timed task, by greatly reduce project development time, reduce later maintenance cost.
Based on above-mentioned analysis, ultimate principle of the present invention is: timed task is managed as a module, and for example this module can be a timed task plug-in unit, can be by reference, and timed task plug-in unit is joined in project.After service starts, this timed task plug-in unit is read as the timing task information of timed task configuration pending in described service, for pending timed task dynamic creation proxy class in service, the proxy class of establishment is registered to scheduler, and configure the trigger of this timed task, satisfied when the trigger condition of this trigger, under the scheduling of scheduler, carry out this timed task.
Therefore, in the performance history of project, no longer need developer need to carry out timed task for certain service and write unnecessary code, greatly reduce the cost of development of project, shortened the construction cycle of project.And follow-up maintenance work has also been simplified.
After having introduced ultimate principle of the present invention, lower mask body is introduced various non-limiting embodiment of the present invention.
application scenarios overview
First with reference to figure 1, Fig. 1 schematically shows the environment that can implement therein according to embodiment of the present invention.This environment is for example a health products selling system, and as shown in Figure 1, this environment comprises 4 services altogether: (1) online service: be responsible for commodity displaying, sale etc.; (2) offline service: be responsible for operation under line, as captured price, logistics information etc.; (3) cache service: be responsible for cache search etc.; (4) ms service: background service, be responsible for the configuring maintenance of commodity data, system data etc.Timed task plug-in unit can be disposed in the respective server of respective service.Each service can have different one or more servers, or a plurality of services can be shared a server, more generally, can be that a server is all shared in all services.After system starts, timed task plug-in unit is understood the configuration in automatic reading database TB_CRON, automatically creates the proxy class of timed task, and trade mark agency class is to scheduler, the trigger of configuring timing tasks, when the trigger condition of trigger meets, under the scheduling of scheduler, carry out timed task.It is pointed out that database TB_CRON can be positioned on the server of ms service, also can be positioned on the server of other three services.More generally, database TB_CRON also can be positioned at one independently on server.It will be understood by those skilled in the art that the applied environment shown in Fig. 1 is only the example that embodiments of the present invention can be achieved therein.Scope of the present invention is not subject to the restriction of this any aspect of environment.
Applied environment based on shown in Fig. 1, in an exemplary application scene, system manager can be provided by ms by the timing task management module providing, and unifies intuitively the timed task of each service of global administration.When system manager is provided by ms by the timing task management module providing, in Update Table storehouse after the configuration of timed task (timing task information), timed task plug-in unit can read the variation of timed task automatically, and revises manner of execution, strategy of timed task etc.When a timed task of the interim urgent execution of needs, also can serve by ms the timing task management module providing, by specified interface, call timed task plug-in unit, with the interim urgent timed task of carrying out.According to the present invention, complete above these operations, code that almost completely need not be unnecessary, only does simple configuration, maintains easily.
illustrative methods
Below in conjunction with the application scenarios of Fig. 1, be described with reference to Figure 2 according to the process flow diagram of the disposal route for timed task of exemplary embodiment of the invention.It should be noted that above-mentioned application scenarios is only to illustrate for the ease of understanding spirit of the present invention and principle, the present invention is unrestricted in this regard.On the contrary, any scene that embodiments of the present invention can be applied to be suitable for.
Fig. 2 schematically shows according to the process flow diagram of the disposal route of the timed task of the embodiment of the present invention.As shown in Figure 2, in the present embodiment, for example, specifically can comprise the following steps:
Step 201, after service starts, be read as the timing task information of timed task configuration pending in described service;
Step 202, according to described timing task information, the proxy class of timed task described in dynamic creation;
Step 203, register described proxy class to scheduler, and configure the trigger of described timed task;
Step 204, when the trigger condition of described trigger meets, under the scheduling of described scheduler, carry out described timed task.
The disposal route of timed task shown in Fig. 2 is the performed concrete steps of timed task plug-in unit in the embodiment of the present invention.In one embodiment, the main principle that realizes of this timed task plug-in unit is when system is moved, and by ASM direct control bytecode (class) file, thereby dynamically derives enhancing subclass when operation, and this enhancing subclass is the proxy class of timed task.Wherein, ASM only controls framework for the present embodiment as an optional bytecode, and those skilled in the art can adopt as required alternate manner when specific implementation.
Fig. 3 schematically shows the core code class figure according to the timed task plug-in unit of the embodiment of the present invention, can be used as the reference that those skilled in the art realize timed task plug-in unit.As shown in Figure 3, in one embodiment, after serving startup, timed task plug-in unit can be the timing task information that timed task pending in service configures in reading database, then according to the proxy class of this timed task of timing task information dynamic creation.This constructive process for example can comprise by the proxy class of ClassGenerator dynamic creation timed task: the subclass of AbstractJob class.Wherein, ClassGenerator can be by calling EnhanceJobClassAdapter, DoJobMethodAdapter, InitMethodAdapter comes dynamic construction to go out the subclass AbstractJob $ EnhancedByASM $ * * * of AbstractJob.In the constructive process of proxy class, each timed task can be according to a unique coding id of the different generation of the configuration of self, and the title of the proxy class generating for this timed task can comprise this unique id.This proxy class is the subclass of AbstractJob, has rewritten abstract method doJob, in doJob method, for example can get quoting of bean by Spring container, then calls the method configuring in bean.
Again as shown in Figure 3, generate after the proxy class of timed task, can this proxy class be registered on scheduler (Scheduler) by JobManager, and configure corresponding trigger (Trigger) for timed task, when the trigger condition of trigger meets, under the scheduling of scheduler (Scheduler), carry out timed task.
Wherein, the timed task that this scheduler schedules is all, and for example, this scheduler can be produced by system.
Known with reference to figure 2, Fig. 3, the disposal route of the timed task of the embodiment of the present invention, when operation, dynamic creation class, can avoid the efficiency expense of using reflection to bring.In addition, the trigger configuring for timed task can for example, be associated with the implementation strategy (time) of timed task, while upgrading the implementation strategy of timed task by modification timing task information like this, can upgrade trigger, for example, can directly replace Trigger.
At some, implement in scene, the timed task of an on-line operation, may need to adjust execution time, frequency etc.Use the timed task plug-in unit of the embodiment of the present invention, can not restart under the prerequisite of online service, met above these demands.In embodiment, by revising timing task information, the manner of execution of timed task gets final product dynamic change.Wherein the manner of execution of timed task for example refers to the method in Spring bean.
In addition in an embodiment, when the interim urgent execution timed task of needs, can also timed task be called by the specified interface of timed task plug-in unit.In other words, when needs are interim urgent while carrying out certain timed task, timed task plug-in unit can pass through specified interface, receives the calling of proxy class, thereby carries out corresponding timed task.This is very useful in actual performance history, especially, to this locality debugging, test environment test, has given very large convenience.For example, timed task has a variety of, and for example the commodity buffer memory in system needs periodic refreshing, can configure under normal circumstances each hour to refresh once.But the generation due to various special circumstances need to refresh commodity buffer memory sometimes at once, this demand can be by system manager the specified interface by timed task plug-in unit call timed task and carry out immediately (not according to predetermined time plan, carrying out).
Accompanying drawing explanation
By reference to accompanying drawing, read detailed description below, above-mentioned and other objects of exemplary embodiment of the invention, the feature and advantage easy to understand that will become.In the accompanying drawings, in exemplary and nonrestrictive mode, show some embodiments of the present invention, wherein:
Fig. 1 schematically shows the environment that embodiment of the present invention can be implemented therein;
Fig. 2 schematically shows according to the process flow diagram of the disposal route of the timed task of the embodiment of the present invention;
Fig. 3 schematically shows the core code class figure according to the timed task plug-in unit of the embodiment of the present invention;
Fig. 4 schematically shows health products selling system timing task management original list figure according to an embodiment of the invention;
Fig. 5 schematically shows health products selling system configuring timing tasks page figure according to an embodiment of the invention;
Fig. 6 schematically shows health products selling system timed task according to an embodiment of the invention and carries out log output example figure;
Fig. 7 schematically shows the structural drawing of the treating apparatus of timed task according to an embodiment of the invention;
Fig. 8 schematically shows the structural drawing of the treating apparatus of timed task according to another embodiment of the present invention;
Fig. 9 schematically shows the structural drawing of the treating apparatus of timed task according to another embodiment of the present invention.
In the accompanying drawings, identical or corresponding label represents identical or corresponding part.
Embodiment
Below with reference to some illustrative embodiments, principle of the present invention and spirit are described.Should be appreciated that providing these embodiments is only used to make those skilled in the art can understand better and then realize the present invention, and not limit the scope of the invention by any way.On the contrary, it is in order to make the disclosure more thorough and complete that these embodiments are provided, and the scope of the present disclosure intactly can be conveyed to those skilled in the art.
One skilled in the art will appreciate that embodiments of the present invention can be implemented as a kind of system, device, equipment, method or computer program.Therefore, the disclosure can specific implementation be following form, that is: hardware, software (comprising firmware, resident software, microcode etc.), or the form of hardware and software combination completely completely.
A kind of disposal route and treating apparatus of timed task have been proposed according to the embodiment of the present invention.
In this article, it will be appreciated that, related term " timed task " is illustrated in the time of agreement and carries out the work mapped out.Wherein this timed task can be write by arbitrary computer programming language, can in arbitrary computer program running environment, move, and is not limited to Linux.In addition, any number of elements in accompanying drawing is all unrestricted for example, and any name is all only for distinguishing, and does not have any limitation.
Below with reference to some representative embodiments of the present invention, explain in detail principle of the present invention and spirit.
In embodiment, an engineering (a for example service) is if need to introduce timed task plug-in unit, and technician needs the operation of following several parts.It should be noted that, be only an instantiation to the introducing of timed task plug-in unit in this example, and those skilled in the art, when specific implementation, can adopt with reference to the enforcement of this example other concrete configuration mode.Introducing in this example timed task plug-in unit can comprise:
(1) jar bag is joined in engineering classpath;
(2) newdata storehouse table TB_CRON in database;
(3), in the SqlMapConfig.xml of engineering, for example add the configuration of following his-and-hers watches TB_CRON:
<mapper?resource="com/netease/cron/CronDaoImpl.xml"/>;
(4) for example in the configuration file of Spring, add configuration as follows:
<context:annotation-config/>
<context:component-scan?base-package="com.netease.cron"/>;
(5), if need log, can in log4j.properties, add for example following configuration of cron.log:
log4j.category.cron.log=INFO,A3
log4j.appender.A3=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A3.File=log/cron.log
log4j.appender.A3.DatePattern='.'yyyy-MM-dd
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=%d-%c[%t]%-5p%c%x%l-%m%n。
In this example, only need above 5 steps, can complete the introducing of timed task plug-in unit, can be configured to timed task to the method in any the Spring bean in service afterwards.
May there are a lot of timed tasks in a service, how managing these timed tasks will certainly become a loaded down with trivial details thing gradually along with the development of service.According to embodiments of the invention, the management of a plurality of timed tasks is mainly undertaken by timed task plug-in unit, user can check, manage timed task very clearly on management backstage, thus the convenient centralized management to timed task.
Fig. 4 schematically shows health products selling system timing task management original list figure according to an embodiment of the invention.
As shown in Figure 4, can intuitively, not relate to bottom specific code each timed task is managed concentratedly, user does not need to know that timed task is these details how to carry out, is therefore very easy to the maintenance management of timed task.
Fig. 5 schematically shows health products selling system configuring timing tasks page figure according to an embodiment of the invention.
As shown in Figure 5, can intuitively, not relating to bottom specific code is configured each timed task.
In addition, in an embodiment, timed task can be input to log in cron.log after carrying out.For example, Fig. 6 schematically shows health products selling system timed task log output example figure according to an embodiment of the invention.
From the above, by the embodiment of the present invention, project development personnel just can introduce timed task plug-in unit by simple configuration, without writing a large amount of codes, have reduced significantly the development difficulty of project, have shortened the construction cycle of project.
And further, when system is moved, dynamic creation class, avoids the system overhead that uses reflection to bring.
Further, in an embodiment, by revising timing task information, the manner of execution of timed task can dynamic change, when upgrading the implementation strategy of timed task by modification timing task information, upgrade the trigger of timed task, can realize the implementation strategy of upgrading at any time timed task, without restarting online service.
Further, in an embodiment, also facilitated the centralized management of timed task and configuration.
example devices
After having introduced the method for exemplary embodiment of the invention, next, with reference to 7 couples, figure according to exemplary embodiment of the invention, for the treating apparatus of timed task, be described.
Fig. 7 schematically shows the structural drawing of the treating apparatus of timed task according to an embodiment of the invention.The treating apparatus of this timed task is for example timed task plug-in unit described above.
As shown in Figure 7, in the present embodiment, the treating apparatus of timed task can comprise:
Read module 701, after starting in service, is read as the timing task information of timed task configuration pending in described service;
Creation module 702, for according to described timing task information, the proxy class of timed task described in dynamic creation;
Registering modules 703, for registering described proxy class to scheduler, and configures the trigger of described timed task;
Execution module 704, while meeting for the trigger condition when described trigger, under the scheduling of described scheduler, carries out described timed task.
Fig. 8 schematically shows the structural drawing of the treating apparatus of timed task according to another embodiment of the present invention.As shown in Figure 8, in the present embodiment, the treating apparatus of timed task comprises outside above-mentioned read module 701, creation module 702, Registering modules 703 and execution module 704, also comprise: update module 801, for passing through to revise described timing task information, while upgrading the implementation strategy of described timed task, upgrade described trigger, wherein said trigger is associated with the implementation strategy of described timed task.
Fig. 9 schematically shows the structural drawing of the treating apparatus of timed task according to another embodiment of the present invention.As shown in Figure 9, in the present embodiment, the treating apparatus of timed task comprises outside above-mentioned read module 701, creation module 702, Registering modules 703 and execution module 704, also comprises: interface module 901, for when needs are carried out described timed task temporarily, receive calling described proxy class.
In one embodiment, by revising described timing task information, the manner of execution of described timed task can dynamic change.
In one embodiment, the manner of execution of described timed task is the method in Spring bean.
In sum, according to the disposal route of the timed task of embodiment of the present invention and treating apparatus, according to the timing task information of the timed task configuration for pending in service, the proxy class of this timed task of dynamic creation, and register this proxy class to scheduler, configure the trigger of this timed task, when the trigger condition of this trigger meets, by scheduler schedules, carry out this timed task, can make developer pass through simple configuration and just can in project, introduce timed task plug-in unit, without writing a large amount of codes, thereby reduced significantly the development difficulty of project, shortened the construction cycle of project.
Further, when system is moved, dynamic creation class, avoids the system overhead that uses reflection to bring.
Further, in embodiment, can realize the implementation strategy of changing at any time timed task, without restarting online service.
Further, the conveniently centralized management to timed task in embodiment.
Although it should be noted that some modules or the submodule of the treating apparatus of having mentioned timed task in above-detailed, this division is only exemplary not enforceable.In fact, according to the embodiment of the present invention, the feature of above-described two or more modules and function can be specialized in a module.Otherwise, the feature of an above-described module and function can Further Division for to be specialized by a plurality of modules.
In addition, although described in the accompanying drawings the operation of the inventive method with particular order,, this not requires or hint must be carried out these operations according to this particular order, or the operation shown in must carrying out all could realize the result of expectation.Additionally or alternatively, can omit some step, a plurality of steps be merged into a step and carry out, and/or a step is decomposed into a plurality of steps carries out.
Although described spirit of the present invention and principle with reference to some embodiments, but should be appreciated that, the present invention is not limited to disclosed embodiment, the division of each side is not meant that to the feature in these aspects can not combine to be benefited yet, and this division is only the convenience in order to explain.The present invention is intended to contain interior included various modifications and the equivalent arrangements of spirit and scope of claims.