This cavalcade by Mahesh Kannan appears via Oracle.
Batch processing is acclimated in abounding industries for tasks alignment from bulk processing; annual generation; end-of-day jobs such as absorption adding and ETL (extract, load, and transform) in a abstracts warehouse; and abounding more. Typically, accumulation processing is bulk-oriented, non-interactive, and continued running—and ability be data- or computation-intensive. Accumulation jobs can be run on agenda or accomplished on demand. Also, aback accumulation jobs are about long-running jobs, check-pointing and restarting are accepted appearance begin in accumulation jobs.
JSR 352 (Batch Processing for Java Platform), allotment of the afresh alien Java EE 7 platform, defines the programming archetypal for accumulation applications additional a runtime to run and administer accumulation jobs. This commodity covers some of the key concepts including affection highlights, an overview of alleged APIs, the anatomy of Job Scheduling Language, and a sample accumulation application. The commodity additionally describes how you can run accumulation applications appliance GlassFish Server Accessible Antecedent Edition 4.0.
This area and Figure 1 alarm the basal apparatus of the accumulation processing architecture.
This commodity demonstrates some of the key appearance of JSR 352 appliance a simple bulk processing application. The appliance has been carefully kept absolutely simple in adjustment to focus on the key concepts of JSR 352.
The SimplePayrollJob batch job involves account ascribe abstracts for bulk processing from a comma-separated ethics (CSV) file. Anniversary band in the book contains an agent ID and the abject bacon (per month) for one employee. The accumulation job afresh calculates the tax to be withheld, the bonus, and the net salary. The job assuredly needs to address out the candy bulk annal into a database table.
We use a CSV book in this archetype aloof to authenticate that JSR 352 allows accumulation applications to apprehend and address from any approximate source.
We discussed that a footfall is a area commodity that encapsulates an independent, consecutive appearance of the job, and a job is basically a alembic for one or added steps.
In JSR 352, a JSL basically specifies the adjustment in which achieve charge be accomplished to achieve the job. The JSL is able abundant to acquiesce codicillary beheading of steps, and it additionally allows anniversary footfall to accept its own properties, listeners, and so on.
A accumulation appliance can accept as abounding JSLs as it wants, appropriately acceptance it to alpha as abounding accumulation jobs as required. For example, an appliance can accept two JSLs, one for bulk processing and addition for address generation. Anniversary JSL charge be alleged abnormally and charge be placed in the META-INF/batch-jobs directory. Subdirectories under META-INF/batch-jobs are ignored.
Our JSL for bulk processing is placed in a book called SimplePayrollJob.xml and looks like Listing 1:
Our SimplePayrollJob batch job has aloof one footfall (called “process”). It is a chunk-style footfall and has (as appropriate for a chunk-style step), an ItemReader, an ItemProcessor, and an ItemWriter. The implementations for ItemReader, ItemProcessor, andItemWriter for this footfall are authentic appliance the ref attribute in the <reader>, <processor>, and <writer> elements.
When the job is submitted (we will see afterwards how to abide accumulation jobs), the accumulation runtime starts with the aboriginal footfall in the JSL and walks its way through until the absolute job is completed or one of the achieve fails. The JSL is able abundant to acquiesce both codicillary achieve and alongside beheading of steps, but we will not awning those capacity in this article.
The item-count attribute, which is authentic as 2 in Listing 1, defines the block admeasurement of the chunk.
Here is a high-level overview of how chunk-style achieve are executed. Please see area 11.6 (“Regular Block Processing”) of the JSR 352 blueprint for added details.
This agency that in our example, the accumulation runtime will apprehend and action two annal and the ItemWriter will address out two annal per transaction.
Our bulk processing accumulation JSL defines a distinct block appearance footfall and specifies that the footfall uses an ItemReader namedsimpleItemReader. Our appliance contains an accomplishing of ItemReader to apprehend ascribe CSV data. Listing 2 shows a atom of our ItemReader:
Note that the chic is annotated with the @Named annotation. Because the @Named annotation uses the absence value, the Contexts and Dependency Injection (CDI) name for this bean is simpleItemReader. The JSL specifies the CDI name of the ItemReader in the<reader> element. This allows the accumulation runtime to instantiate (through CDI) our ItemReader when the footfall is executed.
Our ItemReader also injects a JobContext. JobContext allows the accumulation antiquity (ItemReader, in this case) to apprehend ethics that were anesthetized during job submission.
Our payroll SimpleItemReader overrides the open() method to accessible the ascribe from which bulk ascribe abstracts is read. As we shall see later, the parameter prevCheckpointInfo will not be absent if the job is actuality restarted.
In our example, the open() method, which is apparent in Listing 3, opens the bulk ascribe book (which has been packaged forth with the application).
The readItem() method basically reads one band of abstracts from the ascribe book and determines whether the band contains two integers (one for agent ID and one for abject salary). If there are two integers, it creates and allotment a new instance of PayrollInputRecord and allotment to the accumulation runtime (which is afresh anesthetized to ItemWriter).
The method checkpointInfo() is alleged by the accumulation runtime at the end of every acknowledged block transaction. This allows the Reader to analysis point the aftermost acknowledged apprehend position.
In our example, the checkpointInfo() returns the recordNumber indicating the cardinal of annal that accept been apprehend successfully, as apparent in Listing 5.
Our SimpleItemProcessor follows a arrangement agnate to the arrangement for SimpleItemReader.
The processItem() method receives (from the accumulation runtime) the PayrollInputRecord. It afresh calculates the tax and net and allotment a PayrollRecord as output. Apprehension in Listing 6 that the blazon of commodity alternate by an ItemProcessor can be actual altered from the blazon of commodity it accustomed from ItemReader.
By now, SimpleItemWriter must be afterward anticipated curve for you.
The alone aberration is that it injects an EntityManager so that it can abide the PayrollRecord instances (which are JPA entities) into a database, as apparent in Listing 7.
The writeItems() method persists all the PayrollRecord instances into a database table appliance JPA. There will be at most item-count entries (the block size) in the list.
Now that we accept our JSL, ItemReader, ItemProcessor, and ItemWriter ready, let’s see how a accumulation job can be submitted.
Note that the bald attendance of a job XML book or added accumulation artifacts (such as ItemReader) doesn’t beggarly that a accumulation job is automatically started aback the appliance is deployed. A accumulation job charge be accomplished explicitly, say, from a servlet or from an Enterprise JavaBeans (EJB) timer or an EJB business method.
In our bulk application, we use a servlet (named PayrollJobSubmitterServlet) to abide a accumulation job. The servlet displays an HTML folio that presents to the user a anatomy absolute two ons. Aback the aboriginal on, labeled Calculate Payroll, is clicked, the servlet invokes the startNewBatchJob method, apparent in Listing 8, which starts a new accumulation job.
The aboriginal footfall is to access an instance of JobOperator. This can be done by calling the following:
The servlet afresh creates a Properties object and food the ascribe book name in it. Finally, a new accumulation job is started by calling the following:
The jobname is annihilation but the job JSL XML book name (minus the .xml extension). The properties parameter serves to canyon any ascribe abstracts to the job. The Properties object (containing the name of the bulk ascribe file) is fabricated accessible to added accumulation artifacts (such as ItemReader, ItemProcessor, and so on) through the JobContext interface.
The accumulation runtime assigns a different ID, alleged the beheading ID, to analyze anniversary beheading of a job whether it is a afresh submitted job or a restarted job. Abounding of the JobOperator methods booty the beheading ID as parameter. Appliance the beheading ID, a affairs can access the accepted (and past) beheading cachet and added statistics about the job. The JobOperator.start() method allotment the beheading ID of the job that was started.
When a accumulation job is submitted, the accumulation runtime creates an instance of JobExecution to clue it. JobExecution has methods to access assorted capacity such as the job alpha time, job achievement time, job avenue status, and so on. To access the JobExecution for an beheading ID, you can use the JobOperator.getJobExecution(executionId) method. Listing 9 shows the analogue of JobExecution:
Now that we accept our JSL, ItemReader, ItemProcessor, ItemWriter, and our servlet ready, it is time to amalgamation them and get accessible to deploy.
You can arrange your accumulation appliance as any of the accurate Java EE athenaeum (for example, .war, .jar, or .ear). You can array your accumulation antiquity classes forth with added Java EE classes (such EJB beans and servlets).
The alone appropriate claim is that you charge to abode your job JSLs beneath the META-INF/batch-jobs directory for .jar files. For.war archive types, abode your job JSLs beneath the WEB-INF/classes/META-INF/batch-jobs directory.
Let’s arrange the bulk appliance that we accept developed into the GlassFish 4.0 appliance server. GlassFish 4.0 is the advertence accomplishing (RI) for the Java EE 7.0 blueprint and contains the RI for JSR 352 as well. You can acquisition added advice about GlassFish 4.0 at http://glassfish.org and about the Java Accumulation 1.0 RI at https://java.net/projects/jbatch/.
You can download GlassFish 4.0 from https://glassfish.java.net/public/downloadsindex.html#top and afresh install it. Alpha GlassFish 4.0 by aperture a command window and active the afterward command:
Because the sample bulk appliance uses a database (to address out candy data), we charge a database active afore we can run our application. You can alpha the Apache Derby database by active the afterward command:
First, actualize a new agenda named hello-batch. Afresh change to the hello-batch directory:
To abridge and package, run the afterward command, which creates hello-batch.war under the ambition directory:
To deploy hello-batch.war, run the afterward command:
If you appetite to redeploy the application, you can run the afterward command:
Once you arrange the hello-batch.war file, you can run the appliance by accessing http://localhost:8080/hello-batch/PayrollJobSubmitterServlet from a browser. Accessing this URL should present the awning apparent in Figure 2.
Click the Calculate Payroll on and you should see a new access in the table, as apparent in Figure 3.
Click the Refresh on and you should see the Avenue Cachet and End Time columns adapted for the latest job (see Figure 4). The Avenue Cachet cavalcade shows whether the job bootless or completed successfully. Aback our SimplePayrollJob doesn’t accept any errors (at atomic not yet!), the Avenue Cachet displays COMPLETED.
Click the Calculate Payroll and Refresh ons a few added times. Agenda that anniversary time a job is started, a new beheading ID (and instance ID) is accustomed to the job, as apparent in Figure 5.
So far, we had been starting accumulation jobs appliance the jobOperator.start() method. Let’s say that our bulk ascribe book has some errors. Either the ItemReader or the ItemProcessor could ascertain invalid annal and abort the accepted footfall and the job. The ambassador or the end user can fix the absurdity and can restart the accumulation job. This access of ablution a new job that starts from the alpha afterwards convalescent from errors ability not calibration if the bulk of abstracts to be candy is large. JobOperator provides addition adjustment calledrestart() to break absolutely this problem.
We saw beforehand that a job is about a alembic for steps. Aback a job is started, it charge be tracked, so the accumulation runtime creates aJobInstance. A JobInstance refers to the abstraction of a analytic run. In our example, we accept a PayrollJob and if the PayrollJob is run every month, there will be a Jan-2013 JobInstance and there will be addition Feb-2013 JobInstance, and so on.
If the bulk processing for Jan-2013 fails, it charge be restarted (after apparently acclimation the error), but it is still the Jan-2013 run because it is still processing Jan-2013 records.
A JobExecution refers to the abstraction of a distinct attack to run a Job. Anniversary time a job is started or restarted, a new JobExecution is created that belongs to the same JobInstance. In our example, if the Jan-2013 JobInstance is restarted, it is still the aforementioned Jan-2013JobInstance but a new JobExecution is created that belongs to the same JobInstance.
In summary, a job can accept one or added instances of JobInstance and each JobInstance can accept one or more JobExecutioninstances. Appliance a new JobInstance means “start from the beginning” and appliance an existing JobInstance generally agency “start from area you larboard off.”
If you recall, a chunk-style footfall executes in a transaction in which item-count entries are read, processed, and written. Afterwards theItemWriter’s writeItems() has been invoked, the accumulation runtime calls the checkpointInfo() method on both ItemReader andItemWriter. This allows both ItemReader and ItemWriter to bookmark (save) their accepted progress. The abstracts that is bookmarked for an ItemReader could be annihilation that will advice it to resume reading. For example, our SimpleItemReader needs to save the band cardinal up to which it has apprehend auspiciously so far.
Section 10.8 of the JSR 352 blueprint describes the restart processing in detail.
Let’s booty a moment to attending into the log book area our SimpleItemReader outputs some advantageous letters from the open() andcheckpoint() methods. Anniversary bulletin is prefixed with the string [SimpleItemReader] so you can bound analyze the messages. The log book is amid at <GlassFish install Dir>/domains/domain1/logs/server.log.
Listing 10 shows the letters that are prefixed by the string [SimpleItemReader]:
Note: You could additionally use the command tail -f server.log | grep SimpleItemReader.
Because, our job XML book (SimplePayrollJob.xml) specifies a bulk of 2 for item-count as the block size, the accumulation runtime callscheckpointInfo() on our ItemReader every two records. The accumulation runtime food this checkpoint advice in JobRepository. So, if an absurdity occurs during the bosom of our block processing, the accumulation appliance charge be able to resume from the aftermost acknowledged checkpoint.
Let’s acquaint some errors in our ascribe abstracts book and see how we can balance from ascribe errors.
If you attending at our servlet’s output, which is amid under <GlassFish install Dir>/domains/domain1/applications/hello-batch/WEB-INF/classes/payroll-data/payroll-data.csv, you see that it displays the area of the ascribe book from area CSV abstracts is apprehend for our bulk application. Listing 11 shows the agreeable of the file:
Open your admired editor and acquaint an error. For example, let’s say we add a few characters to the bacon acreage on the eighth record, as apparent in Listing 12:
Save the book and abdicate the editor. Go aback to your browser and bang the Calculate Payroll on followed by the Refresh on. You would see that the afresh submitted job failed, as apparent in Figure 6. (Look at the Avenue Cachet column.)
You will additionally apprehension that a Restart on appears aing to the beheading ID of the job that aloof failed. If you click Refresh, the job will abort (because we haven’t anchored the affair yet). Figure 7 shows what is displayed afterwards a few clicks of the Refresh on.
If you attending into the GlassFish server log (located under <GlassFish install Dir>/domains/domain1/logs/server.log), you will see an exception, as apparent in Listing 13:
You should additionally apprehension that aback you bang the Restart on, a new job beheading is created but its job instance ID charcoal the same. Aback you bang the Refresh on, our PayrollJobSubmitter servlet calls a adjustment named restartBatchJob(), which is apparent in Listing 14:
The key band in Listing 14 is the alarm to JobOperator’s restart() method. This adjustment takes a Properties object aloof like start(), but instead of casual a job XML book name, it passes the beheading ID of the best afresh bootless job. Appliance the best afresh bootless job’s beheading ID, the accumulation runtime can retrieve the antecedent execution’s aftermost acknowledged checkpoint. The retrieved checkpoint abstracts is anesthetized to the open() method of our SimpleItemReader (and ItemWriter) to accredit them to resume account (and writing) from the aftermost acknowledged checkpoint.
While ensuring that your browser shows the folio with a Restart on, adapt the book afresh and aish the accidental characters from the eighth record. Afresh bang the Restart and Refresh ons. The latest beheading should affectation a COMPLETED status, as apparent in Figure 8.
It is time to attending into the log book to accept what aloof happened. Again, attractive for letters prefixed with SimpleItemReader, Listing 15 shows what you ability see:
As you can see, our SimpleItemReader’s open() method was alleged with the antecedent checkpoint bulk (which was almanac cardinal 7) acceptance our SimpleItemReader to skip the aboriginal six annal and resume account from the seventh record.
You can appearance the account of all accumulation jobs in the JobRepository. Fire up a browser window and go to localhost:4848. Afresh click server (Admin Server) in the larboard panel, as apparent in Figure 9.
You can bang the Batch tab, which should account all the accumulation jobs submitted to this GlassFish server. Agenda that the JobRepository is implemented appliance a database and, hence, the job capacity survive GlassFish 4.0 server restarts. Figure 10 shows all the accumulation jobs in theJobRepository.
You can additionally bang one of the IDs listed beneath Beheading IDs. For example, clicking 293 reveals capacity about aloof that execution:
More capacity about the beheading can be acquired by beat the Execution Steps tab on the top.
Look at the statistics provided by this page. It shows how abounding reads, writes, and commits were performed during this execution.
You can additionally appearance the capacity about jobs active in the GlassFish 4.0 server by appliance the command-line interface (CLI).
To appearance the account of accumulation jobs, accessible a command window and run the afterward command:
You should see achievement agnate to Figure 13:
To appearance the account of batch JobExecutions, you can run this command:
You should see achievement agnate to Figure 14:
The command lists the achievement cachet of anniversary beheading and additionally the job ambit anesthetized to anniversary execution.
Finally, in adjustment to see capacity about anniversary footfall in a JobExecution, you could use the afterward command:
You should see achievement agnate to Figure 15:
Take agenda of the STEPMETRICS column. It tells how abounding times ItemReader and ItemWriter were alleged and additionally how abounding commits and rollbacks were done. These are acutely admired metrics.
The CLI achievement charge bout the Admin Console appearance because they both concern the same JobRepository.
You can use asadmin advice <command-name> to get added capacity about the CLI commands.
In this article, we saw how to write, package, and run simple accumulation applications that use chunk-style steps. We additionally saw how the checkpoint affection of the accumulation runtime allows for the accessible restart of bootless accumulation jobs. Yet, we accept almost aching the apparent of JSR 352. With the abounding set of Java EE apparatus and appearance at your disposal, including servlets, EJB beans, CDI beans, EJB automated timers, and so on, feature-rich accumulation applications can be accounting adequately easily.
This commodity additionally covered (briefly) the GlassFish 4.0 Admin Console and CLI abutment for querying the batch JobRepository. Both the Admin Console and the CLI accommodate admired capacity about jobs and achieve that can be acclimated to ascertain abeyant bottlenecks.
JSR 352 supports abounding added agitative appearance such as batchlets, splits, flows, and custom checkpoints, which will be covered in approaching articles.
Mahesh Kannan is a chief software artist with Oracle’s Cloud Appliance Foundation team, and he is the Expert Group Member for the Java Accumulation JSR. Due to his all-encompassing acquaintance with appliance servers, containers, and broadcast systems, he has served as advance artist and “consultant at large” on abounding projects that body avant-garde solutions for Answer products.
Quiz: How Much Do You Know About Entry Level Sql Developer Resume Sample? | Entry Level Sql Developer Resume Sample – entry level sql developer resume sample
| Allowed in order to the website, on this time I’m going to provide you with about entry level sql developer resume sample