转载请注明出处:http://www.cnblogs.com/blazer/p/4958007.html
环境:CentOS6.4
必须安装erlang
先去官网下载
下linux源码版 版本 otp_src_18.1
然后./configure 会报个小错 Makefile:248: /usr/local/otp_src_18.1/make/x86_64-unknown-linux-gnu/otp_ded.mk: No such file or directory error: No curses library functions found
然后需要安装 sudo yum install ncurses-devel.x86_64
然后再继续 ./configure
make
make install
好了
接下来安装非常不便利的rabbitmq
同样是官网下载 版本 rabbitmq_server-3.5.6
然后解压
cd rabbitmq_server-3.5.6
启动 sh sbin/rabbitmq-server
帮助 sh sbin/rabbitmqctl
sh sbin/rabbitmqctl add_user hyy hyy
sh sbin/rabbitmqctl set_permissions -p / hyy ".*" ".*" ".*"
然后就是非常简单的java代码了 其他的自己去官网看吧
发送端:
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
/**
* @author Blazer He
* @__date 2015年11月9日
*/
public class Send {
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.0.57");
factory.setUsername("hyy");
factory.setPassword("hyy");
factory.setVirtualHost("/");
factory.setPort(5672);
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());
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();
}
}
接收端:
import java.util.Date; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.QueueingConsumer; public class Consume { private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.0.57"); factory.setUsername("hyy"); factory.setPassword("hyy"); factory.setVirtualHost("/"); 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(new Date() + " [x] Received '" + message + "'"); } } }
好了,初步可以跑起来了,还有更多的功能需要深入。