Now it's time to build hello world for RabbitMQ.
First, we will create the queue.
Open your web browser, go to RabbitMQ management console, which is localhost port 15672, and
login using guest / guest.
We will create a queue.
Remember, queue is a storage for message.
Lets click on menu queues, and add a new queue
with name "course.hello".
Just fill in the name, left the other fields to default, and click "Add queue"
OK, now we have empty queue named "course.hello",
let's create the java program
Now the java part.
First, we will create the producer, and then the consumer.
On Eclipse, right click on rabbitmq-producer project, and create new Class.
We will call this class as a "HelloRabbitProducer.java" and put it under package "producer".
A package in java is like a sub-folder to organize source files.
Since this is a producer, we will organize it into "producer" package.
OK, click Finish
Spring has a special class, called RabbitTemplate,
that handle publish to RabbitMQ.
So we will create it as variable on HelloRabbitProducer.
I can type few letters and press ctrl+space and Eclipse will suggest auto completion.
Next, we will create a method to send a message to RabbitMQ.
The message is just simple greeting string.
We will call it sendHello() with parameter name.
I'll type it, save it, and explain to you.
Dont forget about the magic of ctrl+space to autocomplete.
I will use a lot of them as I type.
All messages that send to RabbitMQ will be sent in form of byte array.
Without Spring, we will have to convert a message to byte array and then send it.
With Spring, we have RabbitTemplate that do the job for us using rabbitTemplate.convertAndSend
method.
In this lecture, rabbitTemplate.convertAndSend takes two parameters: queue name and message
to send.
Notice several things: We don't use exchange as parameter.
With this method, we will send message to RabbitMQ default exchange, which will put
our message into queue name in first parameter.
We don't need to convert message to byte array.
RabbitTemplate do that for us
For our Producer to run, we will need two
special lines, called annotations.
Annotation @Service in class level to set Spring knows that this Producer is a service class
and annotation @Autowired for rabbitTemplate, so Spring create RabbitTemplate instance for
us.
Notice the placement of both annotations.
It must be exact placement.
I won't explain very detail about Spring in this course.
If you want to know more about Spring annotations, please do some google for now.
To test the program let's go to RabbitmqProducerApplication.java.
I will type first and explain later.
Here, we have HelloRabbitProducer that annotated as Autowired.
Spring will create and inject HelloRabbitProducer to be used.
To run our program, we will create command-line based program, so the class is implements
interface CommandLineRunner
Our RabbitmqProducerApplication needs to implements
specific method from CommandLineRunner interface.
To do this, Eclipse can help by clicking the error message on the line, and select Add
unimplemented methods
Here, I will send a message with parameter
my name and some random number
OK, It should be done.
To test it, right click on RabbitmqProducerApplication.java, select Run as…
Java application
See the console tab.
If no error message, then your message is sent to RabbitMQ.
Don't forget to stop application from Eclipse console window.
Next, we'll see if our message exists on RabbitMQ.
Open RabbitMQ management console, go to tab queues, and open course.hello
To see the message, go to "Get messages" section
and click "Get message" button.
Leave the other option for now.
OK, message received.
Now it's time to build the consumer.
Let's create the consumer.
On Eclipse, right click on rabbitmq-consumer project, and create new Class.
We will call this class as "HelloRabbitConsumer.java" and put it under package "consumer".
Consumer does not need RabbitTemplate, but will use Spring annotation to consume message.
I'll type it and explain to you.
Dont forget about the magic of ctrl+space to autocomplete.
I will use a lot of them as I type.
For our Consumer to run, we will need two annotations.
Annotation @Service in class level to let Spring knows that this Consumer is a service
class, and annotation @RabbitListener to consume messages from queue.
Annotation @RabbitListener is written before method name, so that method will consume message
from queue in annotation's parameter.
So in this case, our listen() method will consume message from queue "course.hello"
and display it to console.
To test the program let's go to RabbitmqConsumerApplication.java.
Basically, this code is ready to run and consume message.
Let's try it, right click on RabbitmqConsumerApplication.java and choose Run as…
Java Application.
See, this is our message from rabbitmq
Let's try again.
Don't stop consumer, but we will run producer once more
OK, that is our second message.
For now lets stop the consumer
And the producer too
In the next lecture, we will prove that consumer consume messages in real-time.
See you
Không có nhận xét nào:
Đăng nhận xét