Slide 1

Slide 1 text

Java EE 7 New Backend Features Dmitry Buzdin

Slide 2

Slide 2 text

Dmitry Buzdin [email protected] @buzdin www.buzdin.lv

Slide 3

Slide 3 text

Agenda • About Java EE 7 • About JSRs • About Containers and Reference Implementations • New Features: Batch, Concurrency • Updates: JPA, CDI etc.

Slide 4

Slide 4 text

Java EE 7

Slide 5

Slide 5 text

What, When? • Java Enterprise Edition 7 • Released on 12th of June 2013

Slide 6

Slide 6 text

Java EE 7 Focus More annotations APIs work together Less code WebSockets JSON Servlet 3.1 NIO REST Batch Concurrency JMS

Slide 7

Slide 7 text

Java EE 7 Content JSR 339 JSR 236 JSR 342

Slide 8

Slide 8 text

Java EE 7 JSR List • Java Platform, Enterprise Edition 7 (JSR 342) • Concurrency Utilities for Java EE 1.0 (JSR 236) (New to JEE!) • Java Persistence 2.1 (JSR 338) • etc. http://java.dzone.com/articles/java-ee-7-approved

Slide 9

Slide 9 text

What is JSR? • Java Specification Request • Based on Java Community Process • http://jcp.org

Slide 10

Slide 10 text

JCP Process JCP is a formal process to develop Java JCP process has versions Current version is 2.9 https://www.jcp.org/en/procedures/overview

Slide 11

Slide 11 text

JCP Approval https://www.jcp.org/en/jsr/results?id=5513

Slide 12

Slide 12 text

JCP Membership • commercial entities: $5000 • educational/non-profit organizations: $2000 • individuals: $0 • Java User Groups: $0 https://jcp.org/en/participation/membership

Slide 13

Slide 13 text

Containers & RIs

Slide 14

Slide 14 text

Java EE 7 Containers http://www.oracle.com/technetwork/java/javaee/overview/ compatibility-jsp-136984.html

Slide 15

Slide 15 text

Containers vs RI • Containers have open source RI inside

Slide 16

Slide 16 text

Java EE has Two Profiles • Full Profile • Web Profile EJB Lite inside

Slide 17

Slide 17 text

Batch 1.0 JSR-352

Slide 18

Slide 18 text

The Problem • Process stuff periodically • Flow of transformations • Exception handling

Slide 19

Slide 19 text

Current Solutions • DIY and pretend smart • SpringBatch • Oracle

Slide 20

Slide 20 text

Domain Concepts Step: chunk or batchlet

Slide 21

Slide 21 text

Chunk-Oriented

Slide 22

Slide 22 text

Job DSL (JSL) !

Slide 23

Slide 23 text

ItemReader @Named public class SimpleItemReader extends AbstractItemReader { ! @Inject JobContext jobContext; public void open(Serializable checkpoint) { } public void close() {} ! public abstract Object readItem(); public Serializable checkpointInfo() { …. } }

Slide 24

Slide 24 text

ItemProcessor @Named public class SimpleItemProcessor implements ItemProcessor { ! @Inject private JobContext jobContext; ! public Object processItem(Object obj) throws Exception { return null; } }

Slide 25

Slide 25 text

ItemWriter @Named public class SimpleItemWriter extends AbstractItemWriter { ! @PersistenceContext EntityManager em; ! public void writeItems(List list) throws Exception { for (Object obj : list) { System.out.println("PayrollRecord: " + obj); em.persist(obj); } } }

Slide 26

Slide 26 text

Start Job private long startNewBatchJob() throws Exception { JobOperator jobOperator = BatchRuntime.getJobOperator(); Properties props = new Properties(); props.setProperty("payrollInputDataFileName", payrollInputDataFileName); return jobOperator.start(JOB_NAME, props); }

Slide 27

Slide 27 text

Execution Audit public interface JobExecution { long getExecutionId(); java.lang.String getJobName(); javax.batch.runtime.BatchStatus getBatchStatus(); java.util.Date getStartTime(); java.util.Date getEndTime(); java.lang.String getExitStatus(); java.util.Date getCreateTime(); java.util.Date getLastUpdatedTime(); java.util.Properties getJobParameters(); }

Slide 28

Slide 28 text

Features • Retries • Transactions • Parallel flows • Existing writers/readers

Slide 29

Slide 29 text

Implementations • JBatch (https://java.net/projects/jbatch/) • Spring Batch (http://projects.spring.io/spring-batch/ ) RI

Slide 30

Slide 30 text

Summary • Chunk oriented and transactional • Simple and extensible

Slide 31

Slide 31 text

Concurrency 1.0 JSR-236

Slide 32

Slide 32 text

Concurrency 1.0 JSR-236 2003 - JSR filed

Slide 33

Slide 33 text

The Problem @Stateless! public class MyService { ExecutorService executorService = Executors.newFixedThreadPool(10); }

Slide 34

Slide 34 text

Even Worse • Where does framework take threads from? • It is possible to start a thread, but it will run in separate context.

Slide 35

Slide 35 text

Current Solutions • CommonJ • JNDI, JCA • Unmanaged threads

Slide 36

Slide 36 text

The Promise • It is not your problem anymore • Just use standard APIs

Slide 37

Slide 37 text

The Architecture

Slide 38

Slide 38 text

Managed Objects • ManagedExecutorService –The interface for submitting asynchronous tasks from a container. • ManagedScheduledExecutorService – The interface for scheduling tasks to run after a given delay or execute periodically. • ContextService – The interface for creating contextual objects. • ManagedThreadFactory – The interface for creating managed threads.

Slide 39

Slide 39 text

Submitting Task public class TestServlet extends HttpPServlet {
 @Resource(name=“java:comp/DefaultManagedExecutorService”)
 ManagedExecutorService executor;
 
 Future future = executor.submit(new MyTask());
 
 class MyTask implements Runnable {
 public void run() { 
 . . . // task logic
 }
 }
 }

Slide 40

Slide 40 text

Task Events Notifications

Slide 41

Slide 41 text

Lifecycle void taskSubmitted(Future future, ManagedExecutorService executor, Object task); ! void taskStarting(Future future, ManagedExecutorService executor, Object task); ! void taskAborted(Future future, ManagedExecutorService executor, Throwable exp); ! void taskDone(Future future, ManagedExecutorService executor, Throwable exp);

Slide 42

Slide 42 text

ManagedTaskListeners public class TaskWithListener implements Runnable, ManagedTask { ! public ManagedTaskListener getManagedTaskListener() { return aManagedTaskListener; } } Runnable task; ManagedTaskListener listener; Runnable taskWithListener = ManagedExecutors.managedTask(task, listener); ! ManagedExecutorService executor = ....; executor.submit(taskWithListener);

Slide 43

Slide 43 text

JPA 2.1 JSR-338

Slide 44

Slide 44 text

Java Persistence API • Hibernate (http://hibernate.org/) • EclipseLink (https://www.eclipse.org/eclipselink/) • OpenJPA (http://openjpa.apache.org/) • DataNucleus (http://www.datanucleus.org/) RI

Slide 45

Slide 45 text

Schema Generation

Slide 46

Slide 46 text

Indexes @Entity @Table(indexes = { @Index(columnList = "ISBN"), @Index(columnList = "NBOFPAGE") }) public class Book { @Id @GeneratedValue private Long id; private String isbn; private Integer nbOfPage; ... }

Slide 47

Slide 47 text

Stored Procedures @Entity
 @NamedStoredProcedureQuery(name = "archiveOldBooks", procedureName = "sp_archive_books", parameters = { @StoredProcedureParameter(name = ”date", mode = IN, type = Date.class), @StoredProcedureParameter(name = "warehouse", mode = IN, type = String.class) public class Book {...} StoredProcedureQuery query = em.createNamedStoredProcedureQuery(“archiveOldBooks") .setParameter(“data”, new Date()) .setParameter(“warehouse”, “1”) .getResultList();

Slide 48

Slide 48 text

Converters @Entity public class Employee { ... @Convert(converter=BooleanTFConverter.class) private Boolean isActive; ... } @Converter public class BooleanTFConverter implements AttributeConverter{ @Override public String convertToDatabaseColumn(Boolean value) { if (value) { return "T"; } else { return "F"; } } @Override public Boolean convertToEntityAttribute(String value) { return "T".equals(value); } }

Slide 49

Slide 49 text

Criteria Update / Delete CriteriaBuilder cb = em.getCriteriaBuilder(); // Updates the salary to 90,000 of all Employee's making more than 100,000. CriteriaUpdate update = cb.createCriteriaUpdate(Employee.class); Root e = update.from(Employee.class); update.set("salary", 90000); update.where(cb.greaterThan(e.get("salary"), 100000)); Query query = em.createQuery(update); int rowCount = query.executeUpdate();

Slide 50

Slide 50 text

CDI Updates

Slide 51

Slide 51 text

@Alternative @Priority(Interceptor.Priority.APPLICATION+10) public class TestCoderImpl implements Coder { ... } @Priority

Slide 52

Slide 52 text

Vetoed @Vetoed public class NonProcessedBean { ... }

Slide 53

Slide 53 text

Beans Discovery ! ! ! ! !

Slide 54

Slide 54 text

Summary

Slide 55

Slide 55 text

Good News • No radical changes • Java 7 Adoption • Modern API approaches • Java EE becomes better • Modular

Slide 56

Slide 56 text

Bad News • Implementations • Migration from Java EE 6 is not easy • Lots of bugs due to complexity

Slide 57

Slide 57 text

Suggestion • Use Java SE and RIs • Upgrade components when necessary

Slide 58

Slide 58 text

How to Start Using • JavaEE 7 RIs are conflicting with JavaEE 6 • Start with RIs in SE context

Slide 59

Slide 59 text

No content