Tuesday, January 28, 2014

Spring Course: Spring In Action Part 3 Integration

Chapter 15 Working with remote services
when we talk about RPC (Remote Procedure Call) we are talking about SYNCHRONUS call, which means blocking execution in the calling code until the called procedure is complete.

sure, RPC is a model, and we have different way to implement it:


Regardless of which remoting model you choose, you’ll find that a common theme runs through Spring’s support for each model


so spring will create a proxy for each service and the client communicate with the Proxy

Remote Method Invocation
1- it came as alternative to CORBA
2- RMI uses binary messages to do the communication
2- in general, building a RMI should go throw the following steps



however with spring the process is easier, you basically define a method with @Bean annotation and write this


this will do all the mentioned things before, (SpitterService is the service that we want to expose).

on the client side you write


we will not go into more details here.

Exposing remote services with Hessian and Burlap
1- another 2 protocols to handle the RPC
2- Hessian uses binary messages like RMI, however, the binary message is portable to languages other than Java, including PHP, Python, C++, and C#
3-  Burlap is an XML-based remoting technology, which automatically makes it portable to any language that can parse XML
4- exporting and using Hessian and Burlap services are similar to RMI, here we use HessianServiceExporter rather than RmiServiceExporter
5- the difference between RMI and Hessian&Burlap is that Hessian&Burlap are HTTP-based, that is why you should define a dispatcher servlet for all .service urls


Using Spring’s HttpInvoker
now we have RMI, it doesn't use HTTP, however it users java serialization, and we have Hessian&Burlap, which uses http, however it has its own serialization technique

that is why we have HttpInvoker, it is http based and uses java serialization

also exporting and implementing HttpInvoker services are similar to other services before

HttpInvoker a remoting solution offered by the Spring Framework only. This means both the client and the service must be Spring-enabled applications

Publishing and consuming web services
in web services word, we talk about loosely coupled stuff, I don't have to know the programming language, I don't have to know anything about the service implementation.

spring uses JAX-WS for building web services, you will use annotations like @Webservice and @Webmethods.

we will not talk about this here, we will have another complete tutorial about webservice

Chapter 16 REST
another model which focuses on accessing resources, we know the stuff like @RequestMapping @ResponseBody ...

Chapter 17 Messaging in Spring
he talked about messaging in general, how it is asynchronus communication, and the types of messaging, point to point or publish subscribe.

in order to use messaging in spring

this is a connection to ActiveMQ

then you define your Quere or Topic bean
then you define a jmsTemplate


now you send a message like this

as you can see JmsOperations is the interface that JmsTemplate implements, and we use the send() method to send a message


in order to receive message from the bean

AMQP Advanced Messaging Queuing Protocol
this is another way of asynchronous messaging. The difference between AMQP and JMS:
1- JMS is an API, AMQP is a protocol
2- JMS is something related to Java, AMQP is platform and language independent
3- in JMS we have point-to-point and publish subscribe, in AMQP we have different messaging model
4- an example of a messaging system that implements AMQP is RabbitMQ

In AMQP the producer doesn't add the message to queue, actually the producer doesn't know if there is a queue or not, the producer communicate with a new layer called exchange:


exchange has 4 types of communication with a queue:
1- fanout: when the exchange receive a message it puts the message in all queue it has access to

2- direct: the exchange will check the message routing_key and put it in the queue that has the same key

3- Topic: here we use wildcard
4- Header: here the exchange matches based on header



Spring can handle AMQP as well
we will not go into the code


Chapter 18 Messaging with Websocket and STOMP
Websocket, you know if you want to get information from the server you usually send an ajax request and pool the latest update.
Websocket is a way to open a socket between the browser and the server (actually between 2 applications) which allow them to communicate all the time, so if the server has something to tell the browser it can tell him directly, the browser doesnt have to keep asking
Websocket is light weight, fast, bi directional, it is part of HTML5, not all browsers support it.

Spring has full support of implementing websocket.
you have SOCKJS, for javascript socket development

we will not go into the code here,

STOMP:  Websocket is too low level it is basically a TCP thing, SOMP is text-based protocol similar to HTTP, however STOMP is actually used when websocket is wanted.
So stomp is used when you want to use websocket and you dont want to write low level code.

STOMP is also used to connect to a message queue like activemq. ActriveMQ support websocket over STOMP.

Chapter 19: Sending Emails
 to send emails you should use JavaMailSenderImpl

then you can write:



Chapter 20: Managing beans with JMX
JMX stands for Java Management Extensions, and is a facility to allow for remote clients to connect to a JVM, and manage/monitor running applications in that JVM.

in order to be able to manage your beans you should define something called MBEAN

1- define an interface with MBean at the end of its namge

now you implement this interface


now you should write this in your main


now you can use JConsole to monitor the bean,
you should add this to the application JVM argument:
-Dcom.sun.management.jmxremote

Chapter 21: Spring Deployment with Spring Boot
Spring Boot Starter: you can aggregate dependencies together into a single dependency, for example sometimes you need these dependenceis in your project 
compile("org.springframework:spring-web:4.0.6.RELEASE") 
compile("org.springframework:spring-webmvc:4.0.6.RELEASE") compile("com.fasterxml.jackson.core:jackson-databind:2.2.2") compile("org.springframework:spring-jdbc:4.0.6.RELEASE")
compile("org.springframework:spring-tx:4.0.6.RELEASE") 
compile("com.h2database:h2:1.3.174")
compile("org.thymeleaf:thymeleaf-spring4:2.1.2.RELEASE") 
with spring boot starter some of these dependencies are compiled together so you need just
compile("org.springframework.boot:spring-boot-starter-web: 1.1.4.RELEASE") compile("org.springframework.boot:spring-boot-starter-jdbc: 1.1.4.RELEASE") compile("com.h2database:h2:1.3.174") 
compile("org.thymeleaf:thymeleaf-spring4:2.1.2.RELEASE") 
Spring Boot autoconfiguration: it cuts down on the amount of Spring configuration, for example you need these beans to configure Thymeleaf ( a template engine similar to velocity )  a ThymeleafViewResolver, a SpringTemplateEngine, and a TemplateResolver.
wit spring boot autoconfiguration, you just add Thymeleaf in the class path and spring will know automatically that it should add these beans.
The Spring Boot CLI: here you can use groovey in configuration.

Monday, January 27, 2014

Spring Course: V160 - V169 JSON and AJAX

Video 160: Creating a JSON Server
in this tutorial we talk about JACKSON, jackson is a library that converts java object to JSON and vice versa.

firstly we should add Jackson mapper dependencies which are jackson-mapper-asl and jackson-core-asl

then what you can do is this

the @ResponseBody will know that we are generating appplication/json and will convert the object to json

Sure JACKSON can be used in code, you can check this tutorial http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

Video 161: Updating Dynamically with JQuery
we talked about $.getJSON function, nothing related to SPRING

Video 162 Generating Pages with Javascript
nothing related to spring

Video 163 Adding Reply Boxes
also javascript stuff

Video 164 Showing and Hiding the Reply Forms
also javascript

Video 165 Stopping and Starting the Timer
also javascript

video 166 Getting the text from the right textarea
javascript stuff

Video 167 Posting Back JSON Data
Nothing new

Video 168 Giving the User Feedback
javascript stuff

Video 169: Sending Email With Springmail
1- we should add spring-context-support dependency and javax.mail
2- then we add this bean
3- then you wire the bean and use it


to use it



Sunday, January 26, 2014

Spring Course: V148 - V159 Spring Webflow

Video 148: Introducing Webflow
you use it to define a sequence of steps.


Video 149: Creating a Flow Registery

1- add spring-webflow dependency
2- add webflow-config namespace in your beans.xml

3- add webflow-config:flow-registry


here you set the base-path, which is the folder that will have the workflow definintion which is an xml file.
flow-location is the workflow file name,

4- add contact-flow.xml
when we define a flow, we basically define states in view-state, we will do that in the next tutorial

Video 150: Hooking Up URLs to Webflows
Here we will add all the required beans 


These are things that you should remember, 
1- FlowRegistry
2- FlowExecutor 
3-FlowHandlerAdapter
4-FlowHandlerMapping

as you can see the id "message" will be used in the url, so to start the workflow you go to ..../message

Video 151: Connecting Webflow and Apache Tiles
as you remember the contact-flow.xml looks like this


the view-state means jsp pages, so showContactForm should be a jsp page.

so, from the previous tutorial if you go to the url ..../message, it will go to the first page showContactForm.jsp

we should create this page, however to be able to use Apache Tiles with these pages you should add couple of extra beans


tilesViewResolver has been defined in previous videos.

now you can use Apache Tiles in your webflow pages.

In apache tiles definition we should add
as you can see showContactForm will redirect me to contact.jsp

Video 152: Creating a "Messages" Table
Nothing here, we added a Message table in the database, we will use it later

Video 153: Creating a Message Class
Here we added a Message Entity Class, and MessagesDao for database operations, we will use these classes later




Video 154: Adding a Message Form
as you remember before, we said that the workflow view state showContactForm will redirect us to contact.jsp

here we will define the contact.jsp page, which is going to contain a form


as you remember, we start the form by going to url ..../message, so we want the form to submit back to the same page, that is why we have commandName = message

you need also to add the hidden field _flowExecuationKey

Video 155: Transition
so now lets say we want to move from one page to another 
1- you add this in your workflow 
as you can see we define a transition on event send to go to the next state,

now we should add this

we added a hidden field to set the _eventId to send
now when we press the button we move to another state (another jsp page).

Video 156: Action States 
in the previous tutorials we were defining view states in our workflow, view state will move us from one page to another, 
here we will add Action State, in action state you can run code before moving to another page


as you can see, in the expression you write the name of the function that you want to execute, in this case it is the sendMessage() in the userService class.

Video 157: Linking to Webflows
Nothing much here, we did one thing.



as you can see we have a message object, and we set the value of username for this object

Video 158: Validating Webflow Forms

in order to validate the form in webflow we should add this


Video 159: Accessing User Details in Webflow
lets say you want to access user information, you can use the currentUser variable which comes with webflow


the usersService.getUser() is a function we defined, and we pass currentUser?.name, the question mark is used to check if currentUser is null or not (we talked about this before).


Saturday, January 25, 2014

Spring Course: Video 136 - V147 Hibernate

Video 136: Introduction Hibernate
nothing here

Video 137: A simple Hibernate Query
1- add the dependencies to pom.xml, we add spring-orm and hibernate-core

2- in datasource.xml add a bean of AnnotationSessionFactoryBean

as you can see you set the datasource, the hibernateProperties you add hibernate.dialect which is the database that you are using.
PackagesToScan, here we set the packages that have the Entities

3- now in UsersDao we were using select statements, now we will use Hibernate,


as you can see, we added SessionFactory, we defined a session(), and we use createQuery() method.

4- now create your entities:

as you see, we use @Entity, @Table and so on...

Video 138: Saving Objects
to save an object to database

Video 139 Validation Groups and Password Encryption
currently, we have the following:


as you can see the password check the size to be less than 15,

however, as we want to encrypt the password before saving we will write this

Hibernate will throw an exception here, because when we encrypt the password the size will be more than 15,

In order to fix this issue, we should tell spring to use the validation only when we submit the form not when we save to database

In order to do that we should use something called Validation Groups,

1- define 2 interfaces PersistanceValidationGroup and FormValidationGroup, sure the name is not important.

2- no simply you do this

as you can see, for each validation you assign the group that you want,
in case of passoword size we assigned it to the FormValidationGroup.

3- now we should tell the form to use the FormValidationGroup:

as you can see we use @Validated(FormValidationGroup.class).

4- we should tell hibernate to use PersistanceValidationGroup

as you can see, we add this to the bean definition as property.

Video 140: Translating Hibernate Exceptions to Spring Exceptions
if you remember we defined this ErrorHandler before


as you can see the handler catch all DataAccessException.

However, Hibernate doesnt throw this exception, so inorder to catch Hibernate database exceptions we should do exception translation:

1- add this bean PersistenceExceptionTranslationPostProcessor

2- add @Repository to classes that do hibernate operations:

Video 141: Queries With Criteria
in order to run queires with Criteria in hibernate

Video 142: Mapping Many-to-one Relationships
in order to define Many-to-one relationship, for example offers and users relationship


Join column is the freign key

Video 143: Restriction on Joind Tables
for example, if you want to get offers related to a specific user

Video 144: Multiple Criteria
in order to make a query with multiple criteria


Video 145: Updating Objects



as you can see we are using session().update or session().save.

also there is another method which is session().saveOrUpdate().

Video 146: Deleting Object



as you can see we createQuery() in order to delete

Video 147: Completing the Offers DAO
Nothing important here