Share
Embed
Java EE 7 New Backend Features
Overview of new backend features released in Java EE 7.
Dmitry Buzdin
March 06, 2014
Transcript
59 slides
-
Agenda • About Java EE 7 • About JSRs •
About Containers and Reference Implementations • New Features: Batch, Concurrency • Updates: JPA, CDI etc. -
What, When? • Java Enterprise Edition 7 • Released on
12th of June 2013 -
Java EE 7 Focus More annotations APIs work together Less
code WebSockets JSON Servlet 3.1 NIO REST Batch Concurrency JMS -
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 -
What is JSR? • Java Specification Request • Based on
Java Community Process • http://jcp.org -
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 -
JCP Membership • commercial entities: $5000 • educational/non-profit organizations: $2000
• individuals: $0 • Java User Groups: $0 https://jcp.org/en/participation/membership -
Java EE has Two Profiles • Full Profile • Web
Profile EJB Lite inside -
The Problem • Process stuff periodically • Flow of transformations
• Exception handling -
Job DSL (JSL) <job id="SimplePayrollJob" xmlns=http://xmlns.jcp.org/xml/ns/javaee version="1.0"> <step id="process"> <chunk
item-count="2"> <reader ref="simpleItemReader"/> <processor ref="simpleItemProcessor"/> <writer ref="simpleItemWriter"/> </chunk> </step> </job> ! -
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() { …. } } -
ItemProcessor @Named public class SimpleItemProcessor implements ItemProcessor { ! @Inject
private JobContext jobContext; ! public Object processItem(Object obj) throws Exception { return null; } } -
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); } } } -
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); } -
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(); } -
Features • Retries • Transactions • Parallel flows • Existing
writers/readers -
The Problem @Stateless! public class MyService { ExecutorService executorService =
Executors.newFixedThreadPool(10); } -
Even Worse • Where does framework take threads from? •
It is possible to start a thread, but it will run in separate context. -
The Promise • It is not your problem anymore •
Just use standard APIs -
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. -
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 } } } -
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); -
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); -
Java Persistence API • Hibernate (http://hibernate.org/) • EclipseLink (https://www.eclipse.org/eclipselink/) •
OpenJPA (http://openjpa.apache.org/) • DataNucleus (http://www.datanucleus.org/) RI -
Schema Generation <persistence ... version="2.1"> <persistence-unit ...> <properties> <property name="javax.persistence.schema-generation.scripts.action"
value="drop-and-create"/> <property name="javax.persistence.schema-generation.scripts.create-target" value="create.ddl"/> <property name="javax.persistence.sql-load-script-source" value="insert.sql"/> </properties> </persistence-unit> -
Indexes @Entity @Table(indexes = { @Index(columnList = "ISBN"), @Index(columnList =
"NBOFPAGE") }) public class Book { @Id @GeneratedValue private Long id; private String isbn; private Integer nbOfPage; ... } -
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(); -
Converters @Entity public class Employee { ... @Convert(converter=BooleanTFConverter.class) private Boolean
isActive; ... } @Converter public class BooleanTFConverter implements AttributeConverter<Boolean, String>{ @Override public String convertToDatabaseColumn(Boolean value) { if (value) { return "T"; } else { return "F"; } } @Override public Boolean convertToEntityAttribute(String value) { return "T".equals(value); } } -
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(); -
Beans Discovery <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"> ! <scan> <exclude
name="com.acme.rest.*" /> ! <exclude name="com.acme.faces.**"> <if-class-not-available name="javax.faces.context.FacesContext"/> </exclude> ! <exclude name="com.acme.verbose.*"> <if-system-property name="verbosity" value="low"/> </exclude> ! <exclude name="com.acme.ejb.**"> <if-class-available name="javax.enterprise.inject.Model"/> <if-system-property name="exclude-ejbs"/> </exclude> </scan> ! </beans> -
Good News • No radical changes • Java 7 Adoption
• Modern API approaches • Java EE becomes better • Modular -
Bad News • Implementations • Migration from Java EE 6
is not easy • Lots of bugs due to complexity -
Suggestion • Use Java SE and RIs • Upgrade components
when necessary -
How to Start Using • JavaEE 7 RIs are conflicting
with JavaEE 6 • Start with RIs in SE context