mac下使用RabbitMQ(Java语言)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mac下使用RabbitMQ(Java语言)相关的知识,希望对你有一定的参考价值。
操作系统为OS X Yosemite 10.10.3
?
1.下载安装
在官网http://www.rabbitmq.com/download.html下载RabbitMQ Server,这个页面右侧有导航,可以找到Install: Mac OS X链接,或者页面中的Installation Guides项目下可以找到Mac OS X:?Standalone下载地址,现在最新版本为rabbitmq-server-mac-standalone-3.6.0.tar.xz,下载解压这个文件。
?
2.启动服务器
终端进入解压之后的目录
cd rabbitmq_server-3.6.0
cd sbin
sudo sh rabbitmq-server
终端输出:
RabbitMQ 3.6.0. Copyright (C) 2007-2015 Pivotal Software, Inc.
## ## Licensed under the MPL. See http://www.rabbitmq.com/
## ##
########## Logs: /Users/zhangmeng27/Downloads/proj/RabbitMQ/rabbitmq_server-3.6.0/var/log/rabbitmq/[email protected]
###### ## /Users/zhangmeng27/Downloads/proj/RabbitMQ/rabbitmq_server-3.6.0/var/log/rabbitmq/[email protected]
##########
Starting broker... completed with 0 plugins.
表明RabbitMQ 服务器已经启动
3.查询服务器状态,关闭服务器
另启一个终端,进入rabbitmq_server-3.6.0/sbin目录下,可以通过?sh rabbitmqctl status 命令查看已经启动的服务器的状态,rabbitmqcrl相关的命令解释可以在这里找到
关闭RabbitMQ服务器可以在启动服务器的那个终端中通过control+c关闭服务,或者新启一个终端进入sbin目录,通过?sh rabbitmqctl stop?命令关闭服务器。
4.通过web管理和监控RabbitMQ
rabbitmqctl组合对应命令可以通过终端查看rabbitmq服务器的状态,但是这样很不直观,rabbitmq还提供了web页面查看并管理rabbitmq服务器,这个功能通过插件实现,默认插件没有开启,所以上面启动服务器过程中最后提示completed with 0 plugins。启动rabbitmq服务器的状态下,另起一个终端在sbin下执行?sh rabbitmq-plugins enable rabbitmq_management?会提示
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Applying plugin configuration to [email protected] started 6 plugins.
现在可以通http://localhost:15672打开web页面
使用默认的账号密码guest/guest登录,登录之后可以看到rabbitmq服务器的内部状态,队列中的消息收发数目,连接数量,状态等信息?
5.java代码实现Producer和Consumer
创建一个maven项目,pom.xml中添加rabbitmq依赖,并使用assembly插件进行打包,因为最终需要同时启动Producer和Consumer两个客户端,所以打包方便后面的实验。
打包配置中是把消费者MQConsumer作为main类的,因此之后生成的jar包启动的是消费者。
pom.xml配置如下?
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>java.test</groupId>
<artifactId>rabbitmq</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.0.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>test.rabbitmq.MQConsumer</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- 此处指定继承合并 -->
<phase>package</phase> <!-- 绑定到打包阶段 -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
?消费者代码如下
package test.rabbitmq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import java.io.IOException;
/**
* Created by zhangmeng27 on 16/2/1.
*/
public class MQConsumer {
private final static String QUEUE_NAME = "hello";
public static void recv(){
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try {
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME,true,consumer);
while(true){
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println(" [x] Received ‘" + message + "‘");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
recv();
}
}
?消息的生产者的代码 ?
package test.rabbitmq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.Scanner;
/**
* Created by zhangmeng27 on 16/2/1.
*/
public class MQProducer {
private static final String QUEUE_NAME = "hello";
public void start(){
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = null;
Channel channel = null;
Scanner sc = new Scanner(System.in);
try {
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "hello world,try again";
System.err.println("-----输入字符串,回车进行发送,\"quit\"退出------");
while(sc.hasNext()){
message = sc.nextLine();
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent ‘" + message + "‘");
if(message.equals("quit")){
message = "Producer已经退出!!";
System.out.println(" [x] Sent ‘" + message + "‘");
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
channel.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MQProducer p = new MQProducer();
p.start();
}
}
IntelliJ IDEA中make之后会在target下生成rabbitmq-1.0-SNAPSHOT-jar-with-dependencies.jar文件,启动RabbitMQ服务器,终端进入刚才生成jar包所在目录,通过java -jar?rabbitmq-1.0-SNAPSHOT-jar-with-dependencies.jar启动消费者
在IntelliJ IDEA中启动MQProducer这个类,控制台中输入字符串回车即可发送。下图左边为启动的消费者终端,右边为IntelliJ IDEA中控制台
参考资料:
http://p.primeton.com/articles/53b3f3b0e1382303e2000037
http://www.rabbitmq.com/tutorials/tutorial-one-java.html
以上是关于mac下使用RabbitMQ(Java语言)的主要内容,如果未能解决你的问题,请参考以下文章