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:

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-webmvc:4.0.6.RELEASE") compile("com.fasterxml.jackson.core:jackson-databind:2.2.2") compile("org.springframework:spring-jdbc:4.0.6.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") 
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.

No comments:

Post a Comment