JEE.next()
Kuba Marchwicki
@kubem
Gdańsk, 26.09.2013
Perfection is achieved, not when
there is nothing more to add,
but when there is nothing left to
take away.
Antoine de Sai...
J2EE 1.2
Servlet, JSP,
EJB, JMS,
RMI
1999
J2EE 1.3
CMP, Connec
tor
Architecture
2001
J2EE 1.4
Web
Services,
Deployment,
As...
Servlet 3.0
JSF 2.0
EJB 3.1
JPA 2.0
JSP
CDI
JTA
Bean
Validation
JAX-WS
JAX-RPC
JAXR
SAAJ
JAX-RS
JAXB
JMS
JAAS
JASPIC
JACC
...
JSP 2.2 JSF 2.2
JAX-RS
2.0
EL 3.0
Servlet 3.1
Portable
extensions
CDI 1.1 Interceptors 1.1
Common
Annotations 1.1
Managed ...
JSP 2.2 JSF 2.2
JAX-RS
2.0
EL 3.0
Servlet 3.1
Portable
extensions
CDI 1.1 Interceptors 1.1
Common
Annotations 1.1
Managed ...
Java EE7 deep dive
• JMS 2.0
• JPA 2.1
• Bean Validation 1.1
• JAX-RS 2.0
• Java API for JSON
• Java API for Websockets
• ...
Before we start
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>pr...
Java EE7 deep dive
• JMS 2.0
• JPA 2.1
• Bean Validation 1.1
• JAX-RS 2.0
• Java API for JSON
• Java API for Websockets
• ...
JMS 2.0
Configuration - Old API
??
JMS 2.0
Configuration - Old API
<subsystem xmlns="urn:jboss:domain:messaging:1.1">
<hornetq-server>
<jms-destinations>
<jm...
JMS 2.0
Configuration – New API
@JMSConnectionFactoryDefinition(
name = "java:global/jms/demoConnectionFactory",
className...
JMS 2.0
Send message – Old API
@Stateless
public class SendMessageService {
@Resource(lookup = "java:global/jms/demoConnec...
JMS 2.0
Send message – New API
@Stateless
public class MessageService {
@Inject
private JMSContext context;
@Resource(mapp...
JMS 2.0
Receive message – New API
@MessageDriven(mappedName="global/jms/demoQueue")
public class MessageConsumer implement...
Java EE7 deep dive
• JMS 2.0
• JPA 2.1
• Bean Validation 1.1
• JAX-RS 2.0
• Java API for JSON
• Java API for Websockets
• ...
JPA 2.1
Old API - persistence.xml
<persistence-unit>
<properties>
<property name="hibernate.connection.driver_class"
value...
JPA 2.1
Old API - persistence.xml
<persistence-unit>
<properties>
<property name="eclipselink.jdbc.driver"
value="org.apac...
JPA 2.1
New API - persistence.xml
<persistence-unit>
<properties>
<property name="javax.persistence.jdbc.driver"
value="or...
JPA 2.1
Old API – Converters
public enum UseSex {
MALE, FEMALE;
}
@Entity
public class User {
@Id
private long id;
@Enumer...
JPA 2.1
Old API – Converters
public enum UseSex {
MALE, FEMALE;
}
@Entity
public class User {
@Id
private long id;
@Enumer...
JPA 2.1
Old API – Converters
@Entity
public class User {
@Transient
private UserSex sex;
private String db_sex;
@PostLoad
...
JPA 2.1
Old API – Converters
@PrePersist
public void beforePersit() {
switch(sex){
case MALE: db_sex = "M";
case FEMALE: d...
JPA 2.1
New API
@Entity
public class User {
@Id
private long id;
@Convert(converter = SexConverter.class)
private UserSex ...
JPA 2.1
New API
@Converter
public class SexConverter implements AttributeConverter<UserSex, String>{
public String convert...
JPA 2.1
New API – stored procedures
@Table(indexes = @Index(columnList = "name"))
@Entity
@NamedQuery(name = User.FIND_USE...
Java EE7 deep dive
• JMS 2.0
• JPA 2.1
• Bean Validation 1.1
• JAX-RS 2.0
• Java API for JSON
• Java API for Websockets
• ...
Bean Validation 1.1
Old API
@Interceptors(ValidationInterceptor.class)
public void addAuthor(@Size(min=5) String name,
Str...
Bean Validation 1.1
Old API
public Object validateMethodInvocation
(InvocationContext ctx) throws Exception {
MethodValida...
Bean Validation 1.1
New API
public void addAuthor(@Size(min=5) String name,
String surename) {
Author a = new Author();
a....
Bean Validation 1.1
New API
@Path("/hello")
public class HelloWorld {
@Path("/{name}")
@GET
@Produces(MediaType.APPLICATIO...
Java EE7 deep dive
• JMS 2.0
• JPA 2.1
• Bean Validation 1.1
• JAX-RS 2.0
• Java API for JSON
• Java API for Websockets
• ...
JAX-RS 2.0
Configuration - Old API
??
JAX-RS 2.0
Configuration - Old API
<servlet>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>
com.sun.jersey.sp...
JAX-RS 2.0
Configuration – New API
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPa...
JAX-RS 2.0
Client - Old API
HttpURLConnection connection =
(HttpURLConnection)serverAddress.openConnection();
connection.s...
JAX-RS 2.0
Client - Old API
<dependency>
<groupId>com.github.kevinsawicki</groupId>
<artifactId>http-request</artifactId>
...
JAX-RS 2.0
New API
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.We...
Java EE7 deep dive
• JMS 2.0
• JPA 2.1
• Bean Validation 1.1
• JAX-RS 2.0
• Java API for JSON
• Java API for Websockets
• ...
Java API for JSON Processing
{
"data": [
"Hello Jakub",
"Guten Tag Jakub"
]
}
Java API for JSON Processing
Old API
??
Java API for JSON Processing
New API – streaming API
JsonParser parser = Json.
createParser(new StringReader(string)));
ev...
Java API for JSON Processing
New API – object API
JsonReader reader = Json.
createReader(new StringReader(string));
JsonOb...
Java API for JSON Processing
New API – builder
import javax.json.Json;
import javax.json.JsonObject;
@Path("/simple/{name}...
Java EE7 deep dive
• JMS 2.0
• JPA 2.1
• Bean Validation 1.1
• JAX-RS 2.0
• Java API for JSON
• Java API for Websockets
• ...
Java API for Websockets
Old API #1
@WebServlet(urlPatterns = "/ping")
public class FeedNotifierWebSocket extends WebSocket...
Java API for Websockets
Old API #1
class NotificationInbound extends MessageInbound {
private WsOutbound outbound;
protect...
Java API for Websockets
Old API #2
@Singleton
@WebSocketEndpoint(path=”/chat”)
public class ChatServer {
Set<Session> peer...
Java API for Websockets
Old API #2
@WebSocketMessage
public void message(String message, Session client)
throws IOExceptio...
Java API for Websockets
New API
@Singleton
@ServerEndpoint("/ping")
public class NotificationServer {
Set<Session> session...
Java API for Websockets
New API
@OnMessage
public void message(String m, Session client)
throws IOException {
for (Session...
Java EE7 deep dive
• JMS 2.0
• JPA 2.1
• Bean Validation 1.1
• JAX-RS 2.0
• Java API for JSON
• Java API for Websockets
• ...
Batch Applications
Old API
??
Batch Applications
Item Reader
Item Processor
Item Writer
Jobs Repository
Job
Operator
Job Step
1 *
1
1
1
1
11
Batch Applications
Job Repository
holds information about jobs current running
and jobs that run in the past. JobOperator
...
Batch Applications
Job
encapsulates an entire batch process
<job id="myJob" xmlns="http://batch.jsr352/jsl">
<step id="myS...
Batch Applications
Chunks
@ItemReader
public class MyItemReader {
//...
}
@ItemProcessor
public class MyItemProcessor {
//...
Java EE7 deep dive
• JMS 2.0
• JPA 2.1
• Bean Validation 1.1
• JAX-RS 2.0
• Java API for JSON
• Java API for Websockets
• ...
The enterprise bean must not
attempt to manage threads. The
enterprise bean must not attempt
to start, stop, suspend, or r...
@Asynchronous
Old API
@Stateless
public class EventWatcher {
@Asynchronous
public void method(FeedEvent event) {
System.ou...
Asynchronous Servlets
Old API
@WebServlet(urlPatterns = "/somepath", asyncSupported = true)
public class AsyncServlet exte...
ManagedThreadFactory
New API
@Named
public class ThreadManager {
@Resource
ManagedThreadFactory mtf;
public ExecutorServic...
ManagedThreadFactory
New API
@Named
public class ProcessingService {
public void doMuchStuff(ExecutorService executor) {
f...
Java EE8 ??
• JSON-B (JSON binding)
• JCache (JSR 107)
• Adopt JSR
• Open TCK (??)
• More JSP (+ templates), less JSF
• no...
twitter: @kubem
http://github.com/kubamarchwicki/jee7-examples
http://www.slideshare.net/kubamarchwicki/jeenext
In case you ask – we are hiring
JEE.next()
JEE.next()
JEE.next()
Upcoming SlideShare
Loading in …5
×

JEE.next()

1,472 views

Published on

What's new in JEE7

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,472
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

JEE.next()

  1. 1. JEE.next() Kuba Marchwicki @kubem Gdańsk, 26.09.2013
  2. 2. Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away. Antoine de Saint-Exupery
  3. 3. J2EE 1.2 Servlet, JSP, EJB, JMS, RMI 1999 J2EE 1.3 CMP, Connec tor Architecture 2001 J2EE 1.4 Web Services, Deployment, Async Connector 2003 JEE 5 Prunning EJB 3.0, JPA, JSF, JAXB, JAX-WS, StAX 2006 Web profile Servlet 3.0 EJB 3.1 Lite 2009 Web profile JAX-RS 2.0 2013 JEE 6 Prunning CDI, JAX-RS JEE 7 Prunning JMS 2.0, Batch, JSON, Web sockets
  4. 4. Servlet 3.0 JSF 2.0 EJB 3.1 JPA 2.0 JSP CDI JTA Bean Validation JAX-WS JAX-RPC JAXR SAAJ JAX-RS JAXB JMS JAAS JASPIC JACC JCA JavaMail JSR 88 JSR 77 RMI JNDI Web profile Full profile Java EE6 profiles
  5. 5. JSP 2.2 JSF 2.2 JAX-RS 2.0 EL 3.0 Servlet 3.1 Portable extensions CDI 1.1 Interceptors 1.1 Common Annotations 1.1 Managed Beans 1.0 EJB 3.2 Connector 1.6 JPA 2.1 JTA 1.2 JMS 2.0 BeanValidation1.1 Concurrency Utilities (JSR 236) Batch Applications (JSR 352) Java API for JSON (JSR 353) Java API for Websockets (JSR 356) Java EE7
  6. 6. JSP 2.2 JSF 2.2 JAX-RS 2.0 EL 3.0 Servlet 3.1 Portable extensions CDI 1.1 Interceptors 1.1 Common Annotations 1.1 Managed Beans 1.0 EJB 3.2 Connector 1.6 JPA 2.1 JTA 1.2 JMS 2.0 BeanValidation1.1 Concurrency Utilities (JSR 236) Batch Applications (JSR 352) Java API for JSON (JSR 353) Java API for Websockets (JSR 356) Java EE7 deep dive
  7. 7. Java EE7 deep dive • JMS 2.0 • JPA 2.1 • Bean Validation 1.1 • JAX-RS 2.0 • Java API for JSON • Java API for Websockets • Batch Application • Concurrency Utilities
  8. 8. Before we start <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency>
  9. 9. Java EE7 deep dive • JMS 2.0 • JPA 2.1 • Bean Validation 1.1 • JAX-RS 2.0 • Java API for JSON • Java API for Websockets • Batch Application • Concurrency Utilities
  10. 10. JMS 2.0 Configuration - Old API ??
  11. 11. JMS 2.0 Configuration - Old API <subsystem xmlns="urn:jboss:domain:messaging:1.1"> <hornetq-server> <jms-destinations> <jms-queue name="testQueue"> <entry name="queue/test"/> </jms-queue> <jms-topic name="testTopic"> <entry name="topic/test"/> </jms-topic> </jms-destinations> </hornetq-server> </subsystem>
  12. 12. JMS 2.0 Configuration – New API @JMSConnectionFactoryDefinition( name = "java:global/jms/demoConnectionFactory", className = "javax.jms.ConnectionFactory") @JMSDestinationDefinition( name = "java:global/jms/demoQueue", interfaceName = "javax.jms.Queue", destinationName = "demoQueue") public class JmsConfiguration { }
  13. 13. JMS 2.0 Send message – Old API @Stateless public class SendMessageService { @Resource(lookup = "java:global/jms/demoConnectionFactory") ConnectionFactory connectionFactory; @Resource(lookup = "java:global/jms/demoQueue") Queue demoQueue; public void sendMessage(String payload) { try { Connection connection = connectionFactory.createConnection(); try { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer messageProducer = session .createProducer(demoQueue); TextMessage textMessage = session.createTextMessage(payload); messageProducer.send(textMessage); } finally { //...
  14. 14. JMS 2.0 Send message – New API @Stateless public class MessageService { @Inject private JMSContext context; @Resource(mappedName = "jms/inboundQueue") private Queue inboundQueue; public void sendMessage(String payload) { context.createProducer().send(inboundQueue, payload); } }
  15. 15. JMS 2.0 Receive message – New API @MessageDriven(mappedName="global/jms/demoQueue") public class MessageConsumer implements MessageListener { @Override public void onMessage(Message msg) { try { //No casting!!! String payload = msg.getBody(String.class); } catch (JMSException e) { e.printStackTrace(); } } }
  16. 16. Java EE7 deep dive • JMS 2.0 • JPA 2.1 • Bean Validation 1.1 • JAX-RS 2.0 • Java API for JSON • Java API for Websockets • Batch Application • Concurrency Utilities
  17. 17. JPA 2.1 Old API - persistence.xml <persistence-unit> <properties> <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver" /> <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/sample;create=true" /> <property name="hibernate.connection.username" value="user" /> <property name="hibernate.connection.password" value="pass" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.use_sql_comments" value="true" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /> <property name="hibernate.hbm2ddl.auto" value="create-drop" /> </properties> </persistence-unit>
  18. 18. JPA 2.1 Old API - persistence.xml <persistence-unit> <properties> <property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" /> <property name="eclipselink.jdbc.url" value="jdbc:derby://localhost:1527/sample;create=true" /> <property name="eclipselink.jdbc.user" value="user" /> <property name="eclipselink.jdbc.password" value="pass" /> <property name="eclipselink.logging.level" value="FINE" /> <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.MySQLPlatform" /> <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> </properties> </persistence-unit>
  19. 19. JPA 2.1 New API - persistence.xml <persistence-unit> <properties> <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" /> <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample;create=true" /> <property name="javax.persistence.jdbc.user" value="user" /> <property name="javax.persistence.jdbc.password" value="pass" /> <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" /> <property name="eclipselink.logging.level" value="FINE" /> <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.MySQLPlatform" /> </properties> </persistence-unit>
  20. 20. JPA 2.1 Old API – Converters public enum UseSex { MALE, FEMALE; } @Entity public class User { @Id private long id; @Enumerated(EnumType.ORDINAL) private UserSex sex; //.. }
  21. 21. JPA 2.1 Old API – Converters public enum UseSex { MALE, FEMALE; } @Entity public class User { @Id private long id; @Enumerated(EnumType.STRING) private UserSex sex; //.. }
  22. 22. JPA 2.1 Old API – Converters @Entity public class User { @Transient private UserSex sex; private String db_sex; @PostLoad public void afterLoad() { switch(db_sex){ case "M": sex = UserSex.MALE; case "F": sex = UserSex.FEMALE; default: throw new IllegalArgumentException(); } }
  23. 23. JPA 2.1 Old API – Converters @PrePersist public void beforePersit() { switch(sex){ case MALE: db_sex = "M"; case FEMALE: db_sex = "F"; default: throw new IllegalArgumentException(); } }
  24. 24. JPA 2.1 New API @Entity public class User { @Id private long id; @Convert(converter = SexConverter.class) private UserSex sex; //.. }
  25. 25. JPA 2.1 New API @Converter public class SexConverter implements AttributeConverter<UserSex, String>{ public String convertToDatabaseColumn(UserSex arg0) { switch(arg0){ case MALE: return "M"; case FEMALE: return "F"; default: throw new IllegalArgumentException(); } } public UserSex convertToEntityAttribute(String arg0) { switch(arg0){ case "M": return UserSex.MALE; case "F": return UserSex.FEMALE; default: throw new IllegalArgumentException(); } } }
  26. 26. JPA 2.1 New API – stored procedures @Table(indexes = @Index(columnList = "name")) @Entity @NamedQuery(name = User.FIND_USER_BY_NAME, query = "from User u where name = ?") @NamedStoredProcedureQuery(name = User. REFRESH_USERS, procedureName = "USR_STRD_PRCR_CALL") public class User { public final static String FIND_USER_BY_NAME = "User.findByName"; public final static String REFRESH_USERS = "User.doSomething"; //.. }
  27. 27. Java EE7 deep dive • JMS 2.0 • JPA 2.1 • Bean Validation 1.1 • JAX-RS 2.0 • Java API for JSON • Java API for Websockets • Batch Application • Concurrency Utilities
  28. 28. Bean Validation 1.1 Old API @Interceptors(ValidationInterceptor.class) public void addAuthor(@Size(min=5) String name, String surename) { Author a = new Author(); a.setName(name); a.setSurename(surename); em.persist(a); }
  29. 29. Bean Validation 1.1 Old API public Object validateMethodInvocation (InvocationContext ctx) throws Exception { MethodValidator validator = validatorFactory.getValidator() .unwrap(MethodValidator.class); Set<MethodConstraintViolation<Object>> violations = validator.validateAllParameters( ctx.getTarget(), ctx.getMethod(), ctx.getParameters()); }
  30. 30. Bean Validation 1.1 New API public void addAuthor(@Size(min=5) String name, String surename) { Author a = new Author(); a.setName(name); a.setSurename(surename); em.persist(a); }
  31. 31. Bean Validation 1.1 New API @Path("/hello") public class HelloWorld { @Path("/{name}") @GET @Produces(MediaType.APPLICATION_JSON) public JsonObject sayHello( @NotEmpty @PathParam("name") String name) { //.. } }
  32. 32. Java EE7 deep dive • JMS 2.0 • JPA 2.1 • Bean Validation 1.1 • JAX-RS 2.0 • Java API for JSON • Java API for Websockets • Batch Application • Concurrency Utilities
  33. 33. JAX-RS 2.0 Configuration - Old API ??
  34. 34. JAX-RS 2.0 Configuration - Old API <servlet> <servlet-name>JAX-RS Servlet</servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>JAX-RS Servlet</servlet-name> <url-pattern>/jax-rs/*</url-pattern> </servlet-mapping>
  35. 35. JAX-RS 2.0 Configuration – New API import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/rs") public class RestApp extends Application { }
  36. 36. JAX-RS 2.0 Client - Old API HttpURLConnection connection = (HttpURLConnection)serverAddress.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setReadTimeout(10000); BufferedReader rd = new BufferedReader( new InputStreamReader(connection.getInputStream())); StringBuilder sb = new StringBuilder(); while ((line = rd.readLine()) != null) { sb.append(line + 'n'); } System.out.println(sb.toString());
  37. 37. JAX-RS 2.0 Client - Old API <dependency> <groupId>com.github.kevinsawicki</groupId> <artifactId>http-request</artifactId> <version>5.4.1</version> </dependency> HttpRequest request = HttpRequest.get(baseURL) .receive(output); System.out.println(request.toString());
  38. 38. JAX-RS 2.0 New API import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; Client client = ClientBuilder.newBuilder().build(); WebTarget target = client.target(uri.toString()); Response response = target.request().get(); assertThat(response.getStatus()).isEqualTo(200); assertThat(target.request().get(String.class)) .isEqualTo("{}");
  39. 39. Java EE7 deep dive • JMS 2.0 • JPA 2.1 • Bean Validation 1.1 • JAX-RS 2.0 • Java API for JSON • Java API for Websockets • Batch Application • Concurrency Utilities
  40. 40. Java API for JSON Processing { "data": [ "Hello Jakub", "Guten Tag Jakub" ] }
  41. 41. Java API for JSON Processing Old API ??
  42. 42. Java API for JSON Processing New API – streaming API JsonParser parser = Json. createParser(new StringReader(string))); event = parser.next(); assertThat(event).is(new Condition<Object>() { public boolean matches(Object value) { return value instanceof Event && value == Event.VALUE_STRING; } }); assertThat(parser.getString()).isEqualTo("Hello Jakub");
  43. 43. Java API for JSON Processing New API – object API JsonReader reader = Json. createReader(new StringReader(string)); JsonObject obj = reader.readObject(); assertThat(obj.containsKey("data")).isTrue(); JsonArray results = obj.getJsonArray("data"); assertThat(results.size()).isEqualTo(2); assertThat(results.getString(0)).isEqualTo("Hello Jakub"); assertThat(results.getString(1)).isEqualTo("Guten tag Jakub");
  44. 44. Java API for JSON Processing New API – builder import javax.json.Json; import javax.json.JsonObject; @Path("/simple/{name}") @GET @Produces(MediaType.APPLICATION_JSON) public JsonObject saySimpleHello(@PathParam("name") String name) { return Json.createObjectBuilder() .add("data", Json.createArrayBuilder() .add("Hello " + name) .add("Guten tag " + name) .build()) .build(); }
  45. 45. Java EE7 deep dive • JMS 2.0 • JPA 2.1 • Bean Validation 1.1 • JAX-RS 2.0 • Java API for JSON • Java API for Websockets • Batch Application • Concurrency Utilities
  46. 46. Java API for Websockets Old API #1 @WebServlet(urlPatterns = "/ping") public class FeedNotifierWebSocket extends WebSocketServlet { protected StreamInbound createWebSocketInbound( String subprotocol, HttpServletRequest req) { //.. } }
  47. 47. Java API for Websockets Old API #1 class NotificationInbound extends MessageInbound { private WsOutbound outbound; protected void onOpen(WsOutbound outbound) { this.outbound = outbound; } protected void onBinaryMessage(ByteBuffer m) { outbound.writeBinaryMessage(message); } protected void onTextMessage(CharBuffer m) { outbound.writeTextMessage(message); } }
  48. 48. Java API for Websockets Old API #2 @Singleton @WebSocketEndpoint(path=”/chat”) public class ChatServer { Set<Session> peers = new HashSet<>(); @WebSocketOpen public void onOpen(Session peer) { peers.add(session); } @WebSocketClose public void onClose(Session session) { peers.remove(session); } ...
  49. 49. Java API for Websockets Old API #2 @WebSocketMessage public void message(String message, Session client) throws IOException { for (Session session : peers) { if (!session.equals(client)) { session.getRemote().sendObject(message); } } }
  50. 50. Java API for Websockets New API @Singleton @ServerEndpoint("/ping") public class NotificationServer { Set<Session> sessions = new HashSet<>(); @OnOpen public void onOpen(Session s) throws IOException { sessions.add(s); } @OnClose public void onClose(Session s) throws IOException { sessions.remove(s); } ...
  51. 51. Java API for Websockets New API @OnMessage public void message(String m, Session client) throws IOException { for (Session s: sessions) { if (s == client) continue; try { s.getBasicRemote().sendText(m); } catch (Exception e) { onClose(s); } } } }
  52. 52. Java EE7 deep dive • JMS 2.0 • JPA 2.1 • Bean Validation 1.1 • JAX-RS 2.0 • Java API for JSON • Java API for Websockets • Batch Application • Concurrency Utilities
  53. 53. Batch Applications Old API ??
  54. 54. Batch Applications Item Reader Item Processor Item Writer Jobs Repository Job Operator Job Step 1 * 1 1 1 1 11
  55. 55. Batch Applications Job Repository holds information about jobs current running and jobs that run in the past. JobOperator provides access to this repository. Job Operator an interface to manage all aspects of job processing, including operational commands, such as start, restart, and stop, retrieval of job and step executions.
  56. 56. Batch Applications Job encapsulates an entire batch process <job id="myJob" xmlns="http://batch.jsr352/jsl"> <step id="myStep" > <chunk reader="MyItemReader" writer="MyItemWriter" processor="MyItemProcessor" buffer-size="5" checkpoint-policy="item" commit-interval="10" /> </step> </job>
  57. 57. Batch Applications Chunks @ItemReader public class MyItemReader { //... } @ItemProcessor public class MyItemProcessor { //... } @ItemWriter public class MyItemWriter { //... }
  58. 58. Java EE7 deep dive • JMS 2.0 • JPA 2.1 • Bean Validation 1.1 • JAX-RS 2.0 • Java API for JSON • Java API for Websockets • Batch Application • Concurrency Utilities
  59. 59. The enterprise bean must not attempt to manage threads. The enterprise bean must not attempt to start, stop, suspend, or resume a thread, or to change a thread’s priority or name. The enterprise bean must not attempt to manage thread groups. 21.2.2. Programming restrictions
  60. 60. @Asynchronous Old API @Stateless public class EventWatcher { @Asynchronous public void method(FeedEvent event) { System.out.println(event); } }
  61. 61. Asynchronous Servlets Old API @WebServlet(urlPatterns = "/somepath", asyncSupported = true) public class AsyncServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //.. } }
  62. 62. ManagedThreadFactory New API @Named public class ThreadManager { @Resource ManagedThreadFactory mtf; public ExecutorService getThreadManager() { return new ThreadPoolExecutor(5,10, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100), mtf); } }
  63. 63. ManagedThreadFactory New API @Named public class ProcessingService { public void doMuchStuff(ExecutorService executor) { for (int i = 0; i < 50; i++) { Runnable worker = new WorkerThread("" + i); executor.execute(worker); } executor.shutdown(); } }
  64. 64. Java EE8 ?? • JSON-B (JSON binding) • JCache (JSR 107) • Adopt JSR • Open TCK (??) • More JSP (+ templates), less JSF • no more EARs (??)
  65. 65. twitter: @kubem http://github.com/kubamarchwicki/jee7-examples http://www.slideshare.net/kubamarchwicki/jeenext
  66. 66. In case you ask – we are hiring

×