It focuses on RabbitMQ environment deployment and simple test cases.
preface
I wrote the message queue series back in April, which focused on message queue selection and the fundamentals of RabbitMQ, Kafka and RocketMQ. I chose RabbitMQ in action to pay off a debt.
Setting up the RabbitMQ environment
Since I use a Mac, I can refer to the website directly:
www.rabbitmq.com/install-hom…
When you need to pay attention, you must first perform:
brew update
Copy the code
Then execute:
brew install rabbitmq
Copy the code
Brew install rabbitMQ without brew update (403 Forbidde) will cause all sorts of strange errors.
If you install RabbitMQ from source, you will need to install Erlang manually as it is dependent on the Erlang environment to start the service. Install all dependencies for Rabbitmq automatically, isn’t that great?
The successful output is as follows:
Start the service:
1 # startup mode: started the background the brew services start the rabbitmq # 2: the current window boot CD/usr/local/Cellar/rabbitmq / 3.8.19 the rabbitmq server. -Copy the code
Type in your browser:
http://localhost:15672/
Copy the code
The RabbitMQ admin page will appear (both username and password are guest) :
Through brew installation, one line command is done, really fragrant!
The RabbitMQ test
Add account
First you have to start MQ
/rabbitmqctl set_permissions -p "/" admin ".*".*".*" ./rabbitmqctl set_user_tags admin administratorCopy the code
Coding the measured
Because Java 8 features are introduced in the code, POM introduces dependencies:
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.5.1</version> </dependency> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins>Copy the code
Start writing code:
public class RabbitMqTest {
// Message queue name
private final static String QUEUE_NAME = "hello";
@Test
public void send(a) throws java.io.IOException, TimeoutException {
// Create connection project
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("admin");
// Create a connection
Connection connection = factory.newConnection();
// Create a message channel
Channel channel = connection.createChannel();
// Generate a message queue
channel.queueDeclare(QUEUE_NAME, true.false.false.null);
for (int i = 0; i < 10; i++) {
String message = "Hello World RabbitMQ count: " + i;
// Publish the message. The first parameter indicates the route (Exchange name). "" indicates the default message route
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
}
// Close the message channel and connection
channel.close();
connection.close();
}
@Test
public void consumer(a) throws java.io.IOException, TimeoutException {
// Create a connection factory
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("admin");
// Create a connection
Connection connection = factory.newConnection();
// Create a message channel
final Channel channel = connection.createChannel();
// Message queue
channel.queueDeclare(QUEUE_NAME, true.false.false.null);
System.out.println("[*] Waiting for message. To exist press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + message + "'");
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {}); }}Copy the code
Console output after send() :
[x] Sent 'Hello World RabbitMQ count: 0'
[x] Sent 'Hello World RabbitMQ count: 1'
[x] Sent 'Hello World RabbitMQ count: 2'
[x] Sent 'Hello World RabbitMQ count: 3'
[x] Sent 'Hello World RabbitMQ count: 4'
[x] Sent 'Hello World RabbitMQ count: 5'
[x] Sent 'Hello World RabbitMQ count: 6'
[x] Sent 'Hello World RabbitMQ count: 7'
[x] Sent 'Hello World RabbitMQ count: 8'
[x] Sent 'Hello World RabbitMQ count: 9'
Copy the code
After executing consumer() :
Examples of code, can be directly refer to the website: www.rabbitmq.com/tutorials/t…
Welcome everyone to like a lot, more articles, please pay attention to the wechat public number “Lou Zai advanced road”, point attention, do not get lost ~~