RabbitMQ for Spring Boot with Jackson

Issue:

When you use rabbitMQ to send the java object you might get this exception. The error you will meet would like this :

Caused by: org.springframework.messaging.converter.MessageConversionException: Cannot convert from [[B]

 

Solution:

This is caused by the MQ converter not using Jackson yet. So just add following piece to solve this issue:

@SpringBootApplication
@EnableRabbit

public class MYCloudApplication {

@Bean
 public MessageConverter jsonMessageConverter() {
 return new Jackson2JsonMessageConverter();
 }
Advertisements

RabbitMQ quick start for Spring Boot

  1. Install the rabbitmq
sudo apt-get update
sudo apt-get install rabbitmq-server
sudo service rabbitmq-server stop
sudo service rabbitmq-server start
sudo service rabbitmq-server status
sudo rabbitmqctl status

cat /proc/$RABBITMQ_BEAM_PROCESS_PID/limits

The broker always appends to the log files, so a complete log history is retained.

/var/log/rabbitmq directory.
See /etc/logrotate.d/rabbitmq-server to configure logrotate.

The rabbbitMQ will run at port localhost:5672

 

2. Let us build a Spring Boot project to make MQ work.

2.1  Download the spring boot sample project from https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-amqp.

remember to change pom.xml parent as this:

 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.5.10.RELEASE</version>
 <relativePath /> <!-- lookup parent from repository -->
 </parent>

 

2.2 Use eclipse to import this maven project. and use “java -jar” run it, you will see it continuously say hello.

2.3 The code use following tricks:

a. @EnableScheduling and @Scheduled(fixedDelay = 1000L)  will make the app auto start a Schedule task every 1 second and call the send() method to send a msg “hello” to the MQ queue “foo”:

@RabbitListener(queues = "foo")

defines a mq listener to listen to the queue of “foo”.

b. When a msg is in the queue, this method

@RabbitHandler
 public void process(@Payload String foo) { ...}

will be triggered to process the msg got in this queue, code just prints it our in stdout.

c. the method fooQueue must names as the fooQueue as the queue name in this example is “foo”.

public Queue fooQueue() {...}

d. This code:

 @Autowired
 private RabbitTemplate rabbitTemplate;

Will auto get a connection from the RabbitMQ so it can do the send later for you.

 

3. Access RabbitMQ GUI to make simple management

Enable the rabbitmq_management module:

sudo rabbitmq-plugins enable rabbitmq_management

The Web UI is located at: http://server-name:15672/
The user “guest” is created with password “guest” by default.

 

 

 

 

 

 

 

 

 

Start from Vagrant – create your own VM Box

Here I will create a ubuntu 16 64bit server image. for which altas link in fact have issue some times. So we create from ubuntu cloud image directly:

https://cloud-images.ubuntu.com/xenial/current/

After you get where is the URL for xenial-server-cloudimg-amd64-vagrant.box, then we can use this cmd to do things.

vagrant init boxname boxurl

vagrant init ubuntu/xenial64 https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-vagrant.box
less Vagrantfile
vagrant up
vagrant ssh

Then you install anything you want to customize your linux VM.
After every thing ready, just exit from your VM.  Shutdown it and pack as a new box.

vagrant halt
vagrant package --output newU64.box

So you will get a new box named as newU64.box. Now you can release this box to team and they can use init command to create a VM at their local folder.

 

To make your new box smaller, there are some command you can use. see this article for details:

https://scotch.io/tutorials/how-to-create-a-vagrant-base-box-from-an-existing-one#toc-add-the-box-into-your-vagrant-install

 

 

 

 

 

 

 

Start from Vagrant – install and up

This is a simple demo how to install the vagrant in the Ubuntu Linux

Install virtualbox

sudo apt-get install virtualbox

Install vagrant

sudo apt-get update
sudo apt-get install vagrant
vagrant version

Create a Vagrantfile from default category list

https://app.vagrantup.com/boxes/search

mkdir ~/vagrant
cd ~/vagrant
vagrant init hashicorp/precise64
less Vagrantfilevagrant up
vagrant ssh

To turn your VM on, navigate to the directory with your Vagrantfile:

vagrant up

To pause your VM, navigate to the directory with your Vagrantfile:

vagrant suspend

To turn your VM off, navigate to the directory with your Vagrantfile:

vagrant halt

To destroy your VM, navigate to the directory with your Vagrantfile:

vagrant destroy

Linux command check CPU IO and VM info

  • Check the CPU utilize rate and disk IO info
$ iostat
Linux 4.4.0-103-generic (xw-HP-Z420-Workstation) 02/02/2018 _x86_64_ (12 CPU)

avg-cpu: %user %nice %system %iowait %steal %idle
 0.17 0.00 0.03 0.01 0.00 99.78

Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 1.73 15.20 18.99 3663865 4578881
sdb 0.00 0.03 0.00 6317 56
  • Virtual memory info
$ vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r b swpd free buff cache si so bi bo in cs us sy id wa st
 0 0 0 21497140 917372 3875112 0 0 1 2 5 3 0 0 100 0 0
 0 0 0 21497336 917376 3875112 0 0 0 172 577 2320 2 0 98 0 0
 0 0 0 21497196 917376 3875112 0 0 0 0 553 2380 3 0 97 0 0
 0 0 0 21497336 917376 3875116 0 0 0 8 553 2432 2 0 97 0 0
 0 0 0 21497336 917376 3875112 0 0 0 0 490 2173 2 0 97 0 0
  • check kernel and driver’s log
dmesg

Some Linux commands about the service and files check up

Here are some commands we common used in Ubuntu linux service and files monitor and check up:

  • Check what service is running in system
service --status-all

Or a GUI tool called bum

sudo apt install bum
sudo bum

Screenshot 2018-02-02 10:44:30

  • List all network interfaces
ifconfig -a

 

  • How to check max files OS could open ?
sysctl -a | grep fs.file-max
       fs.file-max = 3266932

This number normally is around 10% of your system memory in KB number; Such as for my Linux I can get my RAM by top command:

KiB Mem : 32873324 total,

And by default the max files one process can open is 1024. just use this cmd to check:

ulimit -n

 

  • How to check one process’ real limit in the the system for the files and others?
 xw@xwi-HP-Z420-Workstation:~$ ps aux | grep mysql
mysql 1189 0.0 0.4 1246964 160748 ? Ssl Jan30 2:20 /usr/sbin/mysqld
 xw@xwi-HP-Z420-Workstation:~$ cat /proc/1189/limits
Limit Soft Limit Hard Limit Units 
Max cpu time unlimited unlimited seconds 
Max file size unlimited unlimited bytes 
Max data size unlimited unlimited bytes 
Max stack size 8388608 unlimited bytes 
Max core file size 0 unlimited bytes 
Max resident set unlimited unlimited bytes 
Max processes 127918 127918 processes 
Max open files 1024 4096 files 
Max locked memory 65536 65536 bytes 
Max address space unlimited unlimited bytes 
Max file locks unlimited unlimited locks 
Max pending signals 127918 127918 signals 
Max msgqueue size 819200 819200 bytes 
Max nice priority 0 0 
Max realtime priority 0 0 
Max realtime timeout unlimited unlimited us

 

  • How to check one file’s access time stamp info?

xw@xw-HP-Z420-Workstation:~$ stat testFile.txt
File: ‘testFile.txt’
Size: 28 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 8998339 Links: 1
Access: (0664/-rw-rw-r–) Uid: ( 1000/ xiaowei) Gid: ( 1000/ xiaowei)
Access: 2017-12-05 16:17:42.221584809 -0800
Modify: 2017-04-17 11:35:38.000000000 -0700
Change: 2017-04-17 14:40:44.639165159 -0700
Birth: –