首页 技术 正文
技术 2022年11月7日
0 收藏 475 点赞 559 浏览 2764 个字

RabbitMQ是什么 ?

RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。

1:安装RabbitMQ需要先安装Erlang语言开发包。下载地址 http://www.erlang.org/download.html 在win7下安装Erlang最好默认安装。

配置环境变量 ERLANG_HOME C:\Program Files (x86)\erl5.9

添加到PATH  %ERLANG_HOME%\bin;

2:安装RabbitMQ 下载地址 http://www.rabbitmq.com/download.html  安装教程:http://www.rabbitmq.com/install-windows.html

配置环境变量 C:\Program Files (x86)\RabbitMQ Server\rabbitmq_server-2.8.0

添加到PATH %RABBITMQ_SERVER%\sbin;

3:进入%RABBITMQ_SERVER%\sbin 目录以管理员身份运行 rabbitmq-plugins.bat

安装完成之后以管理员身份启动 rabbitmq-service.bat

4:浏览器访问localhost:55672  默认账号:guest  密码:guest

创建队列名称为queue_sina ,java示例代码读写队列中queue_sina的消息queue_sina

private static final String exchangeName = “sina”;

private static final String exchangeRoutingKey = “sina”;

HashMap<String,String> map = new HashMap<String,String>();

map.put(“text”, request.getText());

map.put(“image”, imageUrl);

map.put(“nick_name”, this.getUserName(request.getUserid()));

map.put(“shop_name”, request.getShopname());

String tousu_map = gson.toJson(map, new TypeToken<HashMap<String,String>>(){}.getType());

System.out.println(“tousu_map” + tousu_map);

//写入队列

Producer.sendMsg(PropsUtils.getInstance().getProperty(Constants.EXCHANGE_NAME,

exchangeName), PropsUtils.getInstance()

.getProperty(Constants.EXCHANGE_ROUTING_KEY,

exchangeRoutingKey), tousu_map);

//写入队列模版类

public class Producer {

private static AmqpTemplate amqpTemplate = null;

static {

ApplicationContext context = new AnnotationConfigApplicationContext(TousuConfiguration.class);

amqpTemplate = context.getBean(AmqpTemplate.class);

}

public static void sendMsg(String exchangeName,String routingKey,Object message){

amqpTemplate.convertAndSend(exchangeName, routingKey,message);

System.out.println(“exchangeName: “+exchangeName);

System.out.println(“routingKey: “+routingKey);

System.out.println(“Sent : “+message);

}

}

//读取队列消息

public static void main(String[] args) {

//test

try {

//队列名称 PropertiesUtil.QUEUE_NAME=queue_sina

String queueName = PropertiesUtil.QUEUE_NAME;

ConnectionFactory factory = new ConnectionFactory();

//PropertiesUtil.HOST = localhost

factory.setHost(PropertiesUtil.HOST);

//PropertiesUtil.USER=guest

factory.setUsername(PropertiesUtil.USER);

//PropertiesUtil.PASS=guest

factory.setPassword(PropertiesUtil.PASS);

//PropertiesUtil.PORT=5672

factory.setPort(Integer.parseInt(PropertiesUtil.PORT));

Connection conn = factory.newConnection();

Channel channel = conn.createChannel();

channel.queueDeclare(queueName, true, false, false, null);

QueueingConsumer consumer = new QueueingConsumer(channel);

channel.basicConsume(queueName, true, consumer);

while(true) {

try {

QueueingConsumer.Delivery delivery = consumer.nextDelivery();

String message = new String(delivery.getBody());

System.out.println(” [x] Received ‘” + message + “‘”);

} catch (ShutdownSignalException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

#Java

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291