安装:
1.使用homebrew下载rabbitMQ:
brew install rabbitmq
执行结果如下:
Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/core). ==> Updated Formulae cake dockviz kubeless sdcc ==> Renamed Formulae geth -> ethereum Error: Could not link: /usr/local/share/doc/homebrew Please delete these paths and run `brew update`. ==> Installing dependencies for rabbitmq: jpeg, libpng, libtiff, wxmac, erlang ==> Installing rabbitmq dependency: jpeg ==> Downloading https://homebrew.bintray.com/bottles/jpeg-9c.high_sierra.bottle. ######################################################################## 100.0% ==> Pouring jpeg-9c.high_sierra.bottle.tar.gz 🍺 /usr/local/Cellar/jpeg/9c: 21 files, 724.5KB ==> Installing rabbitmq dependency: libpng ==> Downloading https://homebrew.bintray.com/bottles/libpng-1.6.34.high_sierra.b ######################################################################## 100.0% ==> Pouring libpng-1.6.34.high_sierra.bottle.tar.gz 🍺 /usr/local/Cellar/libpng/1.6.34: 26 files, 1.2MB ==> Installing rabbitmq dependency: libtiff ==> Downloading https://homebrew.bintray.com/bottles/libtiff-4.0.9_3.high_sierra ######################################################################## 100.0% ==> Pouring libtiff-4.0.9_3.high_sierra.bottle.tar.gz 🍺 /usr/local/Cellar/libtiff/4.0.9_3: 246 files, 3.5MB ==> Installing rabbitmq dependency: wxmac ==> Downloading https://homebrew.bintray.com/bottles/wxmac-3.0.4.high_sierra.bot ######################################################################## 100.0% ==> Pouring wxmac-3.0.4.high_sierra.bottle.tar.gz 🍺 /usr/local/Cellar/wxmac/3.0.4: 810 files, 23.7MB ==> Installing rabbitmq dependency: erlang ==> Downloading https://homebrew.bintray.com/bottles/erlang-20.3.2.high_sierra.b ######################################################################## 100.0% ==> Pouring erlang-20.3.2.high_sierra.bottle.tar.gz ==> Caveats Man pages can be found in: /usr/local/opt/erlang/lib/erlang/man Access them with `erl -man`, or add this directory to MANPATH. ==> Summary 🍺 /usr/local/Cellar/erlang/20.3.2: 7,036 files, 277.4MB ==> Installing rabbitmq ==> Downloading https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.4/rabbit ######################################################################## 100.0% ==> /usr/bin/unzip -qq -j /usr/local/Cellar/rabbitmq/3.7.4/plugins/rabbitmq_mana ==> Caveats Management Plugin enabled by default at http://localhost:15672 Bash completion has been installed to: /usr/local/etc/bash_completion.d To have launchd start rabbitmq now and restart at login: brew services start rabbitmq Or, if you don't want/need a background service you can just run: rabbitmq-server ==> Summary 🍺 /usr/local/Cellar/rabbitmq/3.7.4: 232 files, 12.6MB, built in 16 seconds
rabbitMQ安装路径: /usr/local/Cellar/rabbitmq/3.7.4
2. 进入到安装路径下,执行命令启动rabbitMQ服务:
sbin/rabbitmq-server
3. 然后另开终端,启动rabbitMQ的管理插件:
cd sbin && ./rabbitmq-plugins enable rabbitmq_management
4. 输入地址localhost:15672即可看到rabbitMQ管理界面
Java client端的使用:
在RabbitMQ的管理界面中添加一个QUEUE,name:demo,其他选项默认即可。
引入包:
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> </dependency>
根据rabbitMQ官网所示,编写发送端与接收端:
1.sender:
public class Send { private static final String QUEUE_NAME = "demo"; public static void main(String[] args) throws IOException, TimeoutException, NoSuchAlgorithmException, KeyManagementException, URISyntaxException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.1"); factory.setPort(5672); factory.setUsername("admin"); factory.setPassword("admin"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, true, false, false, null); String message = "Hello World!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println( " [x] send '" + message + "'"); channel.close(); connection.close(); } }
2. receiver:
public class Receive { private static final String QUEUE_NAME = "demo"; public static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.1"); factory.setPort(5672); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, true, false, false, null); System.out.println( " [*] Waiting for message. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); } }; channel.basicConsume(QUEUE_NAME, true, consumer); } }
3.在启动发送端和接收端时,可能会遇到这个问题:java.util.concurrent.TimeoutException
解决方案:(参考文档:http://docs.celeryproject.org/en/latest/getting-started/brokers/rabbitmq.html)
修改hosts文件,在127.0.0.1 localhost后面加上 hostname hostname.local,如下所示:
127.0.0.1 localhost miaoying-MacBookPro miaoying-MacBookPro.local