windows下安装RabbitMQ消息服务器 + 读写队列
?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轉載于:https://www.cnblogs.com/systemnet123/p/3267967.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的windows下安装RabbitMQ消息服务器 + 读写队列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WIN7 64位系统注册银行支付组件
- 下一篇: Why NoSQL?