SlideShare a Scribd company logo
1 of 46
GWT

Kunal Jaggi


Engineering Excellence Group
Entity Inheritance
Agenda
Strategies

•   Introduction to RPC
•   Demo Application
•   Persistence
•   Validation
•   Service Layer Impl with Spring 2.5
•   Unit testing- Junit 4 parameterized testing
•   Build and Deployment
•   Q&A

www.javaevangelist.com
Entity Inheritance
Scope of this talk
Strategies

• More of a hands-on tutorial, rather than a
  presentation.
• GWT backend communication option- RPC
• UI framework- GWT
  No Spring MVC




www.javaevangelist.com
Entity Inheritance
Why prefer RPC over Servlets?
Strategies

• Avoid writing any boilerplate code.
  Pass any argument
  Return any result from the server

• The client need not make synchronous HTTP
  requests




www.javaevangelist.com
Entity Inheritance
GWT RPC Mechanism
Strategies

• RPC- fetching data from the server.
• Client-side JavaScript code needs to fetch data
  from the server backend.
• Based on Java Servlets.
• Serialized objects are shared across the client
  and server over HTTP.
• GWT RPC calls are asynchronous to make UI
  more responsive.


www.javaevangelist.com
Entity Inheritance
GWT RPC Mechanism
Strategies



                   GWT UI     Servlets




• GWT RPC is a mechanism for passing Java
  objects to and from a server over standard
  HTTP
www.javaevangelist.com
GWT Plumbing Diagram

   • Source- code.google.com




www.javaevangelist.com
Entity Inheritance
Demo Application
Strategies

• We’ll springify the Stock Watcher application.
• Replace Servlet-based service with Spring
  managed service.




www.javaevangelist.com
Entity Inheritance
Application Layering
Strategies




www.javaevangelist.com
Entity Inheritance
Contract-first Development
Strategies




www.javaevangelist.com
Entity Inheritance
The Remote Interface
Strategies
• The remote service interface is a pivotal element for
  integration.
• Code Snippet
@RemoteServiceRelativePath("stocks/stockPriceServic
  e")
public interface StockPriceService extends
  RemoteService {
  //abstract methods
}

The relative path defined by the remote service interface
  is the key to identify the Spring managed bean to be
  invoked
www.javaevangelist.com
Package Structure




www.javaevangelist.com
Persistence Tier




www.javaevangelist.com
Entity Inheritance
Hitting the DB with Hibernate ORM
Strategies
• Code Snippet
@Entity
@Table(name="T_LISTED_STOCKS")
public class StockPrice implements Serializable {
  @Id
  private String symbol;
  @Transient
  private double price;
  @Transient
  private double change;
www.javaevangelist.com
Entity Inheritance
DAO Implementation
Strategies

• Code Snippet
• StockPriceDAOImpl.java
@Repository
public class StockPriceDAOImpl implements
  StockPriceDAO {
  @Autowired
  private HibernateTemplate hibernateTemplate;



www.javaevangelist.com
Entity Inheritance
Preparing the DAO Layer
Strategies




www.javaevangelist.com
Entity Inheritance
Test the DAO Implementation
Strategies

• We’ll use Spring custom runner to avoid
  extending from framework classes.
• Code Snippet
StockPriceDAOTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
  "classpath:/applicationContext-test.xml" })
public class StockPriceDAOTest {
…
www.javaevangelist.com
Domain Classes




www.javaevangelist.com
Entity Inheritance
Object Serialization
Strategies

• Methods parameters and return types should
  be serializable.
• A user defined type is serializable if it’s
  assignable to IsSerializable or Serializable.




www.javaevangelist.com
Implementing Spring Service




www.javaevangelist.com
Entity Inheritance
Implementing Business Logic with Spring
Strategies

• Code Snippet
@Service("stockPriceService")
public class StockPriceServiceImpl implements
     StockPriceService

• The business service is no longer managed by
  the GWT RemoteServiceServlet.



www.javaevangelist.com
Entity Inheritance
Demarcating Transactions
Strategies

• Spring framework delegates the responsibility
  of transaction manager to an underlying
  implementation.
• Since we’re using Hibernate, we’ll roll in a
  HibernateTransactionManager to take care of
  transaction management.




www.javaevangelist.com
Entity Inheritance
Transaction Demarcation
Strategies
• Declarative annotation-based transaction
  demarcation.
• Code Snippet
StockPriceServiceImpl.java
@Service("stockPriceService")
@Transactional(readOnly = true)
public class StockPriceServiceImpl implements
     StockPriceService{
…
}
www.javaevangelist.com
Entity Inheritance
Overriding Transaction Behavior
Strategies

• Since Write operations are not allowed in read-
  only mode, we must override the transaction
  behavior for the saveupdate and delete
  operations.
• Code Snippet
@Transactional(readOnly = false)
public boolean saveOrUpdate(StockPrice
  stock)throws StockValidationExcepion {


www.javaevangelist.com
Validations and Exception Handling




www.javaevangelist.com
Entity Inheritance
Implementing Validations
Strategies

• Client-side validation: in the addStock()
  method we alert the user if she tries to add a
  stock symbol which is not a number, letter or
  dots; for example, a special character.

• Server-side validation: we propagate a
  DelistedException when the user tries to add a
  delisted stock symbol. For the sake of
  simplicity, a checked DelistedException is
  thrown when the stock symbol “ERR” is added.
www.javaevangelist.com
Entity Inheritance
Introducing Hibernate Validator
Strategies

• Reference Implementation (RI) of JSR 303-
  bean validation, simplifies domain validation by
  providing a cross tier validation support.
• It’s not tied to any particular tier, you can
  validate your domain classes just from
  anywhere- web, business services or even
  before domain objects are inserted into a
  relational table from the data access layer.


www.javaevangelist.com
Entity Inheritance
Annotating POJOs for Validations
Strategies

• Code Snippet
StockPrice.java
public class StockPrice implements Serializable {
@Pattern(regexp="[0-9a-zA-Z.]{1,10}$",
  message="Stock code must be between 1 and
  10 chars that are numbers, letters, or dots.")
@CheckDelistedStock
private String symbol;

www.javaevangelist.com
Entity Inheritance
Defining Custom Validation Annotations
Strategies

• Code Snippet
@Target( { METHOD, FIELD,
  ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy =
  CheckDelistedStockValidator.class)
@Documented
public @interface CheckDelistedStock {

www.javaevangelist.com
Entity Inheritance
Unit Testing the Validation Implementation
Strategies

• We’ll leverage from JUnit4 parameterized test
  cases.




www.javaevangelist.com
Entity Inheritance
Handling Exceptions
Strategies




www.javaevangelist.com
Presentation Tier




www.javaevangelist.com
Entity Inheritance
Plumbing Spring Services into GWT
Strategies

• We’ll use Spring4GWT, an open source library
  for integrating GWT modules with Spring
  managed back-end.




www.javaevangelist.com
Entity Inheritance
GWT RPC with Spring
Strategies

• Step 1 – Define synchronous interface which
  extends from RemoteService interface.

• Step 2 – Define Spring-based service
  implementation class which implements the
  synchronous interface (created in step 1).

• Step 3 – Configure web.xml for Spring4GWT


www.javaevangelist.com
Entity Inheritance
GWT RPC with Spring (contd.)
Strategies

• Step 4 – Define asynchronous interface. Follow
  the naming pattern- <sync_interface>Async.

• Step 5 – Create service proxy at the client-side

• Step 6 – Define client-side callbacks to handle
  success/failures.



www.javaevangelist.com
Entity Inheritance
Step 1- Hooking GWT Modules with Spring
Strategies
• The remote service interface is a pivotal element for
  integration.
• Code Snippet
@RemoteServiceRelativePath("stocks/stockPriceServic
  e")
public interface StockPriceService extends
  RemoteService {
  //abstract methods
}

The relative path defined by the remote service interface
  is the key to identify the Spring managed bean to be
  invoked
www.javaevangelist.com
Entity Inheritance
Step 2 - Spring Service Implementation
Strategies

• The Service Implementation class is a pure
  Spring bean without any dependency on the
  GWT RPC RemoteServiceServlet.
• Code Snippet
StockPriceServiceImpl.java
@Service("stockPriceService")
@Transactional(readOnly = true)
public class StockPriceServiceImpl implements
     StockPriceService {
//dependencies
www.javaevangelist.com
Entity Inheritance
Step 3 - Configure the DD- web.xml
Strategies
• Code Snippet
<servlet>
  <servlet-name>springGwtRemoteServiceServlet</servlet-
  name>
  <servlet-class>
org.spring4gwt.server.SpringGwtRemoteServiceServlet
</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>springGwtRemoteServiceServlet</servlet-
  name>
  <url-pattern>/stockwatcher/stocks/*</url-pattern>
</servlet-mapping>
www.javaevangelist.com
Entity Inheritance
Step 4 - Write an asynchronous interface
Strategies

• Guidelines
  Same name as the service interface, appended
  with Async
  Each method must have the same name and
  signature as in the service interface with an
  important difference: the method has no return
  type and the last parameter is an
  AsyncCallback object.


www.javaevangelist.com
Entity Inheritance
Step 5 - Instantiate the Service Proxy
Strategies

• Create an instance of the service proxy class by
  calling GWT.create(Class)
• There’s noting new at the front-end. The remote
  service is called in the usual way using the
  GWT.create() method.
• Code Snippet
private StockPriceServiceAsync stockPriceSvc =
  GWT.create(StockPriceService.class);


www.javaevangelist.com
Entity Inheritance
Step 5 - Write callback methods
Strategies

• Specify a callback method which executes
  when the call completes.
• The AsyncCallback object contains two
  methods, one of which is called depending on
  whether the call failed or succeeded:
  onFailure(Throwable) and onSuccess(T).




www.javaevangelist.com
Entity Inheritance
Defining Asynchronous Interface
Strategies
interface MyServiceAsync {
  public void myMethod(String s, AsyncCallback<String>
   callback);
}



                           pass in a callback object
         No return type    that can be notified when
                             an asynchronous call
                                   completes

www.javaevangelist.com
Entity Inheritance
Why asynchronous calls?
Strategies

• All GWT-based server calls execute
  immediately.
• GWT RPC calls are non-blocking.
• Improves user experience
  UI remains responsive
  A client can do multitask
  Can call several server methods in parallel



www.javaevangelist.com
Compile and Deploy




www.javaevangelist.com
Entity Inheritance
Ant Targets
Strategies

• Compile and build- build
• Execute test suites- testReports
• Deploy on Tomcat- war




www.javaevangelist.com
Q&A




www.javaevangelist.com

More Related Content

What's hot

What's hot (20)

Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
React JS
React JSReact JS
React JS
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
JPA Best Practices
JPA Best PracticesJPA Best Practices
JPA Best Practices
 
ASP.NET WEB API Training
ASP.NET WEB API TrainingASP.NET WEB API Training
ASP.NET WEB API Training
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Java spring ppt
Java spring pptJava spring ppt
Java spring ppt
 
Automated Test Framework with Cucumber
Automated Test Framework with CucumberAutomated Test Framework with Cucumber
Automated Test Framework with Cucumber
 
Automation testing & Unit testing
Automation testing & Unit testingAutomation testing & Unit testing
Automation testing & Unit testing
 
ModulFlutter-1.pptx
ModulFlutter-1.pptxModulFlutter-1.pptx
ModulFlutter-1.pptx
 
Angular Unit Testing
Angular Unit TestingAngular Unit Testing
Angular Unit Testing
 
Jenkins tutorial for beginners
Jenkins tutorial for beginnersJenkins tutorial for beginners
Jenkins tutorial for beginners
 
Flutter
FlutterFlutter
Flutter
 
Getting started with karate dsl
Getting started with karate dslGetting started with karate dsl
Getting started with karate dsl
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
React hooks
React hooksReact hooks
React hooks
 
What is Agile Testing? Edureka
What is Agile Testing? EdurekaWhat is Agile Testing? Edureka
What is Agile Testing? Edureka
 
Test driven development with react
Test driven development with reactTest driven development with react
Test driven development with react
 

Similar to Integrating GWT, Spring and Hibernate ORM

Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望
javatwo2011
 
SaaS transformation with OCE - uEngineCloud
SaaS transformation with OCE - uEngineCloudSaaS transformation with OCE - uEngineCloud
SaaS transformation with OCE - uEngineCloud
uEngine Solutions
 

Similar to Integrating GWT, Spring and Hibernate ORM (20)

Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望
 
SaaS transformation with OCE - uEngineCloud
SaaS transformation with OCE - uEngineCloudSaaS transformation with OCE - uEngineCloud
SaaS transformation with OCE - uEngineCloud
 
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
 
Struts Interceptors
Struts InterceptorsStruts Interceptors
Struts Interceptors
 
JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0
 
Why jakarta ee matters (ConFoo 2021)
Why jakarta ee matters (ConFoo 2021)Why jakarta ee matters (ConFoo 2021)
Why jakarta ee matters (ConFoo 2021)
 
What is WAAT?
What is WAAT?What is WAAT?
What is WAAT?
 
The Spring Framework
The Spring FrameworkThe Spring Framework
The Spring Framework
 
The Spring Framework
The Spring FrameworkThe Spring Framework
The Spring Framework
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083
 
The JavaFX Ecosystem
The JavaFX EcosystemThe JavaFX Ecosystem
The JavaFX Ecosystem
 
Integrating Servlets and JSP (The MVC Architecture)
Integrating Servlets and JSP  (The MVC Architecture)Integrating Servlets and JSP  (The MVC Architecture)
Integrating Servlets and JSP (The MVC Architecture)
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
 
Java EE 8 Update
Java EE 8 UpdateJava EE 8 Update
Java EE 8 Update
 
Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts
 
Haj 4328-java ee 7 overview
Haj 4328-java ee 7 overviewHaj 4328-java ee 7 overview
Haj 4328-java ee 7 overview
 
Chaincode Use Cases
Chaincode Use Cases Chaincode Use Cases
Chaincode Use Cases
 
Struts2 - 101
Struts2 - 101Struts2 - 101
Struts2 - 101
 
Struts2-Spring=Hibernate
Struts2-Spring=HibernateStruts2-Spring=Hibernate
Struts2-Spring=Hibernate
 

More from Marakana Inc.

Behavior Driven Development
Behavior Driven DevelopmentBehavior Driven Development
Behavior Driven Development
Marakana Inc.
 
Why Java Needs Hierarchical Data
Why Java Needs Hierarchical DataWhy Java Needs Hierarchical Data
Why Java Needs Hierarchical Data
Marakana Inc.
 
Pictures from "Learn about RenderScript" meetup at SF Android User Group
Pictures from "Learn about RenderScript" meetup at SF Android User GroupPictures from "Learn about RenderScript" meetup at SF Android User Group
Pictures from "Learn about RenderScript" meetup at SF Android User Group
Marakana Inc.
 
2010 07-18.wa.rails tdd-6
2010 07-18.wa.rails tdd-62010 07-18.wa.rails tdd-6
2010 07-18.wa.rails tdd-6
Marakana Inc.
 
Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)
Marakana Inc.
 

More from Marakana Inc. (20)

Android Services Black Magic by Aleksandar Gargenta
Android Services Black Magic by Aleksandar GargentaAndroid Services Black Magic by Aleksandar Gargenta
Android Services Black Magic by Aleksandar Gargenta
 
JRuby at Square
JRuby at SquareJRuby at Square
JRuby at Square
 
Behavior Driven Development
Behavior Driven DevelopmentBehavior Driven Development
Behavior Driven Development
 
Martin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMartin Odersky: What's next for Scala
Martin Odersky: What's next for Scala
 
Why Java Needs Hierarchical Data
Why Java Needs Hierarchical DataWhy Java Needs Hierarchical Data
Why Java Needs Hierarchical Data
 
Deep Dive Into Android Security
Deep Dive Into Android SecurityDeep Dive Into Android Security
Deep Dive Into Android Security
 
Securing Android
Securing AndroidSecuring Android
Securing Android
 
Pictures from "Learn about RenderScript" meetup at SF Android User Group
Pictures from "Learn about RenderScript" meetup at SF Android User GroupPictures from "Learn about RenderScript" meetup at SF Android User Group
Pictures from "Learn about RenderScript" meetup at SF Android User Group
 
Android UI Tips, Tricks and Techniques
Android UI Tips, Tricks and TechniquesAndroid UI Tips, Tricks and Techniques
Android UI Tips, Tricks and Techniques
 
2010 07-18.wa.rails tdd-6
2010 07-18.wa.rails tdd-62010 07-18.wa.rails tdd-6
2010 07-18.wa.rails tdd-6
 
Efficient Rails Test-Driven Development - Week 6
Efficient Rails Test-Driven Development - Week 6Efficient Rails Test-Driven Development - Week 6
Efficient Rails Test-Driven Development - Week 6
 
Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)
 
What's this jQuery? Where it came from, and how it will drive innovation
What's this jQuery? Where it came from, and how it will drive innovationWhat's this jQuery? Where it came from, and how it will drive innovation
What's this jQuery? Where it came from, and how it will drive innovation
 
jQuery State of the Union - Yehuda Katz
jQuery State of the Union - Yehuda KatzjQuery State of the Union - Yehuda Katz
jQuery State of the Union - Yehuda Katz
 
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...
 
Efficient Rails Test Driven Development (class 4) by Wolfram Arnold
Efficient Rails Test Driven Development (class 4) by Wolfram ArnoldEfficient Rails Test Driven Development (class 4) by Wolfram Arnold
Efficient Rails Test Driven Development (class 4) by Wolfram Arnold
 
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldEfficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
 
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas Enebo
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas EneboLearn about JRuby Internals from one of the JRuby Lead Developers, Thomas Enebo
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas Enebo
 
Replacing Java Incrementally
Replacing Java IncrementallyReplacing Java Incrementally
Replacing Java Incrementally
 
Learn to Build like you Code with Apache Buildr
Learn to Build like you Code with Apache BuildrLearn to Build like you Code with Apache Buildr
Learn to Build like you Code with Apache Buildr
 

Recently uploaded

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 

Integrating GWT, Spring and Hibernate ORM

  • 2. Entity Inheritance Agenda Strategies • Introduction to RPC • Demo Application • Persistence • Validation • Service Layer Impl with Spring 2.5 • Unit testing- Junit 4 parameterized testing • Build and Deployment • Q&A www.javaevangelist.com
  • 3. Entity Inheritance Scope of this talk Strategies • More of a hands-on tutorial, rather than a presentation. • GWT backend communication option- RPC • UI framework- GWT No Spring MVC www.javaevangelist.com
  • 4. Entity Inheritance Why prefer RPC over Servlets? Strategies • Avoid writing any boilerplate code. Pass any argument Return any result from the server • The client need not make synchronous HTTP requests www.javaevangelist.com
  • 5. Entity Inheritance GWT RPC Mechanism Strategies • RPC- fetching data from the server. • Client-side JavaScript code needs to fetch data from the server backend. • Based on Java Servlets. • Serialized objects are shared across the client and server over HTTP. • GWT RPC calls are asynchronous to make UI more responsive. www.javaevangelist.com
  • 6. Entity Inheritance GWT RPC Mechanism Strategies GWT UI Servlets • GWT RPC is a mechanism for passing Java objects to and from a server over standard HTTP www.javaevangelist.com
  • 7. GWT Plumbing Diagram • Source- code.google.com www.javaevangelist.com
  • 8. Entity Inheritance Demo Application Strategies • We’ll springify the Stock Watcher application. • Replace Servlet-based service with Spring managed service. www.javaevangelist.com
  • 11. Entity Inheritance The Remote Interface Strategies • The remote service interface is a pivotal element for integration. • Code Snippet @RemoteServiceRelativePath("stocks/stockPriceServic e") public interface StockPriceService extends RemoteService { //abstract methods } The relative path defined by the remote service interface is the key to identify the Spring managed bean to be invoked www.javaevangelist.com
  • 14. Entity Inheritance Hitting the DB with Hibernate ORM Strategies • Code Snippet @Entity @Table(name="T_LISTED_STOCKS") public class StockPrice implements Serializable { @Id private String symbol; @Transient private double price; @Transient private double change; www.javaevangelist.com
  • 15. Entity Inheritance DAO Implementation Strategies • Code Snippet • StockPriceDAOImpl.java @Repository public class StockPriceDAOImpl implements StockPriceDAO { @Autowired private HibernateTemplate hibernateTemplate; www.javaevangelist.com
  • 16. Entity Inheritance Preparing the DAO Layer Strategies www.javaevangelist.com
  • 17. Entity Inheritance Test the DAO Implementation Strategies • We’ll use Spring custom runner to avoid extending from framework classes. • Code Snippet StockPriceDAOTest.java @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/applicationContext-test.xml" }) public class StockPriceDAOTest { … www.javaevangelist.com
  • 19. Entity Inheritance Object Serialization Strategies • Methods parameters and return types should be serializable. • A user defined type is serializable if it’s assignable to IsSerializable or Serializable. www.javaevangelist.com
  • 21. Entity Inheritance Implementing Business Logic with Spring Strategies • Code Snippet @Service("stockPriceService") public class StockPriceServiceImpl implements StockPriceService • The business service is no longer managed by the GWT RemoteServiceServlet. www.javaevangelist.com
  • 22. Entity Inheritance Demarcating Transactions Strategies • Spring framework delegates the responsibility of transaction manager to an underlying implementation. • Since we’re using Hibernate, we’ll roll in a HibernateTransactionManager to take care of transaction management. www.javaevangelist.com
  • 23. Entity Inheritance Transaction Demarcation Strategies • Declarative annotation-based transaction demarcation. • Code Snippet StockPriceServiceImpl.java @Service("stockPriceService") @Transactional(readOnly = true) public class StockPriceServiceImpl implements StockPriceService{ … } www.javaevangelist.com
  • 24. Entity Inheritance Overriding Transaction Behavior Strategies • Since Write operations are not allowed in read- only mode, we must override the transaction behavior for the saveupdate and delete operations. • Code Snippet @Transactional(readOnly = false) public boolean saveOrUpdate(StockPrice stock)throws StockValidationExcepion { www.javaevangelist.com
  • 25. Validations and Exception Handling www.javaevangelist.com
  • 26. Entity Inheritance Implementing Validations Strategies • Client-side validation: in the addStock() method we alert the user if she tries to add a stock symbol which is not a number, letter or dots; for example, a special character. • Server-side validation: we propagate a DelistedException when the user tries to add a delisted stock symbol. For the sake of simplicity, a checked DelistedException is thrown when the stock symbol “ERR” is added. www.javaevangelist.com
  • 27. Entity Inheritance Introducing Hibernate Validator Strategies • Reference Implementation (RI) of JSR 303- bean validation, simplifies domain validation by providing a cross tier validation support. • It’s not tied to any particular tier, you can validate your domain classes just from anywhere- web, business services or even before domain objects are inserted into a relational table from the data access layer. www.javaevangelist.com
  • 28. Entity Inheritance Annotating POJOs for Validations Strategies • Code Snippet StockPrice.java public class StockPrice implements Serializable { @Pattern(regexp="[0-9a-zA-Z.]{1,10}$", message="Stock code must be between 1 and 10 chars that are numbers, letters, or dots.") @CheckDelistedStock private String symbol; www.javaevangelist.com
  • 29. Entity Inheritance Defining Custom Validation Annotations Strategies • Code Snippet @Target( { METHOD, FIELD, ANNOTATION_TYPE }) @Retention(RUNTIME) @Constraint(validatedBy = CheckDelistedStockValidator.class) @Documented public @interface CheckDelistedStock { www.javaevangelist.com
  • 30. Entity Inheritance Unit Testing the Validation Implementation Strategies • We’ll leverage from JUnit4 parameterized test cases. www.javaevangelist.com
  • 33. Entity Inheritance Plumbing Spring Services into GWT Strategies • We’ll use Spring4GWT, an open source library for integrating GWT modules with Spring managed back-end. www.javaevangelist.com
  • 34. Entity Inheritance GWT RPC with Spring Strategies • Step 1 – Define synchronous interface which extends from RemoteService interface. • Step 2 – Define Spring-based service implementation class which implements the synchronous interface (created in step 1). • Step 3 – Configure web.xml for Spring4GWT www.javaevangelist.com
  • 35. Entity Inheritance GWT RPC with Spring (contd.) Strategies • Step 4 – Define asynchronous interface. Follow the naming pattern- <sync_interface>Async. • Step 5 – Create service proxy at the client-side • Step 6 – Define client-side callbacks to handle success/failures. www.javaevangelist.com
  • 36. Entity Inheritance Step 1- Hooking GWT Modules with Spring Strategies • The remote service interface is a pivotal element for integration. • Code Snippet @RemoteServiceRelativePath("stocks/stockPriceServic e") public interface StockPriceService extends RemoteService { //abstract methods } The relative path defined by the remote service interface is the key to identify the Spring managed bean to be invoked www.javaevangelist.com
  • 37. Entity Inheritance Step 2 - Spring Service Implementation Strategies • The Service Implementation class is a pure Spring bean without any dependency on the GWT RPC RemoteServiceServlet. • Code Snippet StockPriceServiceImpl.java @Service("stockPriceService") @Transactional(readOnly = true) public class StockPriceServiceImpl implements StockPriceService { //dependencies www.javaevangelist.com
  • 38. Entity Inheritance Step 3 - Configure the DD- web.xml Strategies • Code Snippet <servlet> <servlet-name>springGwtRemoteServiceServlet</servlet- name> <servlet-class> org.spring4gwt.server.SpringGwtRemoteServiceServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>springGwtRemoteServiceServlet</servlet- name> <url-pattern>/stockwatcher/stocks/*</url-pattern> </servlet-mapping> www.javaevangelist.com
  • 39. Entity Inheritance Step 4 - Write an asynchronous interface Strategies • Guidelines Same name as the service interface, appended with Async Each method must have the same name and signature as in the service interface with an important difference: the method has no return type and the last parameter is an AsyncCallback object. www.javaevangelist.com
  • 40. Entity Inheritance Step 5 - Instantiate the Service Proxy Strategies • Create an instance of the service proxy class by calling GWT.create(Class) • There’s noting new at the front-end. The remote service is called in the usual way using the GWT.create() method. • Code Snippet private StockPriceServiceAsync stockPriceSvc = GWT.create(StockPriceService.class); www.javaevangelist.com
  • 41. Entity Inheritance Step 5 - Write callback methods Strategies • Specify a callback method which executes when the call completes. • The AsyncCallback object contains two methods, one of which is called depending on whether the call failed or succeeded: onFailure(Throwable) and onSuccess(T). www.javaevangelist.com
  • 42. Entity Inheritance Defining Asynchronous Interface Strategies interface MyServiceAsync { public void myMethod(String s, AsyncCallback<String> callback); } pass in a callback object No return type that can be notified when an asynchronous call completes www.javaevangelist.com
  • 43. Entity Inheritance Why asynchronous calls? Strategies • All GWT-based server calls execute immediately. • GWT RPC calls are non-blocking. • Improves user experience UI remains responsive A client can do multitask Can call several server methods in parallel www.javaevangelist.com
  • 45. Entity Inheritance Ant Targets Strategies • Compile and build- build • Execute test suites- testReports • Deploy on Tomcat- war www.javaevangelist.com