Saturday, August 12, 2017

How to use RabbitMQ


What is RabbitMQ?

RabbitMQ is an Erlang-based message broker that natively speaks AMQP 0.9.1. It implements all the AMQP 0.9.1 concepts of messages, queues, exchanges, bindings, virtual hosts, producers and consumers within the broker application.

In the diagram below, "P" is our producer and "C" is our consumer. The box in the middle is a queue - a message buffer that RabbitMQ keeps on behalf of the consumer.

In short, a broker can be divided up into virtual hosts (mainly for administrative simplicity). Each one of these virtual hosts can have some queues and exchanges associated with it and bindings can be used to attach a transfer to a queue or queues. Those transactions can be direct exchanges (passing a message on to a bound queue determined by a routing key), fanout exchanges (sending all messages onto all bound queues) or header exchanges (sending messages onwards based on matching fields in the header).

The developers have added their own set of extensions to the RabbitMQ mix though. Exchanges can be bound to other trades to create richer routing, improved mechanisms for tracking message delivery, time-to-live settings for queues and messages and more, with a focus on being an efficient, balanced messaging platform. Add to that the RabbitMQ implementation which gives you control of the performance/reliability ratio, clustering with high availability queues, a web-based Admin UI and a plugin extension system for protocols and other functionality.

What is Compose RabbitMQ?

We've taken RabbitMQ and created a Compose deployment of it which delivers the best of RabbitMQ for most users. Using three nodes, clustered together and operating in a high availability mode as a single logical broker; each node mirrors the cluster's configuration. Access to the cluster is, as with all Compose offerings, managed through a pair of haproxy TCP portals which are whitelisting and SSL supported. With Compose RabbitMQ you can move your interapplication connection infrastructure to the cloud where your applications live without having to add it to the services you directly manage and maintain.


If you want to check it out now and don't have a Compose account just sign up for a Compose account and select RabbitMQ as your first service. If you already have a Compose account, go to the Compose Console and choose "Create Deployment" and RabbitMQ to get your RabbitMQ on Compose.

Connecting to Compose for RabbitMQ

You can connect to your service using the credentials that are created along with the service, or with the connection strings and command line that are provided in the Overview tab of your service dashboard.

Connecting a Docker application to Compose for RabbitMQ

To connect a Docker container application to your service, use the credentials that are created along with the service. You can find information on how to connect a Docker container application to a Compose for RabbitMQ service in Connecting to Docker Application.

Connecting to Compose for RabbitMQ from outside the container

If you want to connect to Compose for RabbitMQ from outside of the container, you can use the provided connection strings or command line. You can find information on how to connect in Connecting an external application.

Example of Docker compose for RabbitMQ:
frontend:
  build: ./example-docker-services-frontend
  ports:
    - "4321:4321"
  links:
    - rabbitmq
    - postgres
  environment:
    - AMPQ_ADDRESS=amqp://rabbitmq:4561
    - DATABASE_URL=postgres://postgres:mysecretpassword@postgres:4321
  volumes:
    - ./example-docker-services-frontend/:/app
grader:
  build: ./example-docker-services-grader
  links:
    - rabbitmq
  environment:
    - AMPQ_ADDRESS=amqp://rabbitmq:4561
  volumes:
    - ./example-docker-services-grader/:/app

postgres:
image: postgres:latest
ports:
- 4321

rabbitmq:
  image: rabbitmq
  ports:
    - 4561

  environment:
    - POSTGRES_PASSWORD=mysecretpassword
    - POSTGRES_USER=postgres




I hope this helps and Happy coding,

Thiago Leoncio.

Saturday, July 1, 2017

Consuming JSON using RESTful WebService on Spring Framework

hello everyone,

Today I am going to show you how to consume JSON using RESTFUL WebService on Spring.


  1. Looking from Spring REST client , the RestTemplate is the core class for client-side access to Spring RESTful web services. It goes over HTTP and it basically communicates to HTTP server using RESTful constraints. It is very similar to other template classes in the Spring like HibernateTemplate and JdbcTemplate etc. 
  2. In Spring, RestTemplate provides higher level implementation of corresponding HTTP methods such as GET, POST, PUT, DELETE, HEAD etc. It provides the methods to communicate by using these HTTP methods with URI template, URI param, request object and response type as arguments.

In this example, we will see how to consume JSON response. We have seen Spring RESTful web services crud example. Earlier I have used postman REST UI based client to demonstrate all HTTP methods such as get, post, delete and put. But here we are going to consume Restful web services via RestTemplate of Spring REST


RestTemplateMethods


RestTemplate provides higher level methods that correspond to each of the six main HTTP methods that make invoking many RESTful services. In the following list has methods provided by Spring RestTemplate for each http methods.



If you distinguish per name of these methods in RestTemplate class, it shows which HTTP method they implement internally and second part of the method name indicates what is return by these methods. For example, getForObject() will perform a HTTP GET action on the server and later convert the HTTP response into an object of given type and  then return it to the CLIENT. The other method postForLocation() will do a HTTP POST action on the server - converting the given object into a HTTP request, and returns the response HTTP Location header where the newly created object can be found. 

Let's see these RestTemplate's methods with examples.


 Here I am using Spring Restful web services CRUD JSON Example: 

In this example, we are using @GetMapping, @PostMapping, @PutMapping and @DeleteMapping annotations, these annotations are introduced as of Spring 4.3 version in parallel of @RequestMapping annotation with Http Methods as below.



HTTP GET Method Example
For API GET method call we can use either getForObject() or getForEntity() method of RestTemplate.

Spring REST API GET CODE:




We can define same above API call using this way below:




This call binds to convert on JSON media type only with the @RequestMapping annotation.

NOTE: If you want flexible return media type, let's say JSON or XML . These will be based on the message converter available on the classpath(application). Then it's recommended to not define producers property request mapping annotation. 

Spring RestTemplate get Method :





Output on console:
After running above code, you will get below output:
Account Detail for given AccountId : Account [accountId=1, name=Thiago Leoncio, city=BeloHorizonte, balance=212.45]

HTTP POST Method Example

For Http POST method calling we can use either postForObject() or postForLocation() method of RestTemplate.

Spring Rest API Code post method:



Spring RestTemplate post Method call:




Output on console:
After running above code, you will get below output:
Added account: Account [accountId=3, name=Arthur Jordan, city=BeloHorizonte, balance=312.45]

HTTP PUT Method Example

For Http PUT method calling we can use put() method of RestTemplate.

Spring Rest API Code put method:





Spring RestTemplate put Method call:





Output on console:
After running above code, you will get below output:

Added account: Account [accountId=1, name=Arthur Jordan, city=BeloHorizonte, balance=312.45]
Updated Account: Account [accountId=1, name=Love Rocha, city=BeloHorizonte, balance=123.45]

HTTP DELETE Method Example

For Http DELETE method calling we can use delete() method of RestTemplate.

Spring Rest API Code delete method:






Spring RestTemplate delete Method call:






Output on console:
After running above code, you will get below output:
Total Accounts before DELETE call: 
{accountId=1, name=Love Rocha, city=BeloHorizonte, balance=123.45}
{accountId=2, name=Love Rocha, city=BeloHorizonte, balance=222.45}
{accountId=3, name=Arthur Jordan, city=BeloHorizonte, balance=522.45}
Total Accounts after DELETE call: 
{accountId=1, name=Love Rocha, city=BeloHorizonte, balance=123.45}
{accountId=3, name=Arthur Jordan, city=BeloHorizonte, balance=522.45}



Reference: Spring documentation - Consuming REST  https://spring.io/guides/gs/consuming-rest/


I hope it helps you.

Happy coding,
Thiago Leoncio.