Background technique
Text file is a kind of important computer documents, and since structure is simple, text file is widely used in record letter
Breath.In order to promote development efficiency, code is reused, introduces template engine (template when generating text
Engine), using the function that can reach logical separation after template engine, make program application logic and page display logic point
It opens, program developer can be allowed to be absorbed in the control of data or reaching for function;And visual designer can then be absorbed in page row
Version allows the page to seem with more profession sense.
To generate a kind of common text with Java language --- for html page.HTML page is being carried out with Java language
When face generates, it is generally the case that developer will not directly write entire html page, but according to certain logic, by one
A html page is blocked into many html templates, then ultimately generates entire html page with the template engine based on Java.
The usually used template engine based on Java has Velocity, FreeMarker etc..Such as Velocity, it permits
Perhaps simple template instruction (template language) is used only only to quote the object defined by Java code in anyone,
And by Java object and template itself in a manner of rendering, a HTML fragment is ultimately generated.When Velocity is applied to Web
When page development, Pages Design personnel can only focus on the display effect of the page, and pay close attention to business by java applet developer
Logic coding.Velocity separates Java code from web page, also provides for the long term maintenance of web site in this way
Convenience.
When needing to render a html page with Velocity, the prior art generally comprises following steps:
1. the variable in entire html page is substituted for the reference of Java object according to Velocity grammer to generate
Velocity template file;
2. successively assembling data required for Velocity template file by Java background logic single thread;
3. the data that Velocity template file and Java background logic are assembled are as rendering method in Velocity
Input executes rendering (render) method in Velocity, and the output for finally obtaining rendering method generates complete HTML page
Face.
It can be seen that the method for the existing generation text based on Java has following disadvantage:
1. if will lead to that entire template file is very long there are many template file content, increase the exploitation maintenance of developer
Cost.
The process that the backstage 2.Java prepares data is single thread, if there are many data that template file needs, the backstage Java
The logic of assembling data takes a long time that all DSRs rendering method could be recalled, therefore the page is raw later
It is very long at the time.
3. the process for rendering text, which still must comply with, successively prepares data again when if server cpu busy percentage is lower
The process of rendering, can not the more efficient cpu resource using server.
Summary of the invention
In view of this, the present invention provides a kind of telescopic method for generating text, concrete scheme is:By a large form
File resolves into multiple subtemplate files, and writes rendering task and be associated with corresponding template file and data, then appoints rendering
Business issues thread pool and carries out multithreading rendering, splices rendering result after whole rendering tasks to generate complete text
This.This method can render text file faster, can be as much as possible to utilize clothes when server cpu busy percentage is lower
The cpu resource of business device, accelerates the rendering of text, and the automatic protection server when server utilization reaches a certain level
Cpu resource extends working time of the server in the case where receiving a large amount of render requests.
To achieve the above object, the present invention provides a kind of telescopic methods for generating text.
The telescopic method for generating text of one kind of the invention includes:Entire template file is resolved into multiple subtemplates
File;Rendering task list is generated, includes multiple rendering tasks in the rendering task list, the multiple rendering task is used for
It is associated with the corresponding the multiple subtemplate file of multiple rendering task, and realizes the logic of preparation data before rendering;By institute
It states multiple rendering tasks and carries out multithreading rendering;And the rendering result of the multiple rendering task is spliced to generate text.
Optionally, entire template file is resolved into multiple subtemplate files includes:By the variable in text according to template
The reference that the grammer of engine is substituted for Java object generates entire template file;And resolve into multiple subtemplate files.
Optionally, entire template file is resolved into multiple subtemplate files includes:Variable in text is resolved into more
A group;And the variable of each group is generated into subtemplate text according to the reference that the grammer of template engine is substituted for Java object
Part.
Optionally, entire template file is resolved into multiple subtemplate files includes:By entire template file, by page knot
The length of time required for structure and estimated data preparation resolves into multiple subtemplate files.
Optionally, the multiple rendering task includes location information corresponding with the multiple rendering task.
Optionally, before the rendering task list is carried out multithreading rendering, determine whether thread pool is in completely negative
Lotus state.
Optionally, when thread pool not at full capacity when, the current thread for submitting rendering task submits to current rendering task
The thread pool carries out multithreading rendering;And when thread pool at full capacity when, the current thread for submitting rendering task is no longer requested
Rendering task is executed using multithreading, but is directly rendered using current thread.
Optionally, when receiving new render request, the thread pool opens new thread and executes rendering task.
Optionally, the rendering result of the multiple rendering task is spliced to generate text and includes:By the multiple rendering
The sequencing that task is submitted successively splices the rendering result of the multiple rendering task to generate text.
According to the technique and scheme of the present invention, entire template file is resolved into multiple subtemplate files, each submodule can be made
The content of plate file is relatively fewer, to reduce exploitation maintenance cost;The corresponding rendering task of each subtemplate file, can
Data, which are obtained, with multi-threaded parallel organizes the logic of organization data so that the preparation of modules data is more efficient in the page
It is apparent, so that development scheme is highly efficient;Multiple rendering tasks are subjected to multithreading rendering, according to the workload of thread pool
Situation adjusts rendering mode, can play while the more efficient cpu resource using server to server cpu resource certain
Protective effect is unlikely to generate excessive rendering thread under high concurrent scene, is a kind of to aggravate the competition of cpu resource
Can dynamic retractility generation text mode.
Specific embodiment
Below in conjunction with attached drawing, an exemplary embodiment of the present invention will be described, including the various of the embodiment of the present invention
Details should think them only exemplary to help understanding.Therefore, those of ordinary skill in the art should recognize
It arrives, it can be with various changes and modifications are made to the embodiments described herein, without departing from scope and spirit of the present invention.Together
Sample, for clarity and conciseness, descriptions of well-known functions and structures are omitted from the following description.
Fig. 1 is the key step schematic diagram of the telescopic method for generating text of one kind according to an embodiment of the present invention.Such as
Shown in Fig. 1, the telescopic method for generating text of one kind of the embodiment of the present invention mainly includes the following steps, namely S11 to step
S14。
Step S11:Entire template file is resolved into multiple subtemplate files.The step can be by the following two kinds of programs
It executes:
Variable in text is generated entire mould according to the reference that the grammer of template engine is substituted for Java object by scheme one
Plate file;And resolve into multiple subtemplate files.
Variable in text is resolved into multiple groups by scheme two;And by the variable of each group according to template engine
Grammer be substituted for Java object reference generate subtemplate file.
It in practical applications, can be according to the page structure of the text of required rendering, programming and the difficulty or ease of maintenance
Degree etc. flexibly determines which kind of scheme selected.
Entire template file is resolved into multiple subtemplate files, is essentially by entire template file, by page structure
With the length of time required for estimated data preparation, multiple subtemplate files are resolved into.It under normal conditions, can will be multiple
It is decomposed than relatively time-consuming interface in different rendering tasks, to guarantee the harmony of multiple tasks time-consuming.
Step S12:Rendering task list is generated, includes multiple rendering tasks in the rendering task list, it is the multiple
Rendering task prepares data for being associated with the corresponding the multiple subtemplate file of multiple rendering task, while before realizing rendering
Logic.
One rendering task of association, all subtemplate texts are respectively corresponded according to each subtemplate file that step S11 is generated
Part, which corresponds to associated rendering task, successively to be submitted to thread pool and is rendered.Meanwhile rendering task can also realize rendering
The preceding logic for preparing data, i.e.,:The backstage Java single thread successively data required for rigging file.Since rendering task arranges
Table includes multiple rendering tasks, therefore can assemble data required for multiple subtemplate files simultaneously with multithreading, thus greatly
The time for preparing data is saved greatly.
The multiple rendering task includes location information corresponding with the multiple rendering task.Entire template is literary
When part resolves into multiple subtemplate files, it can be decomposed according to situations such as page structure, each subtemplate file is answered at this time
Location information comprising it in entire template file carries out result splicing to render after the completion, generates corresponding text.
Step S13:The multiple rendering task is subjected to multithreading rendering.After the completion of step S12, rendering task is submitted
Thread the multiple rendering task submitted into thread pool multithreading while rendering.
Before the rendering task list is carried out multithreading rendering, first determine whether thread pool is in shape at full capacity
State.
When thread pool not at full capacity when, current rendering task is submitted to the thread by the current thread for submitting rendering task
Pond carries out multithreading rendering.When receiving new render request, the thread pool opens new thread and executes rendering task.
When thread pool at full capacity when, the current thread for submitting rendering task no longer request that multithreading is used to execute rendering
Task, but directly rendered using current thread.
After the completion of rendering task, rendering result is stored in the specified object of each thread.
Step S14:The rendering result of the multiple rendering task is spliced to generate text.When in rendering task list
After the completion of all rendering tasks, the sequencing submitted with the multiple rendering task is by the rendering knot of the multiple rendering task
Fruit is successively spliced, and entire page of text is generated.
Entire template file is resolved into multiple subtemplate files it can be seen from step S11 to step S14, therefore every
The content of a sub- template file is relatively fewer, to reduce exploitation maintenance cost;The corresponding rendering of each subtemplate file
Task can obtain data with multi-threaded parallel and organize organization data so that the preparation of modules data is more efficient in the page
Logic is also apparent, so that development scheme is highly efficient;Multiple rendering task multithreadings are rendered, it is negative according to the work of thread pool
It carries situation and adjusts rendering mode, server cpu resource can be played centainly while more efficient utilization server cpu resource
Protective effect, be unlikely to generate excessive rendering thread under high concurrent scene, be one to aggravate the competition of cpu resource
Kind can dynamic retractility generation text mode.
Fig. 2 is the detailed step flow chart of the telescopic method for generating text of one kind according to an embodiment of the present invention.
As shown in Fig. 2, being illustrated for rendering page of text with the template engine Velocity based on Java.In order to
It realizes and entire template file is resolved into multiple subtemplate files (step S21), the following two kinds method can be used:
1, first the variable in entire page of text is generated according to the reference that Velocity grammer is substituted for Java object
Velocity template file, length of time required for the page structure and estimated data preparation further according to text etc. will
Velocity template file resolves into multiple sub- Velocity template files.
2, first by the variable in entire page of text according to required for the page structure of text and estimated data preparation
The length etc. of time resolve into multiple groups, then the variable of each group is substituted for Java object according to Velocity grammer
Reference generate Velocity template file.
After completing step S21, according to each sub- Velocity template file of generation, corresponding rendering task is write, with
It is associated with rendering task and its corresponding sub- Velocity template file, while realizing the logic of preparation data before rendering.Rendering is appointed
Business have it is multiple, be submitted in the form of rendering task list rendering factory rendered (step S22).Render factory for pair
Rendering task carries out rendering and splices rendering result to generate text.Rendering in factory includes thread pool, is receiving wash with watercolours
Before dye task, rendering factory is first initialized, for example, the thread pool size of settable rendering factory, minimum thread
Several and work queue size etc..After rendering factory receives rendering task, the rendering task list received is first traversed, later
Rendering task is initialized, and is passed to the information such as rendering task total quantity to rendering task, then according to the feature of rendering task, is
It injects the variable that public tool-class etc. can be shared, then above-mentioned multiple sub- Velocity templates are rendered by the way of multithreading
File (step S23).
Before rendering task list is carried out multithreading rendering, need to determine to render whether the corresponding thread pool of factory is located
In full load condition (step S24).When thread pool at full capacity when, the current thread for submitting rendering task no longer request use multi-thread
Journey mode executes rendering task, but is directly rendered (step S25) using current thread.When thread pool not at full capacity when,
Current rendering task is submitted to thread pool and carries out multithreading rendering by the current thread for submitting rendering task, renders factory at this time
Thread pool opens a new thread and executes rendering task (step S26).After the completion of each thread rendering task, rendering is tied
Fruit is stored in the specified object of each thread (step S27), then checks whether rendering task list is finished (step
S28), finished if being not carried out, turn to step S23, it, will according to the sequencing that rendering task is submitted if being finished
The rendering result of the multiple rendering task successively splices (step S29), to generate a complete page of text.
The telescopic method for generating text proposed by the present invention, more utilizing it can be seen from above preferred embodiment
Thread can adjust rendering mode according to the workload situation of thread pool when being rendered, thread pool occupied Thread Count compared with
When few, render text using multithreading, thread pool occupied Thread Count it is more when be changed to single thread rendering mode, and
After having occupied Thread Count and having reduced and multithreading rendering can be reverted to, page of text formation speed is accelerated, to save
Generate the time of page of text.
The present invention is not only to provide a kind of efficient benefit by way of detecting thread pool state before submitting rendering task
With the method for CPU, equally also proposed it is a kind of Velocity template data is decomposed in multiple rendering tasks, held by multithreading
The parallel method for obtaining data of row rendering, while so that the preparation of modules data is more efficient in the page, organizes number
According to logic it is also apparent, be a kind of more efficient development scheme.
Describe basic principle of the invention in conjunction with specific embodiments above, in the apparatus and method of the present invention, it is clear that
Each component or each step can be decomposed and/or be reconfigured.These decompose and/or reconfigure should be regarded as it is of the invention etc.
Efficacious prescriptions case.Also, the step of executing above-mentioned series of processes can execute according to the sequence of explanation in chronological order naturally, still
It does not need centainly to execute sequentially in time.Certain steps can execute parallel or independently of one another.
Above-mentioned specific embodiment, does not constitute a limitation on the scope of protection of the present invention.Those skilled in the art should be bright
It is white, design requirement and other factors are depended on, various modifications, combination, sub-portfolio and substitution can occur.It is any
Made modifications, equivalent substitutions and improvements etc. within the spirit and principles in the present invention, should be included in the scope of the present invention
Within.