Post on 15-Jul-2015
transcript
• RESTfull development
• Introduction to JavaEE
• Context and Dependency Injection
• WSO2 Application Server
2
Agenda
4
REST Let’s take an
example
REpresentational State Transfer
An architecture style of networked systems
5
Premier
Cooperate
Regular
Customer Care
Answering Machine
Regular Customer
Representative
Premier Customer
Representative
Cooperate Customer
Representative
• Example - Customer care system of a bank - The bank provides a single telephone number
REST
6
Premier
Cooperate
Regular
Answering Machine
Regular Customer
Representative
Premier Customer
Representative
Cooperate Customer
Representative
REST
Answering Machine
Answering Machine
• Example - Customer care system of a bank - The bank provides a multiple(unique) telephone numbers
7
Premier
Cooperate
Regular
Customer Care
Answering Machine
Regular Customer
Representative
Premier Customer
Representative
Cooperate Customer
Representative
• Example - Customer care system of a bank - The bank provides a single telephone number
REST
Not REST
8
Premier
Cooperate
Regular
Answering Machine
Regular Customer
Representative
Premier Customer
Representative
Cooperate Customer
Representative
REST
Answering Machine
Answering Machine
• Example - Customer care system of a bank - The bank provides a multiple(unique) telephone numbers
REST
Resources Every disAnguishable enAty is a resource. A resource may be a Web site, an HTML page, an XML document, a Web service, a physical device, etc. URLs Iden4fy Resources Every resource is uniquely idenAfied by a URL.
9
REST Fundamentals
12
HTTP Method CRUD Desc.
POST CREATE Create -
GET RETRIEVE Retrieve Safe,Idempotent,Cacheable
PUT UPDATE Update Idempotent
DELETE DELETE Delete Idempotent
REST HTTP Methods
13
REST Fundamentals • Client-Server
• Separation principle • Components Independent
• Stateless • Session state on the client • Visibility, reliability and scalability • Trade off (network performance, etc.)
• Cacheable • A response can be cacheable • Efficiency but reduce reliability
• Layered system • System scalability
•
14
JAX-‐RS – Java API for RESTfull WebServices
• Annotation driven API
• Expose annotated Java bean as HTTP based services • Implementation may support WADL
• Define JAVA ó HTTP mapping • Number of implementations
• Apache CXF• Apache Wink• Jersey
JAX-‐RS
JAX-RS Annotated Service
@Path("/hello”)public class HelloWorldService {
@GET @Path("/{user}") public String hello(@PathParam("user") String user) {
} }
16
JAX-‐WS Annota4ons • @Path
• @GET
• @PUT
• @POST
• @DELETE
• @Produces
• @Consumes
• @PathParam
• @QueryParam
• @MatrixParam
• @HeaderParam
• @CookieParam
• @FormParam
• @DefaultValue
• @Context
JAX-‐RS
JAX-RS Annotated Service
@Path("/hello”)public class HelloWorldService {
@GET @Consumes("text/plain") @Produces("text/xml") @Path("/{user}") public String hello(@PathParam("user") String user) {
} }
21
JAX-RS service to accept HTTP calls
JavaEE – Example
@Path("/student”) public class HelloWorldService { @GET @Path(”getall") @Produces({"application/json”}) public String getAll() {
} }
22
JavaEE – Example
@Stateful public class StudentManagerImpl implements StudentManager { @PersistenceContext(unitName = "rest-jpa”) private EntityManager entityManager; public Student getStudent(int index) { Student student = entityManager.find(Student.class, index); return student; } }
JPA based data access service
23
JavaEE – Example How to integrate JAX-RS service with JPA DAO
service ?JAX-RS service
JPA service
24
JavaEE – Example integra4on
@Path("/student”) public class HelloWorldService { StudentManager sm; public HelloWorldService() { sm = new StudentManagerImpl(); } @GET @Path(”getall") @Produces({"application/json”}) public String getAll() { } }
How to integrate JAX-RS service with JPA DAO
service ?
Pure JAVA
25
JavaEE – Example integra4on
@Path("/student”) public class HelloWorldService { StudentManager sm; public void setStudentManager (StudentManager sm) { } public StudentManager setStudentManager () { } @GET @Path(”getall") @Produces({"application/json”}) public String getAll() { } }
How to integrate JAX-RS service with JPA DAO
service ?
Spring Framework
26
JavaEE – Example integra4on
@Path("/student”) public class HelloWorldService { @Inject StudentManager sm; @GET @Path(”getall") @Produces({"application/json”}) public String getAll() { } }
How to integrate JAX-RS service with JPA DAO
service ?
JavaEECDI
27
JavaEE – Example integra4on How to integrate JAX-RS service with JPA DAO
service ?JAX-RS service
JPA service
CDI
28
JavaEE – Context and Dependency InjecAon
@Decorator@Delegate@ApplicationScoped@ConversationScoped@Dependent@NormalScope@RequestScoped@SessionScoped@Observes@Alternative@Any@Default
@Disposes@Model@New@Produces@Specializes@Stereotype@Typed@inject@Named@Qualifier@Scope@Singleton
• Tenant-aware data sources • Tenant-aware JNDI service• Tenant-aware session persistence• Tenant-aware user store • Tenant-aware authentication and
authorization
• Tenant-aware Caching • Tenant-aware configuration
registry• Tenant-aware dashboards • Tenant-aware management
console
JavaEE – MulA-‐tenancy support
MulAple classloading runAmes
There are four in-built environments • Tomcat – Only Tomcat libs are visible (Minimal runtime) • Carbon – Tomcat + Carbon libs are visible • CXF - Tomcat + CXF + Spring are visible • Javaee – JavaEE libs are visible
MulAple classloading runAmes
Ability to create your own custom ‘Classloader Runtime environments’ • Create directory to place your Jar dependencies • Add an entry to webappclassloading-
environments.xml file e.g. –You can have number of incompatible versions of Spring frameworks in server level
Server-‐side JavaScript
• Develop complete server side application using Jaggery.js and deploy on Application Server
• Web Applications
• RESTfull services
• WebSocket services
Web Service development
• Supported web service development models
• JAX –WS ( based on Apache CXF) • Apache Axis2
• Bring your own WS framework • Metro, Spring WS
• Rich dashboard and development tools• Try-It tool support • Download WS client as maven project• Basic statistics
• Service publish and discovery support through WSO2 Governance Registry
• WS – Discovery supported
RESTfull service development • Supported RESTfull service
development models.• JAX –RS ( based on Apache CXF) • Apache Axis2 REST services• Jaggery REST services ( Java
Script )•
• Bring your own WS framework • Jersey, Rest Easy, Restlet, Spring
• Rich dashboard and development tools• Swagger support. • Download REST client as maven
project • Basic statistics
Web Socket development
• Supported WebSocket development models
• Java API for WebSocket
• Jaggery Web Sockets ( Java Script )
AcAvity and server monitoring
• Embedded Monitoring dashboard
• Request, response, errors, sessions over time
• Geo info , language, referrals
• Alerts on thresholds
• Server and infrastructure health monitoring.
41
References
• https://github.com/wso2as-developer/javaee-samples
• https://docs.wso2.com/display/AS521/WSO2+Application+Server+Documentation
• http://wso2.com/whitepapers/evaluating-java-ee-application-migration-and-java-ee-service-migration-to-wso2-application-server/