Slide 1

Slide 1 text

JAKARTA BATCH (VER. 3.1) A part of Jakarta EE™ specification Batch Processing Framework 1 By Koichi NAKAGAWA Enterprise Web Application Development Course (7)

Slide 2

Slide 2 text

Update Information • Ver. 3: Use Rocky Linux™ instead of CentOS™ as a Linux platform and Payara Server 6™ certified as Jakarta EE 9.1 Platform Compatible Products. • Ver. 2 : Use JDK 11 instead of JDK 8 and Jakarta EE 9 instead of Jakarta EE 8 in all exercises. 2

Slide 3

Slide 3 text

JBatch Jakarta Batch 3

Slide 4

Slide 4 text

EWA development course curriculum JSF with CDI JPA + JTA with CDI JAX-RS Application Architecture Design Patterns Eclipse IDE™ Version Control Tool with Git™ Build Tool with Apache Maven™ Payara Server™ Administration Windows 10™ + Linux (Rocky Linux™) Total EWA Development Exercise Jakarta Batch Java SE (Oracle JDK™/OpenJDK™) Required Skills to take courses Test Tool with JUnit5 PostgreSQL™ Administration 4 Object Oriented Development Methodology

Slide 5

Slide 5 text

Trademarks Notice • Jakarta™ EE and its logo( ) are trademarks of the Eclipse Foundation. • PostgreSQL™ and its logo( ) are registered trademarks of the PostgreSQL Community Association of Canada. • Apache Derby™ and its logo ( ) are trademarks of the Apache Software Foundation. • Payara™ Server and their logo( ) are trademarks of Payara Services® LTD. • EclipseLink™, EclipseLink™ logo ( ), Eclipse IDE for Enterprise Java Developer™, the Eclipse IDE for Enterprise Java Developer ™ logo( ), Eclipse M2Eclipse™, M2Eclipse™, Eclipse GitTeam Provider™, Eclipse EGit™, EGit™, Eclipse Java development tools™, Java development tools™, Eclipse JDT™, JDT™ are trademarks of the Eclipse Foundation. • Apache Maven™, Maven™ are trademarks of the Apache Software Foundation (ASF). • Git™ and the Git™ logo( ) are either registered trademarks or trademarks of Software Freedom Conservancy, Inc., corporate home of the Git Project, in the United States and/or other countries. • Java™ is trademark of Oracle Corporation. 5

Slide 6

Slide 6 text

Assumption for this course • The trainees who take this course are required the following skills in advance • PostgreSQL (Version: 13.5) – Basic Administration Operations • Payara Server (Version: 6.2021.1.Alpha1) – Basic Administration Operations • Oracle JDK/OpenJDK (Version: 11) • Eclipse IDE for Enterprise Java Developers (Version: 2021-12 (4.22.0)) • Build Tool Training Course • Version Control Tool Training Course • Test Tool Training Course • JSF with CDI Training Course • JPA + JTA with CDI Training Course • JAX-RS Training Course 6

Slide 7

Slide 7 text

Objectives This course is aimed to obtain the following skills • Jakarta Batch technology • Development of Batch applications using Jakarta Batch • Integration with JAX-RS in presentation layer 7

Slide 8

Slide 8 text

Jakarta Batch • Concept of Jakarta Batch • How to use Jakarta Batch • Architecture Design with Jakarta Batch and JAX-RS • First Jakarta Batch Application • Exercise 8

Slide 9

Slide 9 text

Concept of Jakarta Batch 9

Slide 10

Slide 10 text

Concept of Jakarta Batch • What’s Batch Processing Framework 10 Batch Processing Framework Batch Job A DB, File, … DB, File, … • No User Interface • Long-time Execution • Job Flow Definitions • Job Management Batch Job B DB, File, … DB, File, … . . . Ex. Billing System, Reporting Generation System, Image Processing System, etc.

Slide 11

Slide 11 text

Concept of Jakarta Batch • Required Functionalities for Batch Processing Framework 11 Define jobs, steps, decision elements, and the relationships between them Execute some groups of steps or parts of a step in parallel Maintain state information for jobs and steps Launch jobs and resume interrupted jobs Handle errors

Slide 12

Slide 12 text

Job X Batch Runtime Job Y Job X Concept of Jakarta Batch • Components in Jakarta Batch 12 Batch Artifact A Batch Artifact B Batch Artifact C Java API Job A Job A Job X Instance Job A Job A Job Y Instance . . . Batch Client App. • Job Execution • Job Interruption • Job Information Jakarta Batch Processing Framework Job X Job Definition File Jakarta EE Container

Slide 13

Slide 13 text

Concept of Jakarta Batch • Job and Step 13 Each Job application comprises several Steps. Job A Step A1 Step A2 Step A3 Job B Step B1 Step B2 Job C Step C1 Step C2 Step C3 Step C4 Job D Step D1

Slide 14

Slide 14 text

Input Retrieval (Item Reader) Business Processing (Item Processor) Output Results (Item Writer) Concept of Jakarta Batch • Step Implementation Style – Chunk Step and Task Step 14 Chunk Step: Step A1 Task Step: Step A2 Task Processing (Batchlet) Batch Artifact Batch Artifact Batch Artifact Batch Artifact

Slide 15

Slide 15 text

Concept of Jakarta Batch • Flow, Split and Decision 15 Job X Flow X11 Flow X12 Split X1 Decision X3 Step X2 Step X4 Step X111 Step X121 Step X122 Step X112

Slide 16

Slide 16 text

How to use Jakarta Batch 16

Slide 17

Slide 17 text

Batch Runtime Job Y Job X How to use Jakarta Batch • Architecture of Jakarta Batch 17 Batch Client App. Job Request (Start, Stop, Restart, Check Info., …) Job Response (Exec ID, Info., …) Jakarta EE Container JobOperator Job A Job A Job X Instance Job A Job A Job Y Instance . . . Batch Artifact A Job X Job Definition File Batch Artifact A Batch Artifact A

Slide 18

Slide 18 text

Batch Runtime Job Y Job X How to use Jakarta Batch • Job Definition File 18 Batch Client App. Job Request (Start, Stop, Restart, Check Info., …) Job Response (Exec ID, Info., …) Jakarta EE Container JobOperator Job A Job A Job X Instance Job A Job A Job Y Instance . . . Batch Artifact A Batch Artifact A Batch Artifact A Job X Job Definition File

Slide 19

Slide 19 text

Job X How to define Batch Job • Job Definition File in Job Specification Language (JSL) 19 Job Definition File for Batch Job ID X (X.xml) : : : Define Batch Job META-INF batch-jobs X.xml Y.xml Z.xml Location (Under Class Path) [Job ID].xml . . .

Slide 20

Slide 20 text

How to define Batch Job • Job Steps and Batch Artifacts 20 Job Definition File Batch Artifact A (CDI: @Named("A")) Batch Artifact B2 (CDI: @Named("B2")) Batch Artifact C (CDI: @Named("C")) . . . Job X Step X1 Step X2 Step X3 : : Batch Artifact B1 (CDI: @Named("B1")) Batch Artifact B3 (CDI: @Named("B3"))

Slide 21

Slide 21 text

How to define Batch Job • Job Flows, Splits and Decisions 21 : ... ... ... … : … : Job Definition File Job X Flow X1 Flow X111 Flow X112 Flow X113 Split X11 Decision X13 Step X12 Step X14

Slide 22

Slide 22 text

How to define Batch Job • “Batch Status” (Runtime Status Value) 22 Status(*) Description STARTING Job has been submitted to the batch runtime. STARTED Job is running. STOPPING Job has been requested to stop. STOPPED Job has stopped. FAILED Job finished executing because of an error. COMPLETED Job finished executing successfully. ABANDONED Job was marked abandoned. (*)The above batch status is automatically set by the Batch Runtime. • “Exit Status” (User-Defined Value) “Exit Status” is user-defined value and set by Batch Artifacts or Job Definition Files. Default value is the same as Batch Status. During the execution of a Batch Job, “Batch Status” and “Exit Status” for the Job are set several times and changing. Step X1 • Batch Status and Exit Status Split X11 Flow X111 Job X Decision X13

Slide 23

Slide 23 text

How to define Batch Job • Transition Elements in Step, Decision (and Flow) 23 : ... ... ... … : … : Job Definition File Transition Elements Batch Status Job ends with FAILED COMPLETED

Slide 24

Slide 24 text

Batch Runtime How to define Batch Job • Listeners 24 Job Definition File : ... : Intercept Job Execution Job X Job Level Listener (CDI: @Named("JobListener")) Types of Listeners Job Level Listener, Step Level Listener, Chunk Listener, Item Read Listener, Item Process Listener, Item Write Listener, Skip Listener, Retry Listener Job Execution

Slide 25

Slide 25 text

Batch Runtime Job X Job Execution How to define Batch Job • Properties 25 Job Definition File : : passing Static Properties Job Level Properties input_file= input.txt output_file = output.txt Types of Properties Job Level Properties, Step Level Properties, Step Level Listener Properties, Reader Properties, Processor Properties, Writer Properties, Checkpoint Algorithm Properties, Batchlet Properties, Partition Properties, Mapper Properties, Partition Reducer Properties, Partition Collector Properties, Partition Analyzer Properties, Decision Properties

Slide 26

Slide 26 text

Batch Runtime How to define Batch Job • Partitioned Step 26 Job Definition File . . . Step X1 Step X2 : … : Partition #0 Partition #1 Partition #0 Partition #1 Partition #2 Thread #1 Thread #2 Thread #1 Thread #2 Partition #3 Job X Partition Properties filename = /tmp/file1.txt Partition Properties filename = /tmp/file2.txt Wait …

Slide 27

Slide 27 text

Batch Runtime Job Y Job X How to use Jakarta Batch • Batch Artifact 27 Batch Client App. Job Request (Start, Stop, Restart, Check Info., …) Job Response (Exec ID, Info., …) Jakarta EE Container JobOperator Job A Job A Job X Instance Job A Job A Job Y Instance . . . Batch Artifact A Batch Artifact A Batch Artifact A Job X Job Definition File

Slide 28

Slide 28 text

Batch Runtime How to develop Batch Artifacts • Batch Context 28 Batch Context Job Context Step Context Batch Artifact • Information about running batch jobs/steps • Storage of interim values for batch jobs/steps Get/Set Job Information Job Job A Job A Job Instance Job Job Definition File

Slide 29

Slide 29 text

Batch Runtime How to develop Batch Artifacts • Run-time Job Parameters 29 Batch Artifact Start Job with Job Parameters Job Job A Job A Job Instance Batch Client App. JobOperator Obtain Job Parameters

Slide 30

Slide 30 text

How to develop Batch Artifacts • Reader, Processor and Writer in Chunk Step 30 Chunk Step: Step A1 MyReader (Item_Reader) MyProcessor (Item_Processor) MyWriter (Item_Writer) Job Definition File MyReader MyProcessor 10 Items processed? MyWriter No Yes Read Next 10 Items Batch Artifact Batch Artifact Batch Artifact

Slide 31

Slide 31 text

How to develop Batch Artifacts • Check Point Class for Chunk Step 31 public class MyCheckpoint implements Serializable { private long lineNum = 0; public void increase() { lineNum++; } public long getLineNum() { return lineNum; } } (*) MyCheckpoint class keeps track of the line number currently processed an input file. Serializable interface must be implemented.

Slide 32

Slide 32 text

How to develop Batch Artifacts • ItemReader Class for Chunk Step 32 @Dependent @Named("MyReader") public class MyReader implements javax.batch.api.chunk.ItemReader { private MyCheckpoint checkpoint; private BufferedReader breader; @Inject JobContext jobCtx; public MyReader() {} @Override public void open(Serializable ckpt) throws Exception { if (ckpt == null) checkpoint = new MyCheckpoint(); else checkpoint = (MyCheckpoint) ckpt; String fileName = jobCtx.getProperties().getProperty("input_file"); breader = new BufferedReader(new FileReader(fileName)); for (long i = 0; i < checkpoint.getLineNum(); i++) breader.readLine(); } @Override public void close() throws Exception { breader.close(); } @Override public Object readItem() throws Exception { String line = breader.readLine(); if (line != null) checkpoint.increase(); return line; } @Override public Serializable checkpointInfo() throws Exception { return checkpoint; } } For resuming this job, move the reading point. Obtain the static parameter values from Job Definition File. Named CDI Inject JobContext

Slide 33

Slide 33 text

How to develop Batch Artifacts • ItemProcessor Class for Chunk Step 33 @Dependent @Named("MyProcessor") public class MyProcessor implements jakarta.batch.api.chunk.ItemProcessor { public MyProcessor() {} @Override public Object processItem(Object obj) throws Exception { String line = (String) obj; return line.toUpperCase(); } }

Slide 34

Slide 34 text

How to develop Batch Artifacts • ItemWriter Class for Chunk Step 34 @Dependent @Named("MyWriter") public class MyWriter implements jakarta.batch.api.chunk.ItemWriter { private BufferedWriter bwriter; @Inject private JobContext jobCtx; @Override public void open(Serializable ckpt) throws Exception { String fileName = jobCtx.getProperties().getProperty("output_file"); bwriter = new BufferedWriter(new FileWriter(fileName, (ckpt != null))); } @Override public void close() throws Exception { bwriter.close(); } @Override public void writeItems(List items) throws Exception { for (int i = 0; i < items.size(); i++) { String line = (String) items.get(i); bwriter.write(line); bwriter.newLine(); } } @Override public Serializable checkpointInfo() throws Exception { return new MyCheckpoint; } } For the first time, this opens the output file with OVERWRITE mode. Otherwise, it opens the file with the APPEND mode.

Slide 35

Slide 35 text

How to develop Batch Artifacts • Batchlet in Task Step 35 Job Definition File Task Step: Step A2 Task Processing (Batchlet)

Slide 36

Slide 36 text

How to develop Batch Artifacts • Batchlet Class for Task Step 36 @Dependent @Named("MyBatchlet") public class MyBatchlet implements jakarta.batch.api.Batchlet { @Inject private JobContext jobCtx; @Override public String process() throws Exception { JobOperator jobOperator = BatchRuntime.getJobOperator(); Properties jobParameters = jobOperator.getParameters(jobCtx.getExecutionId()); String value1 = (String)jobParameters.get("parameter1"); System.out.println("The value of parameter1 is " + value1 + "."); String fileName = jobCtx.getProperties().getProperty("output_file"); System.out.println("The size of the output file processed by Batch Job is " + (new File(fileName)).length() + "."); return "COMPLETED"; } : Obtain the Job Parameter values. Set an Exit Status. : @Override public void stop() throws Exception { System.out.println("MyBatchlet(" + jobCtx.getExecutionId() + ") is stopping ..."); } }

Slide 37

Slide 37 text

Batch Runtime Job Y Job X How to use Jakarta Batch • Batch Client App. 37 Batch Client App. Job Request (Start, Stop, Restart, Check Info., …) Job Response (Exec ID, Info., …) Jakarta EE Container JobOperator Job A Job A Job X Instance Job A Job A Job Y Instance . . . Batch Artifact A Batch Artifact A Batch Artifact A Job X Job Definition File

Slide 38

Slide 38 text

Batch Runtime Job Y How to develop Batch Client App. • Job Execution 38 Job X JobInstance (Exec ID = 2, 3) JobInstance (Exec ID = 4) JobInstance (Exec ID = 1) JobOperator Batch Client App. Job Request (Start, Stop, Restart, Check Info., …) Job Response (Exec ID, Info., …) JobExecution (Exec ID = 1) JobExecution (Exec ID = 2) JobExecution (Exec ID = 3) . . . . . . JobExecution (Exec ID = 4) Fail

Slide 39

Slide 39 text

How to develop Batch Client Apps • Batch Client App. 39 public class BatchJobClient { : public Long startJob(Properties props ) throws Exception { JobOperator jobOperator = BatchRuntime.getJobOperator(); Long execID = jobOperator.start("simplejob", props); return execID; } : public String getJobStatus(Long execID) throws Exception { JobOperator jobOperator = BatchRuntime.getJobOperator(); JobExecution jobExec = jobOperator.getJobExecution(execID); String status = jobExec.getBatchStatus().toString(); return status; } : } Start Job with the Job Parameters. Obtain the JobOperator object. Obtain the JobExecution object. Obtain the Job Status.

Slide 40

Slide 40 text

Architecture Design of Jakarta Batch and JAX-RS 40

Slide 41

Slide 41 text

Business Domain Service Batch Runtime Architecture Design • Architecture Design of Jakarta Batch along with JAX-RS 41 Presentation REST Req. Handler Web Resource CDI Web Req. Handler Named CDI Action Method Transactional CDI Service Method Data Access O-R Mapper Entity/VO Entity/VO RDB Entity/VO Entity/VO Name: Vendor: ABC XYZ Update Update Results: Name ABC Vendor XYZ DTO DTO Jakarta EE Container Requester Resource Method Batch Client App. Job X Job A Job A Job X Instance Job Request

Slide 42

Slide 42 text

Batch Client App. with JAX-RS • Batch Client Application Program Example of Web Resource CDI 42 Web Resource Class (CDI) Resource Method @RequestScoped @Path("/batchJob") public class BatchJob { : @PUT @Path("/simplejob") @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.APPLICATION_JSON) public Response simplejob(Properties props) { Long execID = 0L; try { JobOperator jobOperator = BatchRuntime.getJobOperator(); execID = jobOperator.start("simplejob", props); } catch (Exception e) { throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR); } return Response.status(Response.Status.OK).entity(execID).build(); } } Batch Client App.

Slide 43

Slide 43 text

First Jakarta Batch Application 43

Slide 44

Slide 44 text

First Jakarta Batch Application Dev. Exercise • Use Case  Develop a Batch Job of “simplejob” implementing the followings. • Job Definition File (META-INF/batch-jobs/simplejob.xml) • Batch Artifacts (MyReader.java, MyProcessor.java, MyWriter.java, MyBatchlet.java)  Develop a Batch Client App. inside Web Resource CDI (BatchJob.java) to start a Batch Job named “simplejob”. 44 Jakarta EE Container Batch Runtime Job “simplejob” Job A Job A Job “simplejob” Instance Batch Artifact (MyReader.java) Batch Artifact (MyProcessor.java) Batch Artifact (MyWriter.java) Batch Artifact (MyBatchlet.java) Job Definition File (simplejob.xml) Web Resource CDI (BatchJob.java) Resource Method Batch Client App. Exercise: Let’s make the First Jakarta Batch Application.

Slide 45

Slide 45 text

REST Req. Handler Requester First Jakarta Batch Application Dev. Exercise JUnit5 makes a PUT request to a REST Service Provider to start a Batch Job instance of “simplejob”. • PUT Request URI: http://localhost:8080/jsf_jpa_jaxrs_jbatch/webapi/batchJob/simplejob • Request Message: A Property where the name is “parameter1” and the value is “value1” (MIME Type: “application/JSON”) • Response Message: An Execution ID in Long (MIME Type: “text/plain”) 45 Jakarta EE Container Web Resource CDI (BatchJob.java) Resource Method REST Request Message (URI:http://localhost:8080/jsf_jpa_jaxrs_jbatch/ webapi/batchJob/simplejob) (JSON: {“parameter1”: “value1”}) REST Response Message (Long: 123) JUnit5 System Test (integration-test) Eclipse Batch Client App. Batch Runtime Job “simplejob” Job A Job A Job “simplejob” Instance Exercise: Let’s make use of JUnit5 and make a Test Case as REST Client.

Slide 46

Slide 46 text

First Jakarta Batch Application Dev. Exercise • This project of “jsf_jpa_jaxrs_jbatch” is based on the “jsf_jpa_jaxrs” project and add necessary components newly created on the Eclipse IDE. 46 jsf_jpa_jaxrs Copy jsf_jpa_jaxrs_jbatch Add • simplejob.xml : Job Definition File • MyReader.java: Batch Artifact Class • … • BatchJob.java : Web Resource Class • BatchJobIT.java : System Test

Slide 47

Slide 47 text

• Procedure of the Development Edit pom.xml and Change package names Change the Message folder and the associated Class Create the Required Class Execute the REST System Test with Maven Test with Talend API Tester 47 First Jakarta Batch Application Dev. Exercise

Slide 48

Slide 48 text

• Modify the created Maven Object Model file (pom.xml)  Change the artifactId to “jsf_jpa_jaxrs_jbatch”. • Change the package names  Refactor all the packages to rename the packages from “jaxrs_jsf_jpa” to “jsf_jpa_jaxrs_jbatch” creating new packages of “org.example. jsf_jpa_jaxrs_jbatch.batch” under “src/main/java”.  Change the JPA configuration file for the JPA Unit Test, persistence.xml, under “src/test/resources/META-INF” to rename the packages for tags. 4.0.0 org.example jsf_jpa_jaxrs_jbatch war 0.0.1 48 Revised pom.xml Revised persistence.xml Renamed First Jakarta Batch Application Dev. Exercise New Package Names

Slide 49

Slide 49 text

• Procedure of the Development Edit pom.xml and Change package names Change the Message folder and the associated Class Create the Required Class Execute the REST System Test with Maven Test with Talend API Tester 49 First Jakarta Batch Application Dev. Exercise

Slide 50

Slide 50 text

• Change the Message folder Rename the Message folder to “org/example/jsf_jpa_jaxrs_jbatch”. • Change the Message Utility Class Modify the Message Utility Class, Messages.java, under the package of “org.example.jsf_jpa_jaxrs_jbatch.util” about the new folder name. 50 Renamed Revised Messages.java Renamed First Jakarta Batch Application Dev. Exercise

Slide 51

Slide 51 text

• Procedure of the Development Edit pom.xml and Change package names Change the Message folder and the associated Class Create the Required Classes Execute the REST System Test with Maven Test with Talend API Tester 51 First Jakarta Batch Application Dev. Exercise

Slide 52

Slide 52 text

• Create required Classes 1. Create the Job Definition File, simplejob.xml, under the “src/main/resources/META-INF/batch-jobs”. 2. Create MyCheckpoint.java and the Batch Artifacts, MyReader.java, MyProcessor.java, MyWriter.java and MyBatchlet.java, of the “org.example.jsf_jpa_jaxrs_jbatch.batch” package under the “src/main/java”. 3. Create a new Web Resource Class, BatchJob.java, of the “org.example.jsf_jpa_jaxrs_jbatch.rest” package under the “src/main/java” folder, which specifies the Web Resource Class Path of “/batchJob” and create a Resource Method of “simplejob(Properties props)” in the class passing JSON encoded Properties Class and returning a Long value of Job Execution ID whose MIME type is “text/plain” as a response to the PUT request for the Resource Method Path of “/simplejob”. 4. Implement the Resource Method of “simplejob(Properties props)” to start an instance of the Batch Job of “simplejob” passing the Properties object given by the parameter of this method. 5. Modify the Custom REST Application Class, RestApplication.java, of the “org.example.jsf_jpa_jaxrs_jbatch.rest” package under the “src/main/java” folder to add the BatchJob.class as a Web Resource Class. 6. A new System Test Class, BatchJobIT.java, of the “org.example.jsf_jpa_jaxrs_jbatch.batch.rest” package under the “src/test/java” folder is provided to execute a System Test for the Batch Job. 52 First Jakarta Batch Application Dev. Exercise

Slide 53

Slide 53 text

• BatchJobIT.java (1) – Preparation for System Test A new System Test Class, BatchJobIT.java, of the “org.example.jsf_jpa_jaxrs_jbatch.batch.rest” package under the “src/test/java” folder is provided to execute a System Test for the BatchJob.class. 53 class BatchJobIT { private static Client client; private static String baseUri = ""; @BeforeAll public static void loadTestConfig() { final String hostname = System.getProperty("servlet.host"); final String port = System.getProperty("servlet.port"); final String context = System.getProperty("servlet.context"); final String REST_APPLICATION_PATH = "webapi"; baseUri = "http://" + hostname + ":" + port + "/" + context + "/" + REST_APPLICATION_PATH; client = ClientBuilder.newClient(); } First Jakarta Batch Application Dev. Exercise

Slide 54

Slide 54 text

• BatchJobIT.java (2) – Execution of the System Test 54 @Test public void testSimpleBatchJob() { final String WEB_RESOURCE_PATH = "/batchJob"; String RESOURCE_METHOD_PATH = "/simplejob"; WebTarget myResource = client.target(baseUri + WEB_RESOURCE_PATH + RESOURCE_METHOD_PATH); Properties props = new Properties(); props.setProperty("parameter1", "value1"); Long execID = myResource.request(MediaType.TEXT_PLAIN). put(Entity.entity(props, MediaType.APPLICATION_JSON), Long.class); assertNotNull(execID); assertTrue(execID > 0L); } } First Jakarta Batch Application Dev. Exercise

Slide 55

Slide 55 text

• Final Java Source Folder Structure 55 Provided First Jakarta Batch Application Dev. Exercise New New New New New New Modify New Rename Modify

Slide 56

Slide 56 text

• Procedure of the Development Edit pom.xml and Change package names Change the Message folder and the associated Class Create the Required Classes Execute the REST System Test with Maven Test with Talend API Tester 56 First Jakarta Batch Application Dev. Exercise

Slide 57

Slide 57 text

• Execute JUnit through M2Eclipse™ for the provided REST System Test Class (1)  Right click on the project and select “Run As”  “4 Maven build” Specify “jsf_jpa_jaxrs_jbatch – integration-test” for the “Name” field and “clean integration-test” for the “Goals” field. 57 First Jakarta Batch Application Dev. Exercise

Slide 58

Slide 58 text

• Execute JUnit through M2Eclipse™ for the provided REST System Test Class (2)  Click “Run” button. Confirm that “BatchJobIT” test completes successfully on “Console” 58 First Jakarta Batch Application Dev. Exercise

Slide 59

Slide 59 text

• Procedure of the Development Edit pom.xml and Change package names Change the Message folder and the associated Class Create the Required Classes Execute the REST System Test with Maven Test with Talend API Tester 59 First Jakarta Batch Application Dev. Exercise

Slide 60

Slide 60 text

• Execute “Talend API Tester – Free Edition” (1) – Request REST Service  Click the icon of on the Chrome browser.  Select “PUT” Method, specify “http://localhost:8080/jsf_jpa_jaxrs_jbatch/webapi/batchJob/simplejob” as the URI field, add a header of “Content-Type: application/json”, put {“parameter1”:”value1”} in the BODY field and click the “Send” button. 60 First Jakarta Batch Application Dev. Exercise

Slide 61

Slide 61 text

• Execute “Talend API Tester – Free Edition” – Response for the REST Service Request  Confirm that the expected HEADERS and BODY were responded. 61 First Jakarta Batch Application Dev. Exercise Execution ID

Slide 62

Slide 62 text

Exercise 62

Slide 63

Slide 63 text

Processing Departments with Job Exercise • Required features  Create a new Job Definition File for the batch job of “personneljob” to process “Department” Entities.  Develop 4 Batch Artifacts: • “PersonnelReader”: Read all “Department” Entities from RDB with EnterpriseService Transactional CDI and save them to a local property. And then read a Department Entity from the local property. • “PersonnelProcessor”: Capitalize the name of the Department Entity • “PersonnelWriter”: Update the processed Items in RDB with EnterpriseService Transactional CDI. • “PersonnelBatchlet”: Show all the names of Department Entities with EnterpriseService Transactional CDI. 63 Jakarta EE Container Business Data Access Transactional CDI O-R Mapper Domain Service Business Methods RDB Batch Runtime Job “personneljob” Job “simplejob” Instance Batch Artifact (PersonnelReader.java) Batch Artifact (PersonnelProcessor.java) Batch Artifact (PersonnelWriter.java) Batch Artifact (PersonnelBatchlet.java) Job “simplejob” Instance Job Instance Job Definition File (personneljob.xml)

Slide 64

Slide 64 text

Processing Departments with Job Exercise • Required features Add 2 Resource Methods to the existing Web Resource Class of “BatchJob” • Start the Batch Job of “personneljob” with Properties passed in JSON and return the Execution ID as a Long value (“plain/text”). • Check the Job Status with Execution ID passed by a Query Parameter and return the Job Status as a String (“plain/text”). 64 Requestor Presentation Jakarta EE Container Web Resource CDI (Request Scoped) Resource Methods REST Request Message (URI:http://localhost:8080/jsf_jpa_jaxrs_jb atch/webapi/batchJob/personelJob/ Body(JSON): {“parameter1”:“value1”} REST Response Message (Long: “53”) JUnit5 System Test (integration-test) Eclipse Business Data Access Transactional CDI O-R Mapper Provider Domain Service Business Methods RDB Batch Runtime Job “personneljob” Job Instance Batch Client App. Job Instance Job Instance

Slide 65

Slide 65 text

Processing Departments with Job Exercise • PersonnelJobIT.java – System Test for the Batch Job processing Personnel Information 65 class PersonnelJobIT { private static Client client; private static String baseUri = ""; private static String WEB_RESOURCE_PATH = ""; private static String RESOURCE_METHOD_PATH = ""; private static String hostname = ""; private static String port = ""; private static String context = ""; private static String REST_APPLICATION_PATH = ""; private static HashMap departmentMap = new HashMap(); : Keep the maps between Primary Key and expected Name in Department Entities processed.

Slide 66

Slide 66 text

Processing Departments with Job Exercise • PersonnelJobIT.java – System Test for the Batch Job (prepareEntities(): Set Variables) 66 @BeforeAll public static void prepareEntities() { hostname = System.getProperty("servlet.host"); port = System.getProperty("servlet.port"); context = System.getProperty("servlet.context"); REST_APPLICATION_PATH = "webapi"; WEB_RESOURCE_PATH = "/personnelService"; baseUri = "http://" + hostname + ":" + port + "/" + context + "/" + REST_APPLICATION_PATH; client = ClientBuilder.newClient(); :

Slide 67

Slide 67 text

Processing Departments with Job Exercise • PersonnelJobIT.java – System Test for the Batch Job (prepareEntities(): Initialize DB) 67 // Initialize DB RESOURCE_METHOD_PATH = "/initializeDB"; WebTarget myResource = client.target(baseUri + WEB_RESOURCE_PATH + RESOURCE_METHOD_PATH); Integer result = myResource.request(MediaType.TEXT_PLAIN).get(Integer.class); assertEquals(result, new Integer(Response.Status.OK.getStatusCode())); :

Slide 68

Slide 68 text

Processing Departments with Job Exercise • PersonnelJobIT.java – System Test for the Batch Job (prepareEntities(): Create New Entities) 68 // Create a New Department Entity RESOURCE_METHOD_PATH = "/newDepartment"; Department department = new Department("Marketing"); myResource = client.target(baseUri + WEB_RESOURCE_PATH + RESOURCE_METHOD_PATH); department = myResource.request(MediaType.APPLICATION_JSON) .post(Entity.entity(department, MediaType.APPLICATION_JSON), Department.class); assertNotNull(department); assertEquals(department.getName(), "Marketing"); departmentMap.put(department.getId(),"MARKETING"); : } Set the maps between Primary Key and expected Name in Department Entities processed. In the same way, create Department Entities of “Sales” and “Development”

Slide 69

Slide 69 text

Processing Departments with Job Exercise • PersonnelJobIT.java – System Test for the Batch Job (testDepartmentJob(): Start Job) 69 @Test public void testDepartmentJob() { // Start a job named "personneljob" WEB_RESOURCE_PATH = "/batchJob"; String RESOURCE_METHOD_PATH = "/personneljob"; WebTarget myResource = client.target(baseUri + WEB_RESOURCE_PATH + RESOURCE_METHOD_PATH); Properties props = new Properties(); props.setProperty("parameter1", "value1"); Long execID = myResource.request(MediaType.TEXT_PLAIN).put(Entity.entity(props, MediaType.APPLICATION_JSON), Long.class); assertNotNull(execID); assertTrue(execID > 0L); :

Slide 70

Slide 70 text

Processing Departments with Job Exercise • PersonnelJobIT.java – System Test for the Batch Job (testDepartmentJob(): Check Job Status) 70 try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } // Check the status of the above job RESOURCE_METHOD_PATH = "/jobstatus"; myResource = client.target(baseUri + WEB_RESOURCE_PATH + RESOURCE_METHOD_PATH) .queryParam("execID", execID); String jobStatus = myResource.request(MediaType.TEXT_PLAIN).get(String.class); assertNotNull(jobStatus); assertTrue(jobStatus.length() > 0); assertEquals(jobStatus, "COMPLETED"); :

Slide 71

Slide 71 text

Processing Departments with Job Exercise • PersonnelJobIT.java – System Test for the Batch Job (testDepartmentJob(): Check Results) 71 // Check the results WEB_RESOURCE_PATH = "/personnelService"; RESOURCE_METHOD_PATH = "/getAllDepartments"; myResource = client.target(baseUri + WEB_RESOURCE_PATH + RESOURCE_METHOD_PATH); List departments = myResource.request(MediaType.APPLICATION_JSON) .get(new GenericType>() {}); assertNotNull(departments); assertEquals(departments.size(), 3); for (int i = 0; i < 3; i++) assertEquals(departmentMap.get(departments.get(i).getId()), departments.get(i).getName()); }

Slide 72

Slide 72 text

Processing Departments with Job Exercise • PersonnelJobIT.java – System Test for the Batch Job (deleteAllEntities(): Initialize DB) 72 @AfterAll public static void deleteAllEntities() { // Initialize DB WEB_RESOURCE_PATH = "/personnelService"; RESOURCE_METHOD_PATH = "/initializeDB"; WebTarget myResource = client.target(baseUri + WEB_RESOURCE_PATH + RESOURCE_METHOD_PATH); Integer result = myResource.request(MediaType.TEXT_PLAIN).get(Integer.class); assertEquals(result, new Integer(Response.Status.OK.getStatusCode())); }

Slide 73

Slide 73 text

Processing Departments with Job Exercise • Final Java Source Folder Structure 73 Provided Modify New New