代码来自rabbitmq官方实例
1、生成者
package com.sdnware.start04.rabbitmq;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.rabbitmq.client.Channel;
/**
*
* 生产者
* @author chenb.bob
* 2017年5月4日
*
*/
public class JavaProducer {
private final static String QUEUE_NAME =
"hello";
private static Logger LOG = LoggerFactory.getLogger(JavaProducer.class);
public static void main(String[] args) {
try {
ConnectionFactory factory =
new ConnectionFactory();
factory.setHost(
"192.168.100.205");
factory.setPort(
5672);
factory.setUsername(
"admin");
factory.setPassword(
"sdnware");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME,
false,
false,
false,
null);
String message =
"Hello World!";
channel.basicPublish(
"", QUEUE_NAME,
null, message.getBytes());
LOG.info(
" [x] Sent '" + message +
"'");
channel.close();
connection.close();
}
catch (IOException e) {
e.printStackTrace();
}
catch (TimeoutException e) {
e.printStackTrace();
}
}
}
2、消费者
package com.sdnware.start04.rabbitmq;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
/**
*
* 消费者
* @author chenb.bob
* 2017年5月4日
*
*/
public class JavaConsumer {
private final static String QUEUE_NAME =
"hello";
private static Logger LOG = LoggerFactory.getLogger(JavaConsumer.class);
public static void main(String[] args) {
try {
ConnectionFactory factory =
new ConnectionFactory();
factory.setHost(
"192.168.100.205");
factory.setPort(
5672);
factory.setUsername(
"admin");
factory.setPassword(
"sdnware");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME,
false,
false,
false,
null);
LOG.info(
" [*] Waiting for messages.");
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");
LOG.info(
" [x] Received '" + message +
"'");
}
};
channel.basicConsume(QUEUE_NAME,
true, consumer);
}
catch (IOException e) {
e.printStackTrace();
}
catch (TimeoutException e) {
e.printStackTrace();
}
}
}
3、一个错误
如果配置connection-factory时,采用默认的guest/guest账号密码时,有可能会出现org
.springframework.amqp.AmqpAuthenticationException:
com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.的错误提示,解决办法是新建一个管理员权限的用户,并允许访问虚拟主机。步骤如下:
1、打开http://rabbitMQ_IP:
15672/
2、Admin ——> Users, 新建用户,administrator权限。
3、Virtual Hosts,设置新建用户允许访问。