Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java EE 7 New Backend Features

Java EE 7 New Backend Features

Overview of new backend features released in Java EE 7.

Dmitry Buzdin

March 06, 2014

Transcript

59 slides
  1. Agenda • About Java EE 7 • About JSRs •

    About Containers and Reference Implementations • New Features: Batch, Concurrency • Updates: JPA, CDI etc.
  2. Java EE 7 Focus More annotations APIs work together Less

    code WebSockets JSON Servlet 3.1 NIO REST Batch Concurrency JMS
  3. 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
  4. What is JSR? • Java Specification Request • Based on

    Java Community Process • http://jcp.org
  5. 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
  6. JCP Membership • commercial entities: $5000 • educational/non-profit organizations: $2000

    • individuals: $0 • Java User Groups: $0 https://jcp.org/en/participation/membership
  7. 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> !
  8. 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() { …. } }
  9. ItemProcessor @Named public class SimpleItemProcessor implements ItemProcessor { ! @Inject

    private JobContext jobContext; ! public Object processItem(Object obj) throws Exception { return null; } }
  10. 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); } } }
  11. 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); }
  12. 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(); }
  13. Even Worse • Where does framework take threads from? •

    It is possible to start a thread, but it will run in separate context.
  14. 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.
  15. 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
 }
 }
 }
  16. 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);
  17. 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);
  18. Java Persistence API • Hibernate (http://hibernate.org/) • EclipseLink (https://www.eclipse.org/eclipselink/) •

    OpenJPA (http://openjpa.apache.org/) • DataNucleus (http://www.datanucleus.org/) RI
  19. 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>
  20. Indexes @Entity @Table(indexes = { @Index(columnList = "ISBN"), @Index(columnList =

    "NBOFPAGE") }) public class Book { @Id @GeneratedValue private Long id; private String isbn; private Integer nbOfPage; ... }
  21. 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();
  22. 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); } }
  23. 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();
  24. 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>
  25. Good News • No radical changes • Java 7 Adoption

    • Modern API approaches • Java EE becomes better • Modular
  26. Bad News • Implementations • Migration from Java EE 6

    is not easy • Lots of bugs due to complexity
  27. How to Start Using • JavaEE 7 RIs are conflicting

    with JavaEE 6 • Start with RIs in SE context

More Decks by Dmitry Buzdin

Other Decks in Programming