Java EE 6 & Spring: A Lover’s Quarrel

Derrick Kittler Mauricio “Maltron” Leal Vamsi Chemitiganti Agenda o The goal of this talk o Evolution of Spring o Evolution of EE 6 o Side-by-Side o Migrating o Java EE 6 and Spring Coexistence o Conclusion Fair Comparison

Orange Apple Fair Comparison: NOT

Java EE 6 Specification Technology Framework Fair Comparison

Java EE 6

Approach to a Enterprise Solution !! If can’t beat them, join them !!!!

Java EE 6

Sometimes, a join solution can be the best approach The goal of this talk is NOT…

Compare both Java EE 6 technologies and declare a winner

The goal of this talk is…

• How to deliver a good enterprise application • Evaluate both technologies • Show some migration path (only when makes sense) Technology Choice: The big picture

Developer’s Costs Skill Set

Java EE 6

Vendor Monitoring Support

Infrastructure Skill Set

Evolution of Spring

Dependecy Proble Specific Annotation driving JSR 300 “at inject” A new “C” Mobile features Injection XML wiring New Spring namespace Modern Web POJO Extensible Automatic bean Expression Configuration Data Access oriented Configuration configuration Language profiles Social development Bean Scoping New annotation First-class REST Unified property Security Declarative Groovy, JRuby driven MVC support resolution Cloud Ready AOP & and BeanShell framework Java based Java configuration transactions JSP tag library JUnit 4-based configuration features MVC Java 5 integration testing Several new Servlet 3.0 support Framework autoboxing and Spring MVC Declarative generics features caching Support for JSR Spring MVC 303 declarative enhancements validation Scheduled Jobs What is Spring ? • Explicit Configuration • Aspect oriented • Ease-of-use abstractions over lower-level, general purpose API’s like JDBC, JMS and JavaMail • More flexibility for fine grained control, but more complexity • Deployment/Runtime complexity • Uses a Java EE container underneath

Spring Data Spring for Apache Hadoop

Redis HBase GemFire JPA QueryDSL HDFS MapReduce Hive

MongoDB Neo4j Soir JDBC Splunk Pig Cascading SI/Batch

AWS Beanstalk Spring Batch Spring Integration Spring Social Heroku Spring AMQP Twitter LinkedIn FaceBook Spring Web Flow Spring Web Services

OpenShift Spring Secure OAuth

Tomcat 5+

GlassFish 2.1+ DI AOP TX JMS JDBC ORM OXM Scheduling

WebLogic 9+ MVC REST JMX Testing Caching Profiles Expression

WebSphere 6.1+ Java EE 1.4+ / SES+

JPA 2.0 JSF 2.0 JSR 250 JSR 330 JSR 303 JTA JDBC 4.1 JMX 1.0+ Spring JMS Configuration Java EE 6 Evolution of J2EE (later Java EE)

May’98 Dec’99 Sep’01 Nov’03 May’06 Dec’09 Apr’13

Enterprise Robust Web Ease Productivit Application Scalable Services Development Lightweight y & HTML 5

Project J2EE 1.2 J2EE 1.3 J2EE 1.4 Java EE 5 Java EE 6 Java EE 7 JPE

EJB 1.0 EJB 1.1 EJB 2.0 EJB 2.1 EJB 3.0 Prunning JMS 2.0 Servlet 2.1 Servlet 1.1 (CMP, MDB, Servlet 2.4 JPA 1.0 Extensibility Batch JSP 1.1 local EJB) JSP 2.0 JSF 1.2 CDI Caching JMS 1.0.2 Servlets 2.3 Web Servlets 2.5 JAX-RS TX JDBC 2.0 (Event, Services JSP 2.1 Servlet 3.0 Interceptor JNDI 1.2 Filters) JMX (Common EJB 3.1 Lite WebSocket JAF 1.0 JSP 1.2 J2EE EL) JSON JTA 1.0 JDBC 2.1 Deploy JAXB, JAX-RS 2.0 JTS 0.95 JCA 1.0 JACC SAAJ, JavaMail 1.1 JAAS 1.0 JAAS WebService JAXP 1.0 JAX-RPC Injection JAXR JSTL

10 specs 13 specs 20 specs 24 specs 28 specs 32 specs Lessons learned with Spring

Project J2EE 1.2 J2EE 1.3 J2EE 1.4 Java EE 5 Java EE 6 Java EE 7 JPE

• One monolitic server • Lightweight and • Hard to decouple technologies Heavyweight servers (separated • Heavyweight by Profiles) • Technologies doesn’t depend solely on containers • CDI is a gamer changer • Arquilian is ALSO a gamer changer Java EE 6 Highlights • Extensibility • Enhanced easy of Development • JSF 2.1 • Built-In AJAX • Expanded View Model • Profiles Java • Vendor Support EE 6 • CDI (Contexts & )

What is CDI anyway ?

MyInterface hello = new MyImplementation(); What I want How is going to work What is CDI anyway ?

@Inject MyInterface hello; • Less coding = More Productivity • Server (Container) “injects” the other part FOR YOU • Loose coupling Hollywood Principle: • Refactoring easy “Don’t call us, • Easy Maintenance we will call you” Java EE 6 Side-by-Side • Java EE 6 and Spring are both capable middleware solutions • Functionally equivalent Java EE 6 for the most part, with very different approaches • For very vanilla applications, code is almost identical Java EE vs Spring Framework Features

Java EE 6 Spring Framework

Dependency JSR 250 JSR 330 CDI JSR 250 JSR 330 Spring IoC Injection Container Spring AOP Inteceptor Decorator AspectJ AOP

Persistence JPA 2 JPA 2 JDBC iBATIS JDO

Transactions JTA EJB 3.1 JTA JDBC JPA Hibernate JDO

Presentation Spring JSF 2 JSF 2 Struts Tapestry WebWork Framework MVC

Spring MVC JAX- Web JAX-WS JAX-RS JAX-WS XFire Services REST RPC

Messaging JMS EJB 3.1 JMS

Testing CDI EJB 3.1 JPA 2 JUnit TestNG Spring Framework and Java EE 6 (Side-by-Side)

Components Spring MVC/IoC/Security/JPA/JAX-WS JSF/CDI/JAAS/JPA/JAX-RS/JAX-WS

JBoss EAP 6 Server Tomcat 7

11 Kb (WAR file) File Size 32,153 Kb (WAR file)

JARs/ XMLs 0 / 4 36 / 5 Configuration 37 Lines 92 Pre-Deploy 33 Mb Heap / 48 Mb Perm Memory 23 Mb Heap / 21 Mb Perm Post-Deploy 41 Mb Heap / 84 Mb Perm Memory 107 Mb Heap/ 52 Mb Perm Memory (100 71 Mb Heap / 92 Mb Perm Threads) 81 Mb Heap/ 47 Mb Perm Response 2459 ms (average) Time 1100 ms (average) Should I migrate my current Spring application to Java EE 6 ? Why ? To migrate or not to migrate ? • Application is a legacy (written in early versions of Spring) • A real benefit will come Java EE 6 from a migration • Faster • More secure • Easy to manage • Support from multiple vendors Java EE 6 CDI and IoC

Java EE 6

@Named @Controller @RequestScoped @Scope(“request”) public class PersonBean { public class PersonBean {

@Inject @AutoWired private SocialID social; private SocialID social; Persistence

Java EE 6

@Stateless @Repository public class PersonDAO { public class PersonDAO {

@PersistenceContext @PersistenceContext private EntityManager em; private EntityManager em; Persistence

Java EE 6

@Stateless @Repository public class PersonDAO { public class PersonDAO {

@Resource @Autowired private DataSource ds; private JdbcTemplate temp;

public void save(Person p) { public void save(Person p) { try { temp.update( Connection c = “insert … values (?,?)”, ds.getConnection(); p.name(), p.age()); PreparedStatement ps = } c.prepareStatement(“… } ps.setString(1, p.name()); ps.setInt(2, p.age()); ps.execute(); … Transaction and Exception

Java EE 6

@TransactionAttribute(…) @Transactional(…) @Stateless @Repository public class PersonDAO { public class PersonDAO {

@PersistenceContext @PersistenceContext private EntityManager em; private EntityManager em; Transaction and Exception

Java EE 6

@ApplicationException(rollback=tr @Transactional(rollbackFor=DaoExc ue) eption.class) public class DAOException public class PersonController { extends Exception { Web

Java EE 6

@Named @Controller @RequestScoped @Transactional public class PersonMBean { public class PersonController {

private Person p; @Autowired private List ls; private PersonDAO dao;

@Inject @RequestMapping(“/add”) private PersonDAO dao; public ModelAndView add(Person p) { public void save() { dao.save(p); dao.save(p); ModelAndView mv = new resetForm(); ModelAndView(“ok”); } mv.addObject(“person”, … dao.list); return mv;

WebService (SOAP)

Java EE 6

@WebService @WebService public class PersonWS { public class PersonWS extends SpringBeanAutowiringSupport { @Inject private PersonDAO dao; @Autowired private PersonDAO dao; public List list() { return dao.list(); public List list() { } if(dao == null) … injectionContext(this);

… … WebService (REST)

Java EE 6

@Path(“/”) @Controller public class PersonResource { public class PersonResource {

@Inject @Autowired private PersonDAO dao; private PersonDAO dao;

@GET @RequestMapping( @Path(“/person/{id}”) value=“/person/{id}”, @Produces(“application/json”) produces={“application/json”} public Person method=RequestMethod.GET) load(@PathParam(“id”)Long ID) { @ResponseBody return dao.seek(ID); public Person } load(@PathVariable(“ID”)Long ID) … { return dao.seek(ID); … Messaging Queue

Java EE 6

InitialContext ic = … @Autowired JmsTemplate t; ConnectionFactory cf = … @Autowired Queue q; Connection c = … public void sendMessage() { Session s = c.createSession(… this.t.send(q, new TextMessage m = s.createText( MessageCreator() { “hello”); public q = (Queue)ic.lookup(… createMessage(Session s) throws QueueSender sender = … JMSException { Sender.send(m); return s.createTextMessage(“hello”); c.close(); }}); } Java EE 6 Java EE 6 and Spring coexistence

Java EE 6 • Integration with Java EE API’s • Spring beans can be injected into JSF Managed Beans • Spring beans can be referenced in EL with no JSF Backing beans • Spring JmsTemplate can be used on top of raw JMS API for convenience • Spring Listeners similar to EJB MDB especially JCA rather than JMS listeners • Hibernate validator standardized as (JSR 303) • Spring 3 supports excellent bi-directional integration with EJB’s • CDI and Spring Integration through the Spring Bridge to CDI

• Native support for Java EE • Java EE 6 annotations supported by Spring • Spring can use JPA / Hibernate natively

Integration • DataSources can use application Server QoS pooling, transactions, statement caching, debugging, monitoring and security Java EE 6 and Spring coexistence

Java EE 6 • Preference for Standardization • Vendor Neutrality Java EE 6 + CDI PlugIns • Simplicity • XML Aversion Java EE 6 + Spring Modules • Annotations

CDI + • Type-Safety Spring • Near Zero Configuration Spring + • Streamlined Stack CDI Plugins • Early Adopters Spring + Java EE 6 API’s • Some Risk Tolerance

Spring Java EE 6 and Spring coexistence

Java EE 6 • Some specialized integration needs • Interim technology updates Java EE 6 + CDI PlugIns • Slight less vendor neutral, but • Standards compatible Java EE 6 + Spring Modules

CDI + Spring

Spring + CDI Plugins

Spring + Java EE 6 API’s

Spring Java EE 6 and Spring coexistence

Java EE 6 • Specialized integration needs • Existing skill set Java EE 6 + CDI PlugIns • Some sacrifices to vendor neutrality

Java EE 6 + Spring Modules

CDI + Spring

Spring + CDI Plugins

Spring + Java EE 6 API’s

Spring Java EE 6 and Spring coexistence

Java EE 6 • Gradual migration • Skill set transfer Java EE 6 + CDI PlugIns • Technology agnosticism • Highly advanced integration needs Java EE 6 + Spring Modules • Aggressive technology adoption

CDI + Spring

Spring + CDI Plugins

Spring + Java EE 6 API’s

Spring Java EE 6 and Spring coexistence

Java EE 6 • Technology agnosticism • Advanced integration needs Java EE 6 + CDI PlugIns • Likely using JSF

Java EE 6 + Spring Modules

CDI + Spring

Spring + CDI Plugins

Spring + Java EE 6 API’s

Spring Java EE 6 and Spring coexistence

Java EE 6 • Existing skill set • Low risk tolerance Java EE 6 + CDI PlugIns • Standardization • Technology agnosticism Java EE 6 + Spring Modules • Uses Tomcat or more likely a Java

CDI + EE Server Spring • Likely using JSF, JPA, JMS, JAX-RS Spring + maybe EJB CDI Plugins Spring + Java EE 6 API’s

Spring Java EE 6 and Spring coexistence

Java EE 6 • Highly specialized custom system, likely on Tomcat Java EE 6 + CDI PlugIns • Established skill set • Wide deployment Java EE 6 + Spring Modules • Open Source activism

CDI + • Some vendor lock-in risk Spring Spring + CDI Plugins

Spring + Java EE 6 API’s

Spring Java EE 6 Conclusion • There is no practical reason to choose between the two • Balanced competition in Java EE 6 server-side Java is good for both developers and vendors • There is excellent integration possibilities